home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SprocketInvaders / Source / Particles.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.5 KB  |  185 lines  |  [TEXT/MPS ]

  1. //•    ------------------------------------------------------------------------------------------    •
  2. //•
  3. //•    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  4. //•
  5. //•
  6. //•        You may incorporate this sample code into your applications without
  7. //•        restriction, though the sample code has been provided "AS IS" and the
  8. //•        responsibility for its operation is 100% yours.  However, what you are
  9. //•        not permitted to do is to redistribute the source as "DSC Sample Code"
  10. //•        after having made changes. If you're going to re-distribute the source,
  11. //•        we require that you make it clear in the source that the code was
  12. //•        descended from Apple Sample Code, but that you've made changes.
  13. //•
  14. //•        Authors:
  15. //•            Chris De Salvo
  16. //•
  17. //•    ------------------------------------------------------------------------------------------    •
  18.  
  19. //•    ------------------------------    Includes
  20.  
  21. #include "ErrorHandler.h"
  22. #include "GameObject.h"
  23. #include "MemoryHandler.h"
  24. #include "Particles.h"
  25. #include "SprocketInvaders.h"
  26.  
  27. //•    ------------------------------    Private Definitions
  28.  
  29. #define kSpeckLifeTime                50
  30. #define kSpeckLifeTimeVariance        10
  31.  
  32. #define kSpeckSpeedVariance            4
  33. #define kSpeckDistanceVariance        5
  34.  
  35. //•    ------------------------------    Private Types
  36. //•    ------------------------------    Private Variables
  37. //•    ------------------------------    Private Functions
  38.  
  39. static void SpecksInit(SpeckPtr theSpecks, SInt32 x, SInt32 y);
  40. static SInt32 SpeckRand(SInt16 fromZero);
  41.  
  42. //•    ------------------------------    Public Variables
  43.  
  44. //•    --------------------    ParticlesAllocate
  45.  
  46. ParticlesPtr
  47. ParticlesAllocate(SInt32 x, SInt32 y, UInt8 color)
  48. {
  49. ParticlesPtr    theParticles;
  50. SpeckPtr        theSpecks;
  51.  
  52.     theSpecks = (SpeckPtr) NewTaggedPtrClear(sizeof (Speck) * kNumSpecks, 'SPEK', 0);
  53.     if (! theSpecks)
  54.         FatalError("Could not allocate particle specks.");
  55.  
  56.     theParticles = (ParticlesPtr) NewTaggedPtrClear(sizeof (Particles), 'PTCL', 0);
  57.     if (! theParticles)
  58.         FatalError("Could not allocate particles.");
  59.  
  60.     theParticles->numSpecks = kNumSpecks;
  61.     theParticles->specks = theSpecks;
  62.     theParticles->color = color;
  63.  
  64.     SpecksInit(theSpecks, x, y);
  65.  
  66.     return (theParticles);
  67. }
  68.  
  69. //•    --------------------    ParticlesDispose
  70.  
  71. void
  72. ParticlesDispose(ParticlesPtr *theParticles)
  73. {
  74.     DisposeTaggedPtrZ((Ptr *) & (*theParticles)->specks);
  75.     DisposeTaggedPtrZ((Ptr *) theParticles);
  76. }
  77.  
  78. //•    --------------------    ParticlesDraw
  79.  
  80. Rect
  81. ParticlesDraw(ParticlesPtr theParticles, CGrafPtr dest)
  82. {
  83. Rect        r = { 32767, 32767, -32767, -32767 };
  84. SpeckPtr    specks;
  85. UInt32        i;
  86. Rect        bounds;
  87. UInt8        *base;
  88. UInt32        rowBytes;
  89. Boolean        touched;
  90. UInt32        colorVariance;
  91.  
  92.     touched = false;
  93.     specks = theParticles->specks;
  94.     bounds = theParticles->bounds;
  95.  
  96.     base = (UInt8 *) GetPixBaseAddr(dest->portPixMap);
  97.     rowBytes = (**dest->portPixMap).rowBytes & 0x3FFF;
  98.  
  99.     for (i = 0; i < theParticles->numSpecks; i++)
  100.     {
  101.         if (specks[i].age < specks[i].lifeTime)
  102.         {
  103.         Point    p;
  104.         
  105.             //•    All the color ramps are 8-entries long going from lighter to darker.
  106.             //•    These two lines "age" the color of the particle so that it gets dimmer as
  107.             //•    it ages.  This gives the effect of an ember dying out or something.
  108.             //•    Anyway, I like the way it looks -- Chris
  109.             colorVariance = specks[i].age * 8;
  110.             colorVariance /= specks[i].lifeTime;
  111.         
  112.             p.h = specks[i].h;
  113.             p.v = specks[i].v;
  114.             
  115.             if (PtInRect(p, &bounds))
  116.             {
  117.                 if (p.h < r.left)
  118.                     r.left = p.h;
  119.                     
  120.                 if (p.h > r.right)
  121.                     r.right = p.h;
  122.                     
  123.                 if (p.v < r.top)
  124.                     r.top = p.v;
  125.                     
  126.                 if (p.v > r.bottom)
  127.                     r.bottom = p.v;
  128.                     
  129.                 touched = true;
  130.                 
  131.                 * (base + p.h + (p.v * rowBytes)) = theParticles->color + colorVariance;
  132.             }
  133.         }
  134.     }
  135.  
  136.     if (! touched)
  137.         SetRect(&r, 0, 0, 0, 0);
  138.  
  139.     return (r);
  140. }
  141.  
  142. //•    --------------------    SpecksInit
  143.  
  144. static void
  145. SpecksInit(SpeckPtr theSpecks, SInt32 x, SInt32 y)
  146. {
  147. UInt32    i;
  148.  
  149.     for (i = 0; i < kNumSpecks; i++)
  150.     {
  151.     SInt32    speed;
  152.     
  153.         theSpecks[i].age = 0;
  154.         theSpecks[i].lifeTime = kSpeckLifeTime + SpeckRand(kSpeckLifeTimeVariance);
  155.         theSpecks[i].h = x + SpeckRand(kSpeckDistanceVariance);
  156.         theSpecks[i].v = y + SpeckRand(kSpeckDistanceVariance);
  157.         
  158.         speed = SpeckRand(kSpeckSpeedVariance);
  159.         if (speed < 0)
  160.             theSpecks[i].speedH = speed;// - 1;
  161.         else
  162.             theSpecks[i].speedH = speed;// + 1;
  163.             
  164.         speed = SpeckRand(kSpeckSpeedVariance);
  165.         if (speed < 0)
  166.             theSpecks[i].speedV = speed;// - 1;
  167.         else
  168.             theSpecks[i].speedV = speed;// + 1;
  169.     }
  170. }
  171.  
  172. //•    --------------------    SpeckRand
  173.  
  174. static SInt32
  175. SpeckRand(SInt16 fromZero)
  176. {
  177. SInt32    theRand;
  178.  
  179.     theRand = Random();
  180.     theRand *= fromZero;
  181.     theRand /= 32767;
  182.     
  183.     return (theRand);
  184. }
  185.