home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Snippets / MicroAnimationDemo / MicroAnimationDemo.p < prev    next >
Encoding:
Text File  |  1995-12-30  |  4.3 KB  |  140 lines  |  [TEXT/PJMM]

  1. {MicroAnimationDemo by Ingemar Ragnemalm}
  2.  
  3. {Was OffscreenToys too elaborate for you? I know, it keeps some extra}
  4. {information to avoid redrawing the whole window, and the collision}
  5. {handling is pretty fancy. This is stipped down to the limit! (You can}
  6. {reduce the number of lines a little bit, by taking out commants and}
  7. {compilation switches, but not much more.)}
  8.  
  9. {This was really intended for the Mac game programming book, as a first}
  10. {intro to offscren animation, but they found someone else to make that}
  11. {chapter, so what the heck, then I'll let it be a free little demo (and}
  12. {won't bother making a C version).}
  13.  
  14. {So, if it is THIS simple to make sprite animation, what's the point with}
  15. {SAT or even OffscreenToys? I'll tell you: Speed, flexibility, compatibility,}
  16. {expandability.}
  17.  
  18. {PlotCIcon is not the fastest routine around. Actually, it is one of the}
  19. {slower. Check out the boost code in OffScreenToys to learn how to speed}
  20. {it up (replacing PlotCIcon with CopyBits). Also, it can be speeded up by}
  21. {not erasing the entire offscreen and by not copying all of it every frame.}
  22. {However, more speed takes more code.}
  23.  
  24. program MicroAnimationDemo;
  25.     uses
  26. {$IFC UNDEFINED THINK_PASCAL}
  27.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  28.         OSUtils, ToolUtils, Resources, Icons, 
  29. {$ELSEC}
  30. {$SETC GENERATINGPOWERPC := false}
  31. {$ENDC}
  32.         QDOffScreen;
  33.     const
  34.         kMaxObject = 5;
  35.     var
  36.         cicn: array[0..kMaxObject] of CIconHandle;
  37.         pos: array[0..kMaxObject] of Rect;
  38.         speed: array[0..kMaxObject] of Point;
  39.         wind: WindowPtr;
  40.         offScreen: GrafPtr;
  41.         backPat: PixPatHandle;
  42.         i: Integer;
  43.         startTicks: Longint;
  44.  
  45. (* Standard inits *)
  46.     procedure InitToolbox;
  47.     begin
  48. {$IFC UNDEFINED THINK_PASCAL}
  49.         InitGraf(@qd.thePort);
  50.         InitFonts;
  51.         FlushEvents(everyEvent, 0);
  52.         InitWindows;
  53.         InitMenus;
  54.         TEInit;
  55.         InitDialogs(nil);
  56.         InitCursor;
  57. {$ENDC}
  58.     end; (*InitToolbox*)
  59.  
  60. begin
  61.     InitToolbox;
  62.  
  63. {$IFC NOT GENERATINGPOWERPC}
  64. {Is Color QD and 32-bit QD around?}
  65.     if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AA1E, ToolTrap) then {Color QD?}
  66.         halt;
  67.     if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AB1D, ToolTrap) then {32-bit QD?}
  68.         halt;
  69. {$ENDC}
  70.  
  71. {Seed the random number generator}
  72. {$IFC UNDEFINED THINK_PASCAL}
  73.     qd.randSeed := TickCount;
  74. {$ELSEC}
  75.     randSeed := TickCount;
  76. {$ENDC}
  77.  
  78. {Create window from resource}
  79.     wind := GetNewCWindow(128, nil, WindowPtr(-1));
  80.     SetPort(wind);
  81.  
  82. {Make GWorld with default depth and CLUT}
  83. {$IFC UNDEFINED THINK_PASCAL}
  84.     if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, pixelsLocked) then
  85. {$ELSEC}
  86.         if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, [pixelsLocked]) then
  87. {$ENDC}
  88.             halt;
  89. {We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.}
  90.     if LockPixels(CGrafPtr(offScreen)^.portPixMap) then
  91.         ;
  92.  
  93.     SetGWorld(GWorldPtr(offScreen), nil);
  94.  
  95. {Get a pattern and set it to the background pattern for offScreen}
  96.     backPat := GetPixPat(128);
  97.     BackPixPat(backPat);
  98.  
  99. {Set up all objects, loading the 'cicn' resource, and setting its position and speed}
  100.     for i := 0 to kMaxObject do
  101.         begin
  102.             cicn[i] := GetCIcon(128 + i);
  103.             pos[i] := cicn[i]^^.iconMask.bounds;
  104.             OffsetRect(pos[i], abs(Random) mod wind^.portRect.right, abs(Random) mod wind^.portRect.bottom);
  105.             speed[i].h := Random mod 8;
  106.             speed[i].v := Random mod 8;
  107.         end;
  108.  
  109. {*** The animation loop: ***}
  110.  
  111.     while not Button do
  112.         begin
  113.             startTicks := TickCount;
  114. {Erase the offscreen, resetting it to the background image/pattern}
  115.             SetGWorld(GWorldPtr(offScreen), nil);
  116.             EraseRect(wind^.portRect);
  117. {For all objects, move, draw off-screen and do border checks}
  118.             for i := 0 to kMaxObject do
  119.                 begin
  120.                     OffSetRect(pos[i], speed[i].h, speed[i].v);
  121.                     PlotCIcon(pos[i], cicn[i]);
  122.                     if pos[i].top < 0 then
  123.                         speed[i].v := abs(speed[i].v);
  124.                     if pos[i].left < 0 then
  125.                         speed[i].h := abs(speed[i].h);
  126.                     if pos[i].bottom > wind^.portRect.bottom then
  127.                         speed[i].v := -abs(speed[i].v);
  128.                     if pos[i].right > wind^.portRect.right then
  129.                         speed[i].h := -abs(speed[i].h);
  130.                 end;
  131. {Copy from offscreen to screen}
  132.             SetGWorld(GWorldPtr(wind), GetMainDevice);
  133.             CopyBits(offScreen^.portBits, wind^.portBits, wind^.portRect, wind^.portRect, srcCopy, nil);
  134. {Wait until at least one tick has passed}
  135.             while TickCount = startTicks do
  136.                 ;
  137.         end;
  138. {Set back the device before we quit}
  139.     SetGWorld(GWorldPtr(wind), GetMainDevice);
  140. end. {MicroAnimationDemo}