home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEM / S-Z / SillyBalls / SillyBalls / SillyBalls.p / SillyBalls.p
Encoding:
Text File  |  1988-01-19  |  3.8 KB  |  134 lines  |  [TEXT/MPS ]

  1. Program SillyBalls;
  2. {
  3.     purpose        To demonstrate a simple color App using Color QuickDraw.
  4.                         It draws colored balls in a color window, then uses colored
  5.                         text inverted in the ball.  The ball location and color is Random.
  6.                         
  7.                         This program was written by Bo3b Johnson, 1/88.
  8.                         
  9.                         The inverted Bob text was a RIck Blair special concept,
  10.                         kept for obvious aesthetic reasons.
  11.                         
  12.                         You can build the program with this junk:
  13. pascal SillyBalls.p
  14. Link SillyBalls.p.o ∂
  15.     "yourDisk:MPW:Libraries:"Interface.o ∂
  16.     "yourDisk:MPW:Libraries:"Runtime.o ∂
  17.     "yourDisk:MPW:PLibraries:"Paslib.o ∂
  18.     -o SillyBalls
  19. SillyBalls
  20.  
  21. }
  22.  
  23. USES    Memtypes,QuickDraw,OSIntf,ToolIntf;
  24.  
  25. CONST
  26.     BallWidth        = 20;
  27.     BallHeight    = 20;
  28.     BobSize        = 8;        { Size of text in each ball. }
  29.  
  30. VAR
  31.     mainPtr:        WindowPtr;
  32.     windRect:        Rect;
  33.     
  34.     
  35. { Initialize everything for the program, make sure we can run. }
  36.  
  37. Procedure Initialize;
  38.  
  39. VAR
  40.     error:             OSErr;
  41.     theWorld:     SysEnvRec;
  42.     
  43. BEGIN
  44.     { Test the computer to be sure we can do color.  If not we would crash, which
  45.         would be bad.  If we can’t run, just beep and exit. }
  46.     error := SysEnvirons(1, theWorld);
  47.     IF NOT theWorld.hasColorQD THEN BEGIN
  48.         SysBeep (50);
  49.         ExitToShell;                    { If no color QD, we must leave. }
  50.     END;
  51.     
  52.     { Initialize all the needed managers. }
  53.     InitGraf(@thePort);
  54.     InitFonts;
  55.     InitWindows;
  56.     InitMenus;
  57.     TEInit;
  58.     InitDialogs(NIL);
  59.     InitCursor;
  60.  
  61.     { To make the Random sequences truly random, we need to make the seed start
  62.         at a different number.  An easy way to do this is to put the current time
  63.         and date into the seed.  Since it is always incrementing the starting seed
  64.         will always be different.  Don’t for each call of Random, or the sequence
  65.         will no longer be random.  Only needed once, here in the init. }
  66.     GetDateTime (RandSeed);
  67.  
  68.     { Make a new window for drawing in, and it must be a color window.  The window is
  69.         full screen size, made smaller to make it more visible. }
  70.     windRect := screenBits.bounds;
  71.     InsetRect (windRect, 50, 50);
  72.     MainPtr := NewCWindow(NIL, windRect, 'Bo3b Land', TRUE, documentProc, 
  73.         Pointer(-1), FALSE, 0);
  74.         
  75.     SetPort(MainPtr);                            { set window to current graf port   }
  76.     TextSize(BobSize);                        { smaller font for drawing. }
  77. END;    { Initialize }
  78.  
  79.  
  80. { NewBall: make another ball in the window at a random location and color. }
  81.  
  82. Procedure NewBall;
  83.  
  84. VAR
  85.     ballColor:        RGBColor;
  86.     ballRect:        Rect;
  87.     newLeft,
  88.     newTop:        LongInt;
  89.     
  90. BEGIN
  91.     { Make a random new color for the ball. }
  92.     WITH ballColor        DO BEGIN
  93.         red := Random;   green := Random;  blue := Random;
  94.     END;
  95.     
  96.     { Set that color as the new color to use in drawing. }
  97.     RGBForeColor (ballColor);
  98.  
  99.     { Make a Random new location for the ball, that is normalized to the window size.  
  100.         This makes the Integer from Random into a number that is 0..windRect.bottom
  101.         and 0..windRect.right.  They are normalized so that we don't spend most of our
  102.         time drawing in places outside of the window. }
  103.     newTop := Random;    newLeft := Random;
  104.     newTop := ((newTop+32767) * windRect.bottom) DIV 65536;
  105.     newLeft := ((newLeft+32767) * windRect.right) DIV 65536;
  106.     SetRect(ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  107.     
  108.     { Move pen to the new location, and paint the colored ball. }
  109.     MoveTo(newLeft, newTop);
  110.     PaintOval (ballRect);
  111.     
  112.     { Move the pen to the middle of the new ball position, for the text }
  113.     MoveTo(ballRect.Left + BallWidth DIV 2 - BobSize, 
  114.         ballRect.Top + BallHeight DIV 2 + BobSize DIV 2 -1);
  115.     
  116.     { Invert the color and draw the text there.  This won’t look quite right in 1 bit
  117.         mode, since the foreground and background colors will be the same.  Color
  118.         QuickDraw special cases this to not invert the color, to avoid invisible
  119.         drawing. }
  120.     InvertColor(ballColor); 
  121.     RGBForeColor(ballColor);
  122.     DrawString('Bob');
  123. END;    { NewBall }
  124.  
  125.  
  126. BEGIN       { Main body of program SillyBalls }
  127.     Initialize;
  128.     
  129.     Repeat
  130.         NewBall;
  131.     Until Button;
  132.     
  133. END.    { SillyBalls }
  134.