home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / graphics / sprite_bob_anim / vsprite / vsprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  6.9 KB  |  242 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. /* vsprite.c 19oct89 CATS
  23. **
  24. ** lattice c 5.04
  25. ** lc -b1 -cfist -v -y vsprite.c
  26. ** blink FROM LIB:c.o vsprite.o /animtools/animtools.o LIB LIB:lc.lib TO vsprite
  27. */
  28. #include <exec/types.h>
  29. #include <intuition/intuition.h>
  30. #include <graphics/gels.h>
  31. #include <graphics/collide.h>
  32. #include <exec/memory.h>
  33. #include <libraries/dos.h>
  34.  
  35. #include <proto/all.h>
  36. #include <stdlib.h>
  37.  
  38. #include "/animtools/animtools.h"
  39. #include "/animtools/animtools_proto.h"
  40.  
  41. VOID borderCheck(struct VSprite *hitVSprite, LONG borderflags);
  42. VOID process_window(struct Window *win, struct VSprite *MyVSprite);
  43. VOID do_VSprite(struct Window *win);
  44. VOID vspriteDrawGList(struct Window *win);
  45.  
  46. struct GfxBase         *GfxBase;       /* pointer to Graphics library */
  47. struct IntuitionBase *IntuitionBase; /* pointer to Intuition library */
  48.  
  49. int return_code;
  50.  
  51. /* number of lines in the vsprite */
  52. #define GEL_SIZE    4
  53.  
  54. /* vsprite data - there are two sets that are alternated between.
  55. ** note that this data is always displayed as low resolution.
  56. */
  57. WORD chip vsprite_data1[2 * GEL_SIZE] =
  58.     { 0x7ffe, 0x80ff, 0x7c3e, 0x803f, 0x7c3e, 0x803f, 0x7ffe, 0x80ff, };
  59.  
  60. WORD chip vsprite_data2[2 * GEL_SIZE] =
  61.     { 0x7ffe, 0xff01, 0x7c3e, 0xfc01, 0x7c3e, 0xfc01, 0x7ffe, 0xff01, };
  62.  
  63. WORD mySpriteAltColors[] =
  64.     { 0x000f, 0x0f00, 0x0ff0 };
  65.  
  66. WORD mySpriteColors[] =
  67.     { 0x0000, 0x00f0, 0x0f00 };
  68.  
  69. /* information for the new vsprite */
  70. NEWVSPRITE myNewVSprite =
  71.     {
  72.     vsprite_data2,    /* image data                                */
  73.     mySpriteColors,    /* sprite color array                        */
  74.     1,                /* word width (must be 1 for true vsprite)    */
  75.     GEL_SIZE,        /* line height                                */
  76.     2,                /* image depth (must be 2 for true vsprite)    */
  77.     160, 100,        /* x, y position                            */
  78.     VSPRITE,        /* flags (VSPRITE == true vsprite)            */
  79.     1 << BORDERHIT,    /* hit mask                                    */
  80.     0,                /* me mask                                    */
  81.     };
  82.  
  83. /* information for the new window */
  84. struct NewWindow myNewWindow =
  85.     {
  86.     80, 20, 400, 150, -1, -1, CLOSEWINDOW | INTUITICKS,
  87.     ACTIVATE | WINDOWCLOSE | WINDOWDEPTH | RMBTRAP,
  88.     NULL, NULL, "VSprite", NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN
  89.     };
  90.  
  91. /*-------------------------------------------------------------
  92. **
  93. */
  94. VOID vspriteDrawGList(struct Window *win)
  95. {
  96. SortGList(win->RPort);
  97. DrawGList(win->RPort, ViewPortAddress(win));
  98. /* These calls are not intuition compatible...
  99. **    MrgCop(view);
  100. **    LoadView(view);
  101. ** use RethinkDisplay() in intuition environment.
  102. */
  103. RethinkDisplay();
  104. /* WaitTOF(); done by RethinkDisplay */
  105. }
  106.  
  107. /*-------------------------------------------------------------
  108. ** collision routine for vsprite hitting border.
  109. ** note that when the collision is vsprite to vsprite (or bob
  110. ** to bob, bob to animOb, etc), then the parameters are both
  111. ** pointers to a vsprite:
  112. **
  113. **  VOID collCheck(struct VSprite *vsp1,struct VSprite *vsp2)
  114. */
  115. VOID    borderCheck(struct VSprite *hitVSprite, LONG borderflags)
  116. {
  117. if (borderflags & RIGHTHIT)
  118.     {
  119.     hitVSprite->SprColors = mySpriteAltColors;
  120.     hitVSprite->VUserExt  = -40;
  121.     }
  122. if (borderflags & LEFTHIT)
  123.     {
  124.     hitVSprite->SprColors = mySpriteColors;
  125.     hitVSprite->VUserExt  = 20;
  126.     }
  127. }
  128.  
  129. /*-------------------------------------------------------------
  130. ** process window and dynamically change vsprite:
  131. ** - get messages.
  132. ** - go away on CLOSEWINDOW.
  133. ** - update and redisplay vsprite on INTUITICKS.
  134. ** - wait for more messages.
  135. */
  136. VOID process_window(struct Window *win, struct VSprite *myVSprite)
  137. {
  138. struct IntuiMessage *msg;
  139.  
  140. FOREVER
  141.     {
  142.     Wait(1L << win->UserPort->mp_SigBit);
  143.  
  144.     while (NULL != (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  145.         {
  146.         /* only CLOSEWINDOW and INTUITICKS are active */
  147.         if (msg->Class == CLOSEWINDOW)
  148.             {
  149.             ReplyMsg((struct Message *)msg);
  150.             return;
  151.             }
  152.  
  153.         /* must be an INTUITICKS:  change x and y values on the fly.
  154.         ** note offset by window left and top edge--sprite relative
  155.         ** to the screen, not window.
  156.         */
  157.         myVSprite->X = win->LeftEdge + msg->MouseX + myVSprite->VUserExt;
  158.         myVSprite->Y = win->TopEdge  + msg->MouseY + 1;
  159.         ReplyMsg((struct Message *)msg);
  160.         }
  161.  
  162.     /* got a message, change image data on the fly */
  163.     myVSprite->ImageData = (myVSprite->ImageData == vsprite_data1) ?
  164.         vsprite_data2 : vsprite_data1;
  165.  
  166.     SortGList(win->RPort);
  167.     DoCollision(win->RPort);
  168.     vspriteDrawGList(win);
  169.     }
  170. }
  171.  
  172. /*-------------------------------------------------------------
  173. ** working with the vsprite:
  174. ** - setup the gel system, and get a new vsprite (makeVSprite()).
  175. ** - add the vsprite to the system and display.
  176. ** - use the vsprite.
  177. ** - when done, remove vsprite and update the display without the vsprite.
  178. ** - cleanup everything.
  179. */
  180. VOID do_VSprite(struct Window *win)
  181. {
  182. struct VSprite       *myVSprite;
  183. struct GelsInfo       *my_ginfo;
  184.  
  185. if (NULL == (my_ginfo = setupGelSys(win->RPort, 0xfc)))
  186.     return_code = RETURN_WARN;
  187. else
  188.     {
  189.     if (NULL == (myVSprite = makeVSprite(&myNewVSprite)))
  190.         return_code = RETURN_WARN;
  191.     else
  192.         {
  193.         AddVSprite(myVSprite, win->RPort);
  194.         vspriteDrawGList(win);
  195.  
  196.         myVSprite->VUserExt = 20;
  197.         SetCollision(BORDERHIT, borderCheck, win->RPort->GelsInfo);
  198.  
  199.         process_window(win, myVSprite);
  200.  
  201.         RemVSprite(myVSprite);
  202.         freeVSprite(myVSprite);
  203.         }
  204.     vspriteDrawGList(win);
  205.     cleanupGelSys(my_ginfo,win->RPort);
  206.     }
  207. }
  208.  
  209. /*-------------------------------------------------------------
  210. ** example vsprite program:
  211. ** - first open up the libraries and a window.
  212. */
  213. VOID main(int argc, char **argv)
  214. {
  215. struct Window       *win;
  216.  
  217. return_code = RETURN_OK;
  218.  
  219. if (NULL == (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33L)))
  220.     return_code = RETURN_FAIL;
  221. else
  222.     {
  223.     if (NULL == (IntuitionBase =
  224.             (struct IntuitionBase *)OpenLibrary("intuition.library",33L)))
  225.         return_code = RETURN_FAIL;
  226.     else
  227.         {
  228.         if (NULL == (win = OpenWindow(&myNewWindow)))
  229.             return_code = RETURN_WARN;
  230.         else
  231.             {
  232.             do_VSprite(win);
  233.  
  234.             CloseWindow(win);
  235.             }
  236.         CloseLibrary(IntuitionBase);
  237.         }
  238.     CloseLibrary(GfxBase);
  239.     }
  240. exit(return_code);
  241. }
  242.