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

  1. /* The following example demonstrates how to set up, move and free a
  2. ** Simple Sprite.  The animtools.h file included is listed at the end of
  3. ** the chapter.
  4. **
  5. ** ssprite.c - Simple Sprite example
  6. **
  7. ** SAS/C V5.10a
  8. ** lc -b1 -cfist -v -y ssprite.c
  9. ** blink FROM LIB:c.o ssprite.o LIB LIB:lc.lib LIB:amiga.lib TO ssprite
  10. */
  11. #include <exec/types.h>
  12. #include <graphics/gfx.h>
  13. #include <graphics/gfxbase.h>
  14. #include <graphics/gfxmacros.h>
  15. #include <graphics/sprite.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <intuition/screens.h>
  18. #include <hardware/custom.h>
  19. #include <hardware/dmabits.h>
  20. #include <libraries/dos.h>
  21.  
  22. #include <clib/graphics_protos.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/alib_stdio_protos.h>
  26.  
  27. #include <stdlib.h>
  28.  
  29. struct GfxBase *GfxBase = NULL;
  30. struct IntuitionBase *IntuitionBase = NULL;
  31. extern struct Custom far custom ;
  32.  
  33. /* real boring sprite data */
  34. UWORD chip sprite_data[ ] = {
  35.     0, 0,           /* position control           */
  36.     0xffff, 0x0000, /* image data line 1, color 1 */
  37.     0xffff, 0x0000, /* image data line 2, color 1 */
  38.     0x0000, 0xffff, /* image data line 3, color 2 */
  39.     0x0000, 0xffff, /* image data line 4, color 2 */
  40.     0x0000, 0x0000, /* image data line 5, transparent */
  41.     0x0000, 0xffff, /* image data line 6, color 2 */
  42.     0x0000, 0xffff, /* image data line 7, color 2 */
  43.     0xffff, 0xffff, /* image data line 8, color 3 */
  44.     0xffff, 0xffff, /* image data line 9, color 3 */
  45.     0, 0            /* reserved, must init to 0 0 */
  46.     };
  47.  
  48. VOID main(int argc, char **argv)
  49. {
  50. struct SimpleSprite    sprite = {0};
  51. struct ViewPort        *viewport;
  52.  
  53. WORD sprite_num;
  54. SHORT delta_move, ktr1, ktr2, color_reg;
  55. struct Screen *screen;
  56. int return_code;
  57.  
  58. return_code = RETURN_OK;
  59.  
  60. if (NULL == (GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",37L)))
  61.     return_code = RETURN_FAIL;
  62. else
  63.     {
  64.     if (NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37L)))
  65.         return_code = RETURN_FAIL;
  66.     else
  67.         {
  68.         /* opened library, need a viewport to render a sprite over. */
  69.         if (NULL == (screen = OpenScreenTagList(NULL, NULL)))
  70.             return_code = RETURN_FAIL;
  71.         else
  72.             {
  73.             viewport = &screen->ViewPort;
  74.             if (-1 == (sprite_num = GetSprite(&sprite, 2)))
  75.                 return_code = RETURN_WARN;
  76.             else
  77.                 {
  78.                 /* Calculate the correct base color register number, */
  79.                 /* set up the color registers.                       */
  80.                 color_reg = 16 + ((sprite_num & 0x06) << 1);
  81.                 printf("color_reg=%d\n", color_reg);
  82.                 SetRGB4(viewport, color_reg + 1, 12,  3,  8);
  83.                 SetRGB4(viewport, color_reg + 2, 13, 13, 13);
  84.                 SetRGB4(viewport, color_reg + 3,  4,  4, 15);
  85.  
  86.                 sprite.x = 0;       /* initialize position and size info    */
  87.                 sprite.y = 0;       /* to match that shown in sprite_data   */
  88.                 sprite.height = 9;  /* so system knows layout of data later */
  89.  
  90.                 /* install sprite data and move sprite to start position. */
  91.                 ChangeSprite(NULL, &sprite, (APTR)sprite_data);
  92.                 MoveSprite(NULL, &sprite, 30, 0);
  93.  
  94.                 /* move the sprite back and forth. */
  95.                 for ( ktr1 = 0, delta_move = 1;
  96.                       ktr1 < 6; ktr1++, delta_move = -delta_move)
  97.                     {
  98.                     for ( ktr2 = 0; ktr2 < 100; ktr2++)
  99.                         {
  100.                         MoveSprite( NULL, &sprite, (LONG)(sprite.x + delta_move),
  101.                                     (LONG)(sprite.y + delta_move) );
  102.                         WaitTOF(); /* one move per video frame */
  103.  
  104.                         /* Show the effect of turning off sprite DMA. */
  105.                         if (ktr2 == 40) OFF_SPRITE ;
  106.                         if (ktr2 == 60) ON_SPRITE ;
  107.                         }
  108.                     }
  109.                 /* NOTE:  if you turn off the sprite at the wrong time (when it
  110.                 ** is being displayed), the sprite will appear as a vertical bar
  111.                 ** on the screen.  To really get rid of the sprite, you must
  112.                 ** OFF_SPRITE while it is not displayed.  This is hard in a
  113.                 ** multi-tasking system (the solution is not addressed in
  114.                 ** this program).
  115.                 */
  116.                 ON_SPRITE ; /* just to be sure */
  117.                 FreeSprite((WORD)sprite_num);
  118.                 }
  119.             (VOID) CloseScreen(screen);
  120.             }
  121.         CloseLibrary((struct Library *)IntuitionBase);
  122.         }
  123.     CloseLibrary((struct Library *)GfxBase);
  124.     }
  125. exit(return_code);
  126. }
  127.