home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-15 | 14.0 KB | 611 lines | [TEXT/CWIE] |
- // NewLace
- // version 1.1.1
- // by Ken Long <kenlong@netcom.com>
- // updated to CW7 and THINK 7 on 951201
-
- //• --------------------------------------------------------------- •//
- //• An itty bitty bytes™ rewrite of the old MS QuickBasic demo
- //• done on 25 September 1995, by Kenneth A. Long.
- //• This is a Public Domain source code project for Novice C
- //• programmers.
- //• --------------------------------------------------------------- •//
- //• If you put this on a medium, such as a CD or magnetic disk,
- //• which is then sold, you must send me free copy.
- //• kenlong@netcom.com
- //• --------------------------------------------------------------- •//
-
- //• --------------------------------------------------------------- •//
- //• NewLace.c
- //• --------------------------------------------------------------- •//
-
- #include <Picker.h>
- #include <GestaltEqu.h>
-
- #define over qd.screenBits.bounds.right
- #define under qd.screenBits.bounds.bottom
- #define yo true //• Why type 4 leeters when you can type 2?
- #define no false
-
- //• --------------------------------------------------------------- •//
- long ticks;
- WindowPtr laceWind;
- Rect dragRect;
- Rect windRect;
- Rect laceRect = { 0, 0, 27, 27 };
-
- Boolean plus, minus, up, down, square, round, quit = false,
- pause, color, toggle, both, bw, degrees_30;
-
- short diameter, howWide, howHigh, offset;
-
- Str255 title = {
- "\pDiameter: 27, Offset: 2 • 'K' for keys."
- }; //• 12345678901234567890123456789012345678
- //• ^11 ^22
- //• ^10 ^20 ^30
-
- //• --------------------------------------------------------------- •//
- //• Prototypes:
-
- Boolean WeGotHues (void);
- void Originals (void);
- void DisplayData (void);
- void DoAbout (void);
- void DoKeys (void);
- void StartOver (Boolean original);
- short SelectColor (RGBColor *colorPtr);
- void SetForeColor (short red, short green, short blue);
- void SetBackColor (short red, short green, short blue);
- void FigureDirection (void);
- void DoLines (void);
- void Draw (void);
- void SetUpWindow (void);
- void InitMacintosh (void);
- void HandleMouseDown (EventRecord *theEvent);
- short HandleEvent (void);
- void main (void);
- //•
-
- //• --------------------------------------------------------------- •//
- //• Routines:
- //• --------------------------------------------------------------- •//
-
- //• --------------------------------------------------------------- •//
- Boolean WeGotHues (void)
- {
- unsigned char version[ 4 ];
- OSErr err;
-
- err = Gestalt (gestaltQuickdrawVersion, (long *)version);
-
- if (version[ 2 ] > 0)
- return (true);
- else
- return (false);
- }
-
- //• --------------------------------------------------------------- •//
- //• Initial settings.
- void Originals ()
- {
- plus = yo,
- minus = no,
- up = no,
- down = yo,
- square = no,
- round = yo,
- pause = no,
- color = no,
- toggle = yo,
- both = no,
- bw = yo,
- degrees_30 = no;
-
- howWide = 27;
- howHigh = 27;
- offset = 2;
- }
-
- //• --------------------------------------------------------------- •//
- //• Display the diameter and offset, of the sprite,
- //• in the title bar of the window.
- void DisplayData ()
- {
- diameter = laceRect.right - laceRect.left;
-
- if (diameter > 99)
- diameter = 99;
-
- if (diameter < 5)
- diameter = 5;
-
- if (offset > 9)
- offset = 9;
-
- if (offset < 1)
- offset = 1;
-
- if (diameter < 10)
- {
- NumToString (diameter, (title + 11));
- title[11] = '0';
- }
- else
- NumToString (diameter, (title + 10));
-
- NumToString (offset, (title + 22));
-
- //• NumToString inserts a '\0' we don't need.
- *(title + 10) = ' ';
- *(title + 22) = ' ';
- SetWTitle(laceWind, title);
- }
-
- //• --------------------------------------------------------------- •//
- //• Draw the "About" picture.
- void DoAbout ()
- {
- PicHandle pict;
- Rect picRect;
- short T, L, B, R;
-
- if (WeGotHues ())
- pict = GetPicture (128);
- else
- pict = GetPicture (130);
-
- T = (under / 2) - ((*pict)->picFrame.bottom / 2) - 10;
- L = (over / 2) - ((*pict)->picFrame.right / 2);
- B = T + (*pict)->picFrame.bottom;
- R = L + (*pict)->picFrame.right;
-
-
- EraseRect (&laceWind->portRect);
- SetRect (&picRect, L, T, R, B);
- DrawPicture (pict, &picRect);
- Delay (240L, &ticks);
- ReleaseResource ((Handle)pict);
- EraseRect (& laceWind->portRect);
- StartOver (no);
- }
-
- //• --------------------------------------------------------------- •//
- //• Draw the "Keys" picture.
- void DoKeys ()
- {
- PicHandle pict;
- Rect picRect;
- short T, L, B, R;
-
- if (WeGotHues ())
- pict = GetPicture (129);
- else
- pict = GetPicture (131);
-
-
- T = (under / 2) - ((*pict)->picFrame.bottom / 2) - 10;
- L = (over / 2) - ((*pict)->picFrame.right / 2);
- B = T + (*pict)->picFrame.bottom;
- R = L + (*pict)->picFrame.right;
-
-
- EraseRect (&laceWind->portRect);
- SetRect (&picRect, L, T, R, B);
- DrawPicture (pict, &picRect);
- Delay (300L, &ticks);
- ReleaseResource ((Handle)pict);
- EraseRect (& laceWind->portRect);
- StartOver (no);
- }
-
- //• --------------------------------------------------------------- •//
- //• Starts the sprite on a blank screen in the upper-left corner.
- //• Blanks screen with background color - default is white.
- void StartOver (Boolean original)
- {
- diameter = laceRect.right - laceRect.left;
-
- EraseRect (&laceWind->portRect);
-
- //• If original is passed, set the rect as it was on luanch.
- if (original)
- {
- SetRect (&laceRect, 0, 0, 27, 27);
- offset = 2;
- degrees_30 = no;
- howWide = 27;
- howHigh = 27;
- round = yo;
-
- }
-
- //• Otherwise, use diameter previously set, in upper left corner.
- else
- SetRect (&laceRect, 0, 0, howWide, howHigh);
-
- DisplayData ();
-
- //• Make it go down and to the right.
- down = yo;
- up = no;
- plus = yo;
- minus = no;
- }
-
- //• --------------------------------------------------------------- •//
- //• Calls the color picker.
- short SelectColor (RGBColor *colorPtr)
- {
- Point where;
-
- where.h = -1;
- where.v = -1;
-
- return (GetColor (where, "\pChoose background color", colorPtr,
- colorPtr));
- }
-
- //• --------------------------------------------------------------- •//
- //• Sets the 3 RGBForeColor struct members to what's passed in.
- //• (Not used, this trip.)
- void SetForeColor (short red, short green, short blue)
- {
- RGBColor crayola;
-
- crayola.red = red;
- crayola.green = green;
- crayola.blue = blue;
-
- RGBForeColor (&crayola);
- }
-
- //• --------------------------------------------------------------- •//
- //• Sets the 3 RGBBackColor struct members to what's passed in.
- //• (Not used, this trip.)
- void SetBackColor (short red, short green, short blue)
- {
- RGBColor crayola;
-
- crayola.red = red;
- crayola.green = green;
- crayola.blue = blue;
-
- RGBBackColor (&crayola);
- }
-
- //• --------------------------------------------------------------- •//
- //• Sets the driection of sprite according to window edge hit.
- void FigureDirection ()
- {
- Rect winder = laceWind->portRect;
-
- //• Sprite bottom greater than window bottom?
- if (laceRect.bottom > winder.bottom)
- {
- down = no; //• Change down to up...
- up = yo;
- }
- //• Sprite right greater than window right?
- if (laceRect.right > winder.right)
- {
- plus = no; //• Change right to left...
- minus = yo;
- }
- //• Sprite top lesser than window top?
- if (laceRect.top < winder.top)
- {
- up = no; //• Change up to down...
- down = yo;
-
- }
- //• Sprite left lesser than window left?
- if (laceRect.left < winder.left)
- {
- minus = no; //• Change left to right...
- plus = yo;
- }
- }
-
- //• --------------------------------------------------------------- •//
- //• Draws a grid based on sprite diameter.
- //• (Not used, this trip because some alignment coding is needed.
- //• I looks kind of cool in color mode, but must be aligned via
- //• bounce rect size.
- void DoLines ()
- {
- short x, y;
-
- PenNormal ();
- for (x = (diameter / 2); x < over; x += diameter)
- {
- MoveTo (x, 0);
- LineTo (x, under);
-
- for (y = (diameter / 2); y < over; y += diameter)
- {
- MoveTo (0, y);
- LineTo (over, y);
- }
- }
- PenNormal ();
- PenMode (srcXor);
- }
-
- //• --------------------------------------------------------------- •//
- //• Caleed once per event loop. Draws one sprite per event loop.
- void Draw ()
- {
- short first, last;
-
- if (both) //• If square AND round...
- if (toggle) //• If toogle is true...
- PaintOval (&laceRect); //• ...do an oval...
- else //• ...if not...
- PaintRect (&laceRect); //• ...do a square.
-
- //• We're usually round...
- if (round)
- PaintOval (&laceRect);
- else //• ...but it's hip to be square.
- PaintRect (&laceRect);
-
- //• If use hits slash, we do the diamond tracks...
- if (degrees_30)
- {
- //• Right direction.
- if ((plus) && (down))
- OffsetRect (&laceRect, offset / 2, offset);
- if ((plus) && (up))
- OffsetRect (&laceRect, offset / 2, - offset);
-
- //• Left direction.
- if ((minus) && (down))
- OffsetRect (&laceRect, - offset / 2, offset);
- if ((minus) && (up))
- OffsetRect (&laceRect, - offset / 2, - offset);
- }
- //• ...but normally we're trackin' square paths.
- else //• 45 degrees.
- {
- //• Right direction.
- if ((plus) && (down)) //• Down right wonderful!
- OffsetRect (&laceRect, offset, offset);
- if ((plus) && (up)) //• Up, right and vertical!
- OffsetRect (&laceRect, offset, - offset);
-
- //• Left direction.
- if ((minus) && (down)) //• Left down in the dumps!
- OffsetRect (&laceRect, - offset, offset);
- if ((minus) && (up)) //• Left up to powers that be!
- OffsetRect (&laceRect, - offset, - offset);
- }
- toggle = ! toggle;
- }
-
- //• --------------------------------------------------------------- •//
- //• We're doing an rDocProc, with no resource. This is so we still
- //• have a title bar to displat data in, yet black.
- //• It's a "C" window if not running on an antique!
- void SetUpWindow(void)
- {
- dragRect = qd.screenBits.bounds;
-
- SetRect (&windRect, 0, 19, over, under);
-
- if (WeGotHues ())
- laceWind = NewCWindow(0L, &windRect, title, yo, 16, (WindowPtr) -1L, yo, 0);
- else
- laceWind = NewWindow(0L, &windRect, title, yo, 16, (WindowPtr) -1L, yo, 0);
-
- SetPort(laceWind);
- }
-
- //• --------------------------------------------------------------- •//
- //• Get those managers off their butts!
- void InitMacintosh(void)
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- }
-
- //• --------------------------------------------------------------- •//
- //• Not used much, but here if we want to add some tricks...
- void HandleMouseDown (EventRecord *theEvent)
- {
- WindowPtr theWindow;
- short windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inContent:
- if (theWindow == laceWind)
- {
- if (theWindow != FrontWindow())
- SelectWindow(laceWind);
- else
- InvalRect(&laceWind->portRect);
- }
- break;
-
- case inGoAway:
- if (theWindow == laceWind &&
- TrackGoAway(laceWind, theEvent->where))
- ExitToShell();
- break;
- }
- }
-
- //• --------------------------------------------------------------- •//
- //• An event is being held and YOU are invited! If you go to the
- //• event, you're bound to get looped, eventually!
- short HandleEvent(void)
- {
- int ok;
- EventRecord theEvent;
- char theKey;
- RGBColor hue;
-
- HiliteMenu(0);
- SystemTask (); /* Handle desk accessories */
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
-
- //• Here's where we get most of our control.
- case keyDown:
- case autoKey:
- theKey = (theEvent.message & charCodeMask);
- switch (theKey)
- {
- case 'a':
- DoAbout (); //• What about it?
- break;
-
- case 'b': //• Square meal in round can!
- round = no;
- square = no;
- both = yo;
- StartOver (no);
- break;
-
- case 'c': //• Picker knows!
- if (! WeGotHues ())
- break;
-
- InitCursor ();
- SelectColor (&hue);
- HideCursor ();
- RGBBackColor (&hue);
- StartOver (yo);
- break;
-
- case 'd':
- bw = yo;
- BackColor (whiteColor);
- StartOver (yo); //• Get back, Loretta!
- break;
-
- case 'g': //• Is this too much?
- // if (! bw)
- // {
- // StartOver (no);
- // DoLines ();
- // }
- break;
-
- case 'k': //• What was that key?
- DoKeys (); //• Show me!
- break;
-
- case 'p': //• Pa's, paws, P. Oz.
- pause = ! pause;//• But, then, maybe not!
- break;
-
- case 'q': //• Bail.
- quit = true;
- break;
-
- case 'r': //• Round and round we go.
- round = yo;
- square = no;
- both = no;
- StartOver (no);
- break;
-
- case 's': //• Equalateral quadrangle!
- round = no;
- square = yo;
- both = no;
- StartOver (no);
- break;
-
- case '+': //• Fatten up our sprite.
- if (howWide > 98)
- break;
- howWide += 1;
- howHigh += 1;
- StartOver (no); //• No reset.
- break;
-
- case '-': //• Go on a diet.
- if (howWide < 5)
- break;
- howWide -= 1;
- howHigh -= 1;
- StartOver (no); //• No reset.
- break;
-
- case ']': //• Step it up, a little!
- offset += 1;
- StartOver (no);
- break;
-
- case '[': //• Whoa! To fast!
- offset -= 1;
- StartOver (no);
- break;
-
- case '/': //• The angle of the dangle.
- degrees_30 = ! degrees_30;
- StartOver (no);
- break;
- }
- break;
-
- case updateEvt:
- BeginUpdate(laceWind);
-
- //• No bulls, hit!
- // DrawBullseye(((WindowPeek) laceWind)->hilited);
- EndUpdate(laceWind);
- break;
-
- case activateEvt:
- InvalRect(&laceWind->portRect);
- break;
- }
- if (! pause)
- {
- FigureDirection ();
- Draw();
- }
- }
-
- //• --------------------------------------------------------------- •//
- void main(void)
- {
- InitMacintosh();
- // WeGotHues ();
- Originals ();
- HideMenuBar ();
- HideCursor (); //• Swat that fly!
- SetUpWindow();
- DoAbout ();
- DoKeys ();
- PenMode (srcXor); //• This is so we leave tracks.
-
- while (! quit)
- HandleEvent ();
- PenNormal ();
- ShowMenuBar ();
- }
- //• --------------------------------------------------------------- •//
- //• --------------------------------------------------------------- •//
-