home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #9 / Amiga Plus CD - 2004 - No. 09.iso / amigaplus / games / saga / source / gfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-08-03  |  8.2 KB  |  247 lines

  1. // 1. INCLUDES -----------------------------------------------------------
  2.  
  3. #include <exec/exec.h>
  4. #include <intuition/intuition.h>
  5. #include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE */
  6.  
  7. #include "saga.h"
  8.  
  9. #define ANFS      ANFRACSIZE
  10. #define BNFS      (ANFRACSIZE-2)
  11.  
  12. /* These are used for MeMask (1 << ID_xxx) */
  13. #define ID_BORDER 0
  14. #define ID_BNG    4
  15.  
  16. #include "animtools.h"
  17. #include "gfx.h"
  18.  
  19. // 2. DEFINES ------------------------------------------------------------
  20.  
  21. // 3. EXPORTED VARIABLES -------------------------------------------------
  22.  
  23. // 4. IMPORTED VARIABLES -------------------------------------------------
  24.  
  25. IMPORT  struct Window   *HelpWindowPtr,
  26.                         *MainWindowPtr;
  27. IMPORT  UWORD            DisplayDepth;
  28.  
  29. // 5. MODULE VARIABLES ---------------------------------------------------
  30.  
  31. MODULE struct GelsInfo*  gInfo;
  32. MODULE struct AnimOb    *boingOb,
  33.                         *animKey;
  34.  
  35. // 6. MODULE STRUCTURES --------------------------------------------------
  36.  
  37. // 7. MODULE FUNCTIONS ---------------------------------------------------
  38.  
  39. MODULE  struct AnimOb*   setupBoing(void);
  40. MODULE  struct GelsInfo* setupDisplay(void);
  41. MODULE  WORD             goInFrontOfHead(struct AnimComp *aComp);
  42. MODULE  WORD             goBehindHead(struct AnimComp *aComp);
  43.  
  44. // 8. CODE ---------------------------------------------------------------
  45.  
  46. AGLOBAL void drawabout(void)
  47. {   DrawImage(HelpWindowPtr->RPort, &About, 16 + 2, 20 + 2);
  48. }
  49. AGLOBAL void drawlogo(void)
  50. {   DrawImage(MainWindowPtr->RPort, &Logo, (SCREENXPIXEL / 2) - (212 / 2), TBSIZE + 66);
  51. }
  52.  
  53. MODULE struct GelsInfo* setupDisplay(void)
  54. {   struct GelsInfo* gInfo;
  55.  
  56.     /* set up the gels system.
  57.     ** 0x03 says: when you allocate sprites for me, don't ever use
  58.     ** sprites zero or one. This guarantees that sprite zero, the
  59.     ** intuition pointer, stays intact. Remember sprite one shares
  60.     ** colors with sprite zero. */
  61.  
  62.     if (NULL != (gInfo = setupGelSys(HelpWindowPtr->RPort, 0x03)))
  63.     {   return(gInfo);
  64.     } else
  65.     {   return(NULL);
  66. }   }
  67.  
  68. AGLOBAL VOID DrawGels(void)
  69. {   Animate(&animKey, HelpWindowPtr->RPort);
  70.     SortGList(HelpWindowPtr->RPort);   /* Put the list in order. */
  71.     DoCollision(HelpWindowPtr->RPort); /* Collision routines may called now */
  72.     SortGList(HelpWindowPtr->RPort);   /* Put the list in order. */
  73.     DrawGList(HelpWindowPtr->RPort, ViewPortAddress(HelpWindowPtr));
  74.     WaitTOF();
  75. }
  76.  
  77. /*--------------------------------------------------------------
  78. ** setupBoing() - allocate and initialize an object that will
  79. ** display as a boing ball with orbiting satellites.
  80. **
  81. ** this is an animation object with four animation sequences
  82. ** (boing and three satellites). Note that the satellites all share the
  83. ** same single image data.
  84. **
  85. ** return NULL on failure.
  86. */
  87. MODULE struct AnimOb* setupBoing(void)
  88. {   struct AnimOb*   bngOb;
  89.     struct AnimComp* bngComp;
  90.     struct AnimComp* satAComp;
  91.     struct AnimComp* satBComp;
  92.     struct AnimComp* satCComp;
  93.  
  94.     /* Firstly, we allocate and initialize an AnimOb structure, then we */
  95.     /* call makeSeq() for the main ball and for each satellite. */
  96.  
  97.     if (NULL != (bngOb = AllocMem((LONG)sizeof(struct AnimOb), MEMF_CLEAR)))
  98.     {   bngOb->NextOb           = NULL;
  99.     bngOb->PrevOb        = NULL;
  100.     bngOb->Clock        = 0;
  101.         bngOb->AnY              = ( 28 << ANFRACSIZE);
  102.         bngOb->AnX              = (  3 << ANFRACSIZE);
  103.     bngOb->AnOldY        = bngOb->AnY;
  104.     bngOb->AnOldX        = bngOb->AnX;
  105.         bngOb->YVel             = 0;
  106.         bngOb->XVel             = 1 << ANFRACSIZE;
  107.     bngOb->YAccel        = 0;
  108.     bngOb->XAccel        = 0;
  109.     bngOb->RingYTrans    = BNG3RINGY << ANFRACSIZE;
  110.     bngOb->RingXTrans    = BNG3RINGX << ANFRACSIZE;
  111.     bngOb->AnimORoutine    = bounceORoutine;
  112.     bngOb->AUserExt        = 0;
  113.  
  114.         newBoingBob.nb_DBuf     = 0;
  115.         newBoingBob.nb_RasDepth = DisplayDepth;
  116.  
  117.         newBoingSeq.nas_HeadOb  = bngOb;
  118.  
  119.         satACRoutines[4]        = goInFrontOfHead;
  120.         satACRoutines[12]       = goBehindHead;
  121.         satBCRoutines[8]        = goInFrontOfHead;
  122.         satBCRoutines[0]        = goBehindHead;
  123.         satCCRoutines[4]        = goInFrontOfHead;
  124.         satCCRoutines[12]       = goBehindHead;
  125.  
  126.         newSatABob.nb_DBuf      = 0;
  127.         newSatBBob.nb_DBuf      = 0;
  128.         newSatCBob.nb_DBuf      = 0;
  129.         newSatASeq.nas_HeadOb   = bngOb;
  130.         newSatBSeq.nas_HeadOb   = bngOb;
  131.         newSatCSeq.nas_HeadOb   = bngOb;
  132.  
  133.     if (NULL != (bngComp = makeSeq(&newBoingBob, &newBoingSeq)))
  134.         {   bngComp->Flags |= RINGTRIGGER;
  135.             bngOb->HeadComp = bngComp;
  136.  
  137.             if (NULL != (satAComp = makeSeq(&newSatABob, &newSatASeq)))
  138.             {   bngComp->AnimBob->Before = satAComp->AnimBob;
  139.                 satAComp->AnimBob->After = bngComp->AnimBob;
  140.  
  141.                 if (NULL != (satBComp = makeSeq(&newSatBBob, &newSatBSeq)))
  142.                 {   satAComp->AnimBob->Before = satBComp->AnimBob;
  143.                     satBComp->AnimBob->After = satAComp->AnimBob;
  144.  
  145.                     if (NULL != (satCComp = makeSeq(&newSatCBob, &newSatCSeq)))
  146.                     {   satBComp->AnimBob->Before = satCComp->AnimBob;
  147.                         satCComp->AnimBob->After = satBComp->AnimBob;
  148.  
  149.                         bngComp->NextComp  = satAComp;
  150.                         bngComp->PrevComp  = NULL;
  151.  
  152.                         satAComp->NextComp = satBComp;
  153.                         satAComp->PrevComp = bngComp;
  154.  
  155.                         satBComp->NextComp = satCComp;
  156.                         satBComp->PrevComp = satAComp;
  157.  
  158.                         satCComp->NextComp = NULL;
  159.                         satCComp->PrevComp = satBComp;
  160.  
  161.                         return(bngOb);
  162.                     }
  163.                     freeSeq(satBComp, DEPTH);
  164.                 }
  165.                 freeSeq(satAComp, DEPTH);
  166.             }
  167.             freeSeq(bngComp, DEPTH);
  168.         }
  169.     FreeMem(bngOb, (LONG)sizeof(struct AnimOb));
  170.     }
  171.     return(NULL);
  172. }
  173.  
  174. /*------------------------------------------------------
  175. ** This ORoutine makes the Object Bounce off Borders. */
  176.  
  177. WORD bounceORoutine(struct AnimOb* anOb)
  178. {   SHORT X = anOb->AnX >> ANFRACSIZE;
  179.  
  180.     if (((X                                                     < 3              ) && (anOb->XVel < 0)) ||
  181.         ((X + (anOb->HeadComp->AnimBob->BobVSprite->Width << 4) > ABOUTXPIXEL - 3) && (anOb->XVel > 0)))
  182.        {   anOb->XVel = -(anOb->XVel);
  183.        }
  184.  
  185.     return(0);
  186. }
  187.  
  188. /*--------------------------------------------------------------
  189. ** This CRoutine rearranges Bob Before and After pointers in a
  190. ** way that makes the Component passed look like it is in front
  191. ** of its' head component.
  192. **
  193. ** Used for Boing satelittes.
  194. **
  195. ** So that they go in front of AND behind the boing ball.
  196. */
  197. MODULE WORD goInFrontOfHead(struct AnimComp *aComp)
  198. {   /* close up hole */
  199.     if (aComp->AnimBob->Before != NULL)
  200.     aComp->AnimBob->Before->After = aComp->AnimBob->After;
  201.     if (aComp->AnimBob->After != NULL)
  202.     aComp->AnimBob->After->Before = aComp->AnimBob->Before;
  203.  
  204.     /* reinsert */
  205.     aComp->AnimBob->Before = aComp->HeadOb->HeadComp->AnimBob->Before;
  206.     aComp->AnimBob->After = aComp->HeadOb->HeadComp->AnimBob;
  207.     if (aComp->AnimBob->Before != NULL)
  208.     aComp->AnimBob->Before->After = aComp->AnimBob;
  209.     aComp->HeadOb->HeadComp->AnimBob->Before = aComp->AnimBob;
  210.  
  211.     return(0);
  212. }
  213.  
  214. /*--------------------------------------------------------------
  215. ** This CRoutine rearranges Bob Before and After pointers in a
  216. ** way that makes the Component passed look like it is behind
  217. ** its' head component.
  218. */
  219. MODULE WORD goBehindHead(struct AnimComp *aComp)
  220. {   if (aComp->AnimBob->Before != NULL)
  221.     aComp->AnimBob->Before->After = aComp->AnimBob->After;
  222.     if (aComp->AnimBob->After != NULL)
  223.     aComp->AnimBob->After->Before = aComp->AnimBob->Before;
  224.  
  225.     aComp->AnimBob->After = aComp->HeadOb->HeadComp->AnimBob->After;
  226.     aComp->AnimBob->Before = aComp->HeadOb->HeadComp->AnimBob;
  227.     if (aComp->AnimBob->After != NULL)
  228.     aComp->AnimBob->After->Before = aComp->AnimBob;
  229.     aComp->HeadOb->HeadComp->AnimBob->After = aComp->AnimBob;
  230.  
  231.     return(0);
  232. }
  233.  
  234. AGLOBAL void boingball(void)
  235. {   if (NULL != (gInfo = setupDisplay()))
  236.     {   InitAnimate(&animKey);
  237.  
  238.         if (NULL != (boingOb = setupBoing()))
  239.         {   AddAnimOb(boingOb, &animKey, HelpWindowPtr->RPort);
  240. }   }   }
  241.  
  242. AGLOBAL void unboingball(void)
  243. {   freeOb(boingOb, DEPTH);
  244.     cleanupGelSys(gInfo, HelpWindowPtr->RPort);
  245. }
  246.  
  247.