home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / rkrm.lha / RKRM / Graphics_Libraries / Sprites_Bobs / vsprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  7.3 KB  |  203 lines

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