home *** CD-ROM | disk | FTP | other *** search
-
- /* Hugh Fisher 1993
-
- Demo program to show off EachRegionRect code.
-
- Generates a series of regions and then redraws
- them using the function. Click to move on to
- the next.
- */
-
- #include <stdio.h>
-
- WindowPtr window;
-
- #define ArraySize(array) (sizeof((array))/sizeof((array)[0]))
-
- extern void EachRegionRect (RgnHandle r, void (* proc)(Rect *));
-
- void initMac (void);
-
- void makeRegion (RgnHandle * rgn, Point points[], short npoints);
- void makeDisconnected (RgnHandle * rgn);
- void makeHole (RgnHandle * rgn);
-
- void waitClick (void);
- void slowMotion (Rect * r);
- void showRegion (RgnHandle rgn);
-
- static Point square[] = {
- { 0, 0 },
- { 96, 0 },
- { 96, 96 },
- { 0, 96 },
- { 0, 0 }
- };
-
- static Point T_up[] = {
- { 32, 0 },
- { 64, 0 },
- { 64, 48 },
- { 96, 48 },
- { 96, 96 },
- { 0, 96 },
- { 0, 48 },
- { 32, 48 },
- { 32, 0 }
- };
-
- static Point T_down[] = {
- { 0, 0 },
- { 96, 0 },
- { 96, 48 },
- { 64, 48 },
- { 64, 96 },
- { 32, 96 },
- { 32, 48 },
- { 0, 48 },
- { 0, 0 }
- };
-
- static Point H_shape[] = {
- { 0, 0 },
- { 32, 0 },
- { 32, 32 },
- { 64, 32 },
- { 64, 0 },
- { 96, 0 },
- { 96, 96 },
- { 64, 96 },
- { 64, 64 },
- { 32, 64 },
- { 32, 96 },
- { 0, 96 },
- { 0, 0 }
- };
-
- static Point I_shape[] = {
- { 0, 0 },
- { 96, 0 },
- { 96, 32 },
- { 64, 32 },
- { 64, 64 },
- { 96, 64 },
- { 96, 96 },
- { 0, 96 },
- { 0, 64 },
- { 32, 64 },
- { 32, 32 },
- { 0, 32 },
- { 0, 0 }
- };
-
- void initMac ()
- {
- /* Standard Toolbox incantation */
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- SetEventMask(everyEvent);
- FlushEvents(everyEvent, 0);
- InitCursor();
- }
-
- void makeRegion (RgnHandle * rgn, Point points[], short npoints)
- {
- short loop;
-
- *rgn = NewRgn();
- OpenRgn();
- MoveTo(points[0].v, points[0].h);
- for (loop = 1; loop < npoints; loop ++)
- LineTo(points[loop].v, points[loop].h);
- CloseRgn(*rgn);
- PaintRgn(*rgn);
- }
-
- void makeDisconnected (RgnHandle * rgn)
- {
- Rect bounds;
-
- *rgn = NewRgn();
- OpenRgn();
- SetRect(&bounds, 0, 0, 32, 32);
- FrameRect(&bounds);
- SetRect(&bounds, 64, 64, 96, 96);
- FrameRect(&bounds);
- CloseRgn(*rgn);
- PaintRgn(*rgn);
- }
-
- void makeHole (RgnHandle * rgn)
- {
- Rect bounds;
-
- *rgn = NewRgn();
- OpenRgn();
- SetRect(&bounds, 0, 0, 32, 96);
- FrameRect(&bounds);
- SetRect(&bounds, 64, 0, 96, 96);
- FrameRect(&bounds);
- SetRect(&bounds, 32, 0, 64, 32);
- FrameRect(&bounds);
- SetRect(&bounds, 32, 64, 64, 96);
- FrameRect(&bounds);
- CloseRgn(*rgn);
- PaintRgn(*rgn);
- }
-
- void slowMotion (Rect * r)
- {
- short x, y;
- long now;
- PenState save;
-
- /* Use invert rather than normal draw to make
- sure we aren't doing any pixels twice */
- GetPenState(&save);
- PenMode(patXor);
- for (x = r->left; x < r->right; x++) {
- now = TickCount();
- while (now == TickCount())
- SystemTask();
- MoveTo(x, r->top);
- LineTo(x, r->bottom);
- }
- SetPenState(&save);
- }
-
- void showRegion (RgnHandle rgn)
- {
- long discard;
-
- Delay(60, &discard);
- EraseRect(&(**rgn).rgnBBox);
- EachRegionRect(rgn, slowMotion);
- Delay(60, &discard);
- EraseRect(&window->portRect);
-
- }
-
- void main ()
- {
- #define kWidth 256
- #define kHeight 256
-
- Rect bounds;
- RgnHandle rgn;
- short x, y;
-
- initMac();
-
- x = (screenBits.bounds.right - screenBits.bounds.left - kWidth) / 2;
- y = GetMBarHeight() + 32;
- SetRect(&bounds, x, y, x + kWidth, y + kHeight);
- window = NewWindow(NULL, &bounds, "\pRegion to Rectangles", TRUE, noGrowDocProc, (WindowPtr)-1, FALSE, 0);
- SetPort(window);
- SetOrigin(-64, -64);
-
- makeRegion(&rgn, square, ArraySize(square));
- showRegion(rgn);
-
- makeRegion(&rgn, T_up, ArraySize(T_up));
- showRegion(rgn);
-
- makeRegion(&rgn, T_down, ArraySize(T_down));
- showRegion(rgn);
-
- makeRegion(&rgn, H_shape, ArraySize(H_shape));
- showRegion(rgn);
-
- makeRegion(&rgn, I_shape, ArraySize(I_shape));
- showRegion(rgn);
-
- makeHole(&rgn);
- showRegion(rgn);
-
- makeDisconnected(&rgn);
- showRegion(rgn);
- }
-