home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / vsprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  6.4 KB  |  183 lines

  1. /* The listing given here shows a complete VSprite example.  This
  2. ** program requires the animtools.c, animtools.h and animtools_proto.h
  3. ** support files in order to compile and run.  These files are listed at
  4. ** the end of this chapter.
  5. **
  6. ** vsprite.c
  7. **
  8. ** SAS/C V5.10a
  9. ** lc -b1 -cfist -v -y vsprite.c
  10. ** blink FROM LIB:c.o vsprite.o animtools.o LIB LIB:lc.lib LIB:amiga.lib TO vsprite
  11. */
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <intuition/intuitionbase.h>
  15. #include <graphics/gfx.h>
  16. #include <graphics/gfxbase.h>
  17. #include <graphics/gels.h>
  18. #include <graphics/collide.h>
  19. #include <libraries/dos.h>
  20. #include <stdlib.h>
  21. #include "animtools.h"
  22.  
  23. VOID borderCheck(struct VSprite *hitVSprite, LONG borderflags);
  24. VOID process_window(struct Window *win, struct RastPort *myRPort, struct VSprite *MyVSprite);
  25. VOID do_VSprite(struct Window *win, struct RastPort *myRPort);
  26. VOID vspriteDrawGList(struct Window *win, struct RastPort *myRPort);
  27.  
  28. struct GfxBase         *GfxBase;   /* pointer to Graphics library  */
  29. struct IntuitionBase *IntuitionBase; /* pointer to Intuition library */
  30.  
  31. int return_code;
  32. #define GEL_SIZE    4 /* number of lines in the vsprite */
  33.  
  34. /* VSprite data - there are two sets that are alternated between. */
  35. /* note that this data is always displayed as low resolution.      */
  36. WORD chip vsprite_data1[] = { 0x7ffe, 0x80ff,
  37.                               0x7c3e, 0x803f,
  38.                               0x7c3e, 0x803f,
  39.                               0x7ffe, 0x80ff,
  40.                               0, 0 };
  41.  
  42. WORD chip vsprite_data2[] = { 0x7ffe, 0xff01,
  43.                               0x7c3e, 0xfc01,
  44.                               0x7c3e, 0xfc01,
  45.                               0x7ffe, 0xff01,
  46.                               0, 0 };
  47.  
  48. WORD mySpriteColors[] =     { 0x0000, 0x00f0, 0x0f00 };
  49. WORD mySpriteAltColors[] =  { 0x000f, 0x0f00, 0x0ff0 };
  50.  
  51. NEWVSPRITE myNewVSprite = {              /* information for the new VSprite       */
  52.         /* Image data, sprite color array word width (must be 1 for true VSprite) */
  53.         vsprite_data1, mySpriteColors,1,
  54.         /* Line height, image depth (must be 2 for true VSprite), x, y position   */                                                           */
  55.         GEL_SIZE, 2, 160, 100,
  56.         /* Flags (VSPRITE == true VSprite), hit mask and me mask                  */
  57.         VSPRITE, 1 << BORDERHIT, 0
  58.         };
  59.  
  60. struct NewWindow myNewWindow = {        /* information for the new window */
  61.     80, 20, 400, 150, -1, -1, CLOSEWINDOW | INTUITICKS,
  62.     ACTIVATE | WINDOWCLOSE | WINDOWDEPTH | RMBTRAP | WINDOWDRAG,
  63.     NULL, NULL, "VSprite", NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN
  64.     };
  65.  
  66. /* Basic VSprite display subroutine */
  67. VOID vspriteDrawGList(struct Window *win, struct RastPort *myRPort)
  68. {
  69. SortGList(myRPort);
  70. DrawGList(myRPort, ViewPortAddress(win));
  71. RethinkDisplay();
  72. }
  73.  
  74. /* Collision routine for vsprite hitting border.  Note that when the collision is VSprite to */
  75. /* VSprite (or Bob to Bob, Bob to AnimOb, etc), then the parameters are both pointers to a VSprite. */
  76. VOID borderCheck(struct VSprite *hitVSprite, LONG borderflags)
  77. {
  78. if (borderflags & RIGHTHIT)
  79.     {
  80.     hitVSprite->SprColors = mySpriteAltColors;
  81.     hitVSprite->VUserExt  = -40;
  82.     }
  83. if (borderflags & LEFTHIT)
  84.     {
  85.     hitVSprite->SprColors = mySpriteColors;
  86.     hitVSprite->VUserExt  = 20;
  87.     }
  88. }
  89.  
  90. /* Process window and dynamically change vsprite. Get messages. Go away on           */
  91. /* CLOSEWINDOW.  Update and redisplay vsprite on INTUITICKS. Wait for more messages. */
  92. VOID process_window(struct Window *win, struct RastPort *myRPort, struct VSprite *myVSprite)
  93. {
  94. struct IntuiMessage *msg;
  95.  
  96. FOREVER
  97.     {
  98.     Wait(1L << win->UserPort->mp_SigBit);
  99.     while (NULL != (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  100.         {
  101.         /* Only CLOSEWINDOW and INTUITICKS are active */
  102.         if (msg->Class == CLOSEWINDOW)
  103.             {
  104.             ReplyMsg((struct Message *)msg);
  105.             return;
  106.             }
  107.         /* Must be an INTUITICKS:  change x and y values on the fly.  Note offset by
  108.         ** window left and top edge--sprite relative to the screen, not window.  Divide
  109.         ** the MouseY in half to adjust for Lores movement increments on a Hires screen.
  110.         */
  111.         myVSprite->X = win->LeftEdge + msg->MouseX + myVSprite->VUserExt;
  112.         myVSprite->Y = win->TopEdge  + msg->MouseY/2 + 1;
  113.         ReplyMsg((struct Message *)msg);
  114.         }
  115.     /* Got a message, change image data on the fly */
  116.     myVSprite->ImageData = (myVSprite->ImageData == vsprite_data1) ? vsprite_data2 : vsprite_data1;
  117.     SortGList(myRPort);
  118.     DoCollision(myRPort);
  119.     vspriteDrawGList(win, myRPort);
  120.     }
  121. }
  122.  
  123. /* Working with the VSprite.  Setup the GEL system and get a new VSprite (makeVSprite()).   */
  124. /* Add VSprite to the system and display.  Use the vsprite.  When done, remove VSprite and  */
  125. /* update the display without the VSprite.  Cleanup everything.                             */
  126. VOID do_VSprite(struct Window *win, struct RastPort *myRPort)
  127. {
  128. struct VSprite       *myVSprite;
  129. struct GelsInfo       *my_ginfo;
  130.  
  131. if (NULL == (my_ginfo = setupGelSys(myRPort, 0xfc)))
  132.     return_code = RETURN_WARN;
  133. else
  134.     {
  135.     if (NULL == (myVSprite = makeVSprite(&myNewVSprite)))
  136.         return_code = RETURN_WARN;
  137.     else
  138.         {
  139.         AddVSprite(myVSprite, myRPort);
  140.         vspriteDrawGList(win, myRPort);
  141.         myVSprite->VUserExt = 20;
  142.         SetCollision(BORDERHIT, borderCheck, myRPort->GelsInfo);
  143.         process_window(win, myRPort, myVSprite);
  144.         RemVSprite(myVSprite);
  145.         freeVSprite(myVSprite);
  146.         }
  147.     vspriteDrawGList(win, myRPort);
  148.     cleanupGelSys(my_ginfo, myRPort);
  149.     }
  150. }
  151.  
  152. /* Example VSprite program.  First open up the libraries and a window. */
  153. VOID main(int argc, char **argv)
  154. {
  155. struct Window       *win;
  156. struct RastPort    myRPort = {0};
  157.  
  158. return_code = RETURN_OK;
  159.  
  160. if (NULL == (GfxBase = (struct GfxBase *)OpenLibrary(GRAPHICSNAME,37L)))
  161.     return_code = RETURN_FAIL;
  162. else
  163.     {
  164.     if (NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary(INTUITIONNAME,37L)))
  165.         return_code = RETURN_FAIL;
  166.     else
  167.         {
  168.         if (NULL == (win = OpenWindow(&myNewWindow)))
  169.             return_code = RETURN_WARN;
  170.         else
  171.             {
  172.             InitRastPort(&myRPort);
  173.             myRPort = win->WScreen->RastPort;       /* Copy the structure. */
  174.             do_VSprite(win, &myRPort);
  175.             CloseWindow(win);
  176.             }
  177.         CloseLibrary((struct Library *)IntuitionBase);
  178.         }
  179.     CloseLibrary((struct Library *)GfxBase);
  180.     }
  181. exit(return_code);
  182. }
  183.