home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Graphics / Sprites / Example2.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  12KB  |  411 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: Sprites                     Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program shows how to declare and initialize some sprite data   */
  21. /* and a SimpleSprite structure. It also shows how to reserve a sprite */
  22. /* (sprite 2), and how to move it around. The user moves the sprite by */
  23. /* pressing the arrow keys. In this example we animate the sprite (6   */
  24. /* frames taken from Miniblast).                                       */
  25.  
  26.  
  27.  
  28. #include <intuition/intuition.h>
  29. /* Include this file since you are using sprites: */
  30. #include <graphics/sprite.h>
  31.  
  32.  
  33.  
  34. /* Declare the functions we are going to use: */
  35. void main();
  36. void free_memory();
  37.  
  38.  
  39.  
  40. struct IntuitionBase *IntuitionBase = NULL;
  41. /* We need to open the Graphics library since we are using sprites: */
  42. struct GfxBase *GfxBase = NULL;
  43.  
  44.  
  45.  
  46. /* Declare a pointer to a Window structure: */ 
  47. struct Window *my_window = NULL;
  48.  
  49. /* Declare and initialize your NewWindow structure: */
  50. struct NewWindow my_new_window=
  51. {
  52.   50,            /* LeftEdge    x position of the window. */
  53.   25,            /* TopEdge     y positio of the window. */
  54.   320,           /* Width       320 pixels wide. */
  55.   100,           /* Height      100 lines high. */
  56.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  57.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  58.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  59.   RAWKEY,        /*             user has selected the Close window gad, */
  60.                  /*             or if the user has pressed a key. */
  61.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  62.   WINDOWCLOSE|   /*             Close Gadget. */
  63.   WINDOWDRAG|    /*             Drag gadget. */
  64.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  65.   WINDOWSIZING|  /*             Sizing Gadget. */
  66.   ACTIVATE,      /*             The window should be Active when opened. */
  67.   NULL,          /* FirstGadget No Custom gadgets. */
  68.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  69.   "MICROBLAST",  /* Title       Title of the window. */
  70.   NULL,          /* Screen      Connected to the Workbench Screen. */
  71.   NULL,          /* BitMap      No Custom BitMap. */
  72.   80,            /* MinWidth    We will not allow the window to become */
  73.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  74.   300,           /* MaxWidth    than 300 x 200. */
  75.   200,           /* MaxHeight */
  76.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  77. };
  78.  
  79.  
  80.  
  81. /*********************************************************************/
  82. /* Extra information:                                                */
  83. /* When we declare the window pointer, the intuition library pointer */
  84. /* etc, we initialize them to point to NULL:                         */
  85. /* struct Window *my_window = NULL;                                  */
  86. /* Since we then know that all of the pointers will point to NULL    */
  87. /* when we start, we can check if they still point to NULL when we   */
  88. /* quit. If they do not point to NULL anymore, we close that window, */
  89. /* library etc.                                                      */
  90. /*********************************************************************/
  91.  
  92.  
  93.  
  94. /********************************************************/
  95. /* 1. Declare and initialize some sprite graphics data: */
  96. /********************************************************/
  97. /* Sprite data for a ship: */
  98. /* (6 frames, 4 different images: 1 2 3 4 3 2) */
  99. UWORD chip ship_data[6][28]=
  100. {
  101.   {
  102.       0x0000, 0x0000, /* Ship 1 */
  103.  
  104.       0xFFF8, 0x0000,
  105.       0x0200, 0x0000,
  106.       0x877C, 0x0000,
  107.       0x8786, 0x027C,
  108.       0xBFBF, 0x02C6,
  109.       0xEDFF, 0x1AC2,
  110.       0xA57D, 0x1AFE,
  111.       0xBF19, 0x02FE,
  112.       0x8F12, 0x00FC,
  113.       0x04FC, 0x0000,
  114.       0x0809, 0x0000,
  115.       0x3FFE, 0x0000,
  116.  
  117.       0x0000, 0x0000,
  118.   },
  119.   {
  120.       0x0000, 0x0000, /* Ship 2 */
  121.  
  122.       0x7FF0, 0x0000,
  123.       0x0200, 0x0000,
  124.       0x077C, 0x0000,
  125.       0x8786, 0x027C,
  126.       0xBFBF, 0x02C6,
  127.       0xEDFF, 0x1AC2,
  128.       0xA57D, 0x1AFE,
  129.       0xBF19, 0x02FE,
  130.       0x0F12, 0x00FC,
  131.       0x04FC, 0x0000,
  132.       0x0809, 0x0000,
  133.       0x3FFE, 0x0000,
  134.  
  135.       0x0000, 0x0000,
  136.   },
  137.   {
  138.       0x0000, 0x0000, /* Ship 3 */
  139.  
  140.       0x3FE0, 0x0000,
  141.       0x0200, 0x0000,
  142.       0x877C, 0x0000,
  143.       0x8786, 0x027C,
  144.       0xBFBF, 0x02C6,
  145.       0xEDFF, 0x1AC2,
  146.       0xA57D, 0x1AFE,
  147.       0xBF19, 0x02FE,
  148.       0x8F12, 0x00FC,
  149.       0x04FC, 0x0000,
  150.       0x0809, 0x0000,
  151.       0x3FFE, 0x0000,
  152.  
  153.       0x0000, 0x0000,
  154.   },
  155.   {
  156.       0x0000, 0x0000, /* Ship 4 */
  157.  
  158.       0x1FC0, 0x0000,
  159.       0x0200, 0x0000,
  160.       0x077C, 0x0000,
  161.       0x8786, 0x027C,
  162.       0xBFBF, 0x02C6,
  163.       0xEDFF, 0x1AC2,
  164.       0xA57D, 0x1AFE,
  165.       0xBF19, 0x02FE,
  166.       0x0F12, 0x00FC,
  167.       0x04FC, 0x0000,
  168.       0x0809, 0x0000,
  169.       0x3FFE, 0x0000,
  170.  
  171.       0x0000, 0x0000,
  172.   },
  173.   {
  174.       0x0000, 0x0000, /* Ship 5 (3) */
  175.  
  176.       0x3FE0, 0x0000,
  177.       0x0200, 0x0000,
  178.       0x877C, 0x0000,
  179.       0x8786, 0x027C,
  180.       0xBFBF, 0x02C6,
  181.       0xEDFF, 0x1AC2,
  182.       0xA57D, 0x1AFE,
  183.       0xBF19, 0x02FE,
  184.       0x8F12, 0x00FC,
  185.       0x04FC, 0x0000,
  186.       0x0809, 0x0000,
  187.       0x3FFE, 0x0000,
  188.  
  189.       0x0000, 0x0000,
  190.   },
  191.   {
  192.       0x0000, 0x0000, /* Ship 6 (2) */
  193.  
  194.       0x7FF0, 0x0000,
  195.       0x0200, 0x0000,
  196.       0x077C, 0x0000,
  197.       0x8786, 0x027C,
  198.       0xBFBF, 0x02C6,
  199.       0xEDFF, 0x1AC2,
  200.       0xA57D, 0x1AFE,
  201.       0xBF19, 0x02FE,
  202.       0x0F12, 0x00FC,
  203.       0x04FC, 0x0000,
  204.       0x0809, 0x0000,
  205.       0x3FFE, 0x0000,
  206.  
  207.       0x0000, 0x0000,
  208.   }
  209. };
  210.  
  211.  
  212.  
  213. /*******************************************************/
  214. /* 2. Declare and initialize a SimpleSprite structure: */
  215. /*******************************************************/
  216. struct SimpleSprite my_sprite=
  217. {
  218.   ship_data[0],   /* posctldata, pointer to the sprite data. (Frame 0) */
  219.   12,             /* height, 12 lines tall. */
  220.   40, 80,         /* x, y, position on the screen. */
  221.   -1,             /* num, this field is automatically initialized when  */
  222.                   /* you call the GetSprite() function, so we set it to */
  223.                   /* -1 for the moment.                                 */
  224. };
  225.  
  226.  
  227.  
  228. void main()
  229. {
  230.   /* Sprite position: */
  231.   WORD x = my_sprite.x;
  232.   WORD y = my_sprite.y;
  233.  
  234.   /* Direction of the sprite: */
  235.   WORD x_direction = 0;
  236.   WORD y_direction = 0;
  237.  
  238.   UWORD frame = 0; /* Frame 0 */
  239.  
  240.   /* Boolean variable used for the while loop: */
  241.   BOOL close_me = FALSE;
  242.  
  243.   ULONG class; /* IDCMP */
  244.   USHORT code; /* Code */
  245.  
  246.   /* Declare a pointer to an IntuiMessage structure: */
  247.   struct IntuiMessage *my_message;
  248.  
  249.  
  250.  
  251.   /* Open the Intuition Library: */
  252.   IntuitionBase = (struct IntuitionBase *)
  253.     OpenLibrary( "intuition.library", 0 );
  254.   
  255.   if( IntuitionBase == NULL )
  256.     free_memory(); /* Could NOT open the Intuition Library! */
  257.  
  258.  
  259.  
  260.   /* Since we are using sprites we need to open the Graphics Library: */
  261.   /* Open the Graphics Library: */
  262.   GfxBase = (struct GfxBase *)
  263.     OpenLibrary( "graphics.library", 0);
  264.  
  265.   if( GfxBase == NULL )
  266.     free_memory(); /* Could NOT open the Graphics Library! */
  267.  
  268.  
  269.  
  270.   /* We will now try to open the window: */
  271.   my_window = (struct Window *) OpenWindow( &my_new_window );
  272.   
  273.   /* Have we opened the window succesfully? */
  274.   if(my_window == NULL)
  275.     free_memory(); /* Could NOT open the Window! */
  276.  
  277.  
  278.  
  279.   /* Change the colour register 21 - 23: */
  280.   SetRGB4( &my_window->WScreen->ViewPort, 21,