home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / Blasto-P / Blasto.p next >
Encoding:
Text File  |  1995-02-16  |  4.3 KB  |  149 lines  |  [TEXT/MPPS]

  1. program Blasto;
  2. {    A small program that blasts an 8 bit color icon to the screen.}
  3. {    3-6-92         By Brigham Stevens}
  4. {                Apple Developer Technical Support}
  5. {    3-13-92    - BRS - Uh Oh, it's Friday the 13th}
  6. {                Cleaned up & commented code here and there.}
  7. {    4-13-92 - BRS - changed icon to be self-erasing}
  8. {                Made code somewhat better by changing names and stuff.}
  9. {                Added comments}
  10. {    4-24-92 - BRS - Added ifdefs around timing code, changed names again.}
  11. {    11-11-92 - BRS - made a THINK C project file, and made this code work with }
  12. {                    THINK C.  Also changed the date in the comment above. }
  13.  
  14.     uses
  15. {$IFC UNDEFINED THINK_PASCAL}
  16.         OSUtils, Windows, QDOffscreen, Retrace, Memory, Resources, Events, Menus, 
  17.         Dialogs, Fonts,
  18. {MPW:}    OSEvents,
  19. {$ENDC}
  20.         DirectScreen;
  21.  
  22. {Procedure InitToolBox(numMoreMasters:Integer);}
  23.  
  24. (* Amount to move icons these need to be around 1 or 2    *)
  25. (*    or our icon will not be self erasing *)
  26.     const
  27.         ROW_OFF = 1;
  28.         COL_OFF = 1;
  29.         MAX_SECONDS = 100;
  30.  
  31. {Procedure main;}
  32.     var
  33.         directWindow: WindowPtr;
  34.         screenRect: Rect;
  35.         mainScreen: GDHandle;
  36.         mainscreenPixMap: PixMapHandle;
  37.         colorIconHandle: Handle;
  38.         plotLocation: Point;
  39.         vDelta: Integer;
  40.         hDelta: Integer;
  41.         frameCount: LongInt;
  42. {ticks: LongPtr;}
  43.         beforeTicks, afterTicks: LongInt;
  44. {tickTable: array[0..MAX_SECONDS] of LongInt;}
  45. {tickIndex: Integer;}
  46.  
  47. begin
  48. (* Standard Witch Chant enclosed... *)
  49.  
  50. {$IFC UNDEFINED THINK_PASCAL}
  51.     InitGraf(@qd.thePort);
  52.     InitFonts;
  53.     InitWindows;
  54.     InitMenus;
  55.     InitCursor;
  56.     TEInit;
  57.     FlushEvents(everyEvent, 0);
  58.     InitDialogs(nil);
  59. {    InitToolBox(4);        (* 4 calls to MoreMasters *)
  60. {$ENDC}
  61.  
  62.     vDelta := ROW_OFF;
  63.     hDelta := COL_OFF;
  64. {tickIndex := 0;}
  65. {Original program used the global}
  66. {ticks := LongPtr($16A);}
  67. {to get the ticks.}
  68. {Using the global för ticks is not a good idea. It's better to use the TickCount routine!}
  69.  
  70.     (* get a handle to the color icon we are drawing *)
  71.     colorIconHandle := GetResource('icl8', 128);
  72.     if colorIconHandle = nil then
  73.         begin
  74.             SysBeep(5);
  75. {DebugStr('Build problems: 'icl8' not loaded.');}
  76.             halt;
  77.         end;
  78.  
  79.     (* cover up the part of the screen we are writing on *)
  80.     (* with a window, so other apps will not mess with our *)
  81.     (* animation via update events in the background *)
  82. {$IFC UNDEFINED THINK_PASCAL}
  83.     screenRect := qd.screenBits.bounds;
  84. {$ELSEC}
  85.     screenRect := screenBits.bounds;
  86. {$ENDC}
  87.     directWindow := NewWindow(nil, screenRect, '', true, plainDBox, WindowPtr(-1), false, 0);
  88.  
  89.     (* This covers up the menu bar *)
  90.     (* and fills the screen with white *)
  91.     SetPort(directWindow);
  92.     RectRgn(directWindow^.visRgn, directWindow^.portRect);
  93.     EraseRect(directWindow^.portRect);
  94.  
  95. {Note: The original program created another port with OpenPort. Expanding the window's visRgn is better.}
  96. {Then we stay in a real window, get update events properly, can't accidentally draw in other windows,}
  97. {at least not with QuickDraw. Basically, we take a lot less risks.}
  98.  
  99.     (* get the main screen's Pix map *)
  100.     (* Which this program expects to be 8 bits deep *)
  101.     mainScreen := GetMainDevice;
  102.     mainscreenPixMap := mainScreen^^.gdPMap;
  103.  
  104.     (* Set up a safe rectangle for bouncing off the screen *)
  105.     screenRect.top := 4;
  106.     screenRect.left := 4;
  107.     screenRect.bottom := screenRect.bottom - 32;
  108.     screenRect.right := screenRect.bottom - 32;
  109.  
  110.     (* loop until the mouse is clicked *)
  111.     (* drawing and moving the color icon *)
  112.  
  113.         (* This is where to start drawing the color icon*)
  114.     plotLocation.h := 100;
  115.     plotLocation.v := 100;
  116.     frameCount := 0;
  117.     HideCursor;
  118.     beforeTicks := TickCount;
  119.     
  120.     repeat
  121.         begin
  122. (* update the location of the icon *)
  123.             plotLocation.v := plotLocation.v + vDelta;
  124.             plotLocation.h := plotLocation.h + hDelta;
  125. (* This forces us to stay on the screen *)
  126.             if not PtInRect(plotLocation, screenRect) then
  127.                 begin
  128.                     vDelta := -vDelta;    (* swap the deltas to bounce of corner *)
  129.                     hDelta := -hDelta;
  130.                 end;
  131. (* draw the icon *)
  132.             DirectPlotColorIcon(LongPtr(colorIconHandle^), mainscreenPixMap, plotLocation.v, plotLocation.h);
  133.             frameCount := frameCount + 1;
  134.         end
  135.     until Button;
  136.  
  137.     (* show the average number of frames per second for MAX_SECONDS seconds *)
  138.     afterTicks := TickCount;
  139.  
  140.     ShowCursor;
  141.  
  142.     ParamText(stringof('Frames/sec:', (frameCount * 60 div (TickCount - beforeTicks)) : 1), '', '', '');
  143.     if 1 = Alert(128, nil) then
  144.         ;
  145.  
  146.     CloseWindow(directWindow);
  147.     DrawMenuBar;
  148.     FlushEvents(mDownMask, 0);
  149. end.