home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Graphics / Sprites / Example3.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  12KB  |  382 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:    Example3.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 set up a 15 coloured sprite, and how to */
  21. /* move it around.                                                   */
  22.  
  23.  
  24.  
  25. #include <intuition/intuition.h>
  26. /* Include this file since you are using sprites: */
  27. #include <graphics/sprite.h>
  28.  
  29.  
  30.  
  31. /* Declare the functions we are going to use: */
  32. void main();
  33. void free_memory();
  34.  
  35.  
  36.  
  37. struct IntuitionBase *IntuitionBase = NULL;
  38. /* We need to open the Graphics library since we are using sprites: */
  39. struct GfxBase *GfxBase = NULL;
  40.  
  41.  
  42.  
  43. /* Declare a pointer to a Window structure: */ 
  44. struct Window *my_window = NULL;
  45.  
  46. /* Declare and initialize your NewWindow structure: */
  47. struct NewWindow my_new_window=
  48. {
  49.   50,            /* LeftEdge    x position of the window. */
  50.   25,            /* TopEdge     y positio of the window. */
  51.   320,           /* Width       320 pixels wide. */
  52.   100,           /* Height      100 lines high. */
  53.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  54.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  55.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  56.   RAWKEY,        /*             user has selected the Close window gad, */
  57.                  /*             or if the user has pressed a key. */
  58.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  59.   WINDOWCLOSE|   /*             Close Gadget. */
  60.   WINDOWDRAG|    /*             Drag gadget. */
  61.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  62.   WINDOWSIZING|  /*             Sizing Gadget. */
  63.   ACTIVATE,      /*             The window should be Active when opened. */
  64.   NULL,          /* FirstGadget No Custom gadgets. */
  65.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  66.   "15-COLOURED SPRITE",/* Title Title of the window. */
  67.   NULL,          /* Screen      Connected to the Workbench Screen. */
  68.   NULL,          /* BitMap      No Custom BitMap. */
  69.   80,            /* MinWidth    We will not allow the window to become */
  70.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  71.   300,           /* MaxWidth    than 300 x 200. */
  72.   200,           /* MaxHeight */
  73.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  74. };
  75.  
  76.  
  77.  
  78. /********************************************************/
  79. /* 1. Declare and initialize some sprite graphics data: */
  80. /********************************************************/
  81.  
  82. /* Sprite Data for the Bottom Sprite: */
  83. UWORD chip bottom_sprite_data[36]=
  84. {
  85.   0x0000, 0x0000,
  86.  
  87.   /* Bitplane */
  88.   /* ZERO ONE */
  89.   
  90.   0x0000, 0x0000,
  91.   0xFFFF, 0x0000,
  92.   0x0000, 0xFFFF,
  93.   0xFFFF, 0xFFFF,
  94.  
  95.   0x0000, 0x0000,
  96.   0xFFFF, 0x0000,
  97.   0x0000, 0xFFFF,
  98.   0xFFFF, 0xFFFF,
  99.  
  100.   0x0000, 0x0000,
  101.   0xFFFF, 0x0000,
  102.   0x0000, 0xFFFF,
  103.   0xFFFF, 0xFFFF,
  104.  
  105.   0x0000, 0x0000,
  106.   0xFFFF, 0x0000,
  107.   0x0000, 0xFFFF,
  108.   0xFFFF, 0xFFFF,
  109.     
  110.   0x0000, 0x0000
  111. };
  112.  
  113. /* Sprite Data for the Top Sprite: */
  114. UWORD chip top_sprite_data[36]=
  115. {
  116.   0x0000, SPRITE_ATTACHED, /* We attach the Top Sprite to the Bottom */
  117.                            /* Sprite.                                */
  118.  
  119.   /*  Bitplane  */
  120.   /* TWO  THREE */
  121.   0x0000, 0x0000,
  122.   0x0000, 0x0000,
  123.   0x0000, 0x0000,
  124.   0x0000, 0x0000,
  125.  
  126.   0xFFFF, 0x0000,
  127.   0xFFFF, 0x0000,
  128.   0xFFFF, 0x0000,
  129.   0xFFFF, 0x0000,
  130.  
  131.   0x0000, 0xFFFF,
  132.   0x0000, 0xFFFF,
  133.   0x0000, 0xFFFF,
  134.   0x0000, 0xFFFF,
  135.  
  136.   0xFFFF, 0xFFFF,
  137.   0xFFFF, 0xFFFF,
  138.   0xFFFF, 0xFFFF,
  139.   0xFFFF, 0xFFFF,
  140.     
  141.   0x0000, 0x0000
  142. };
  143.  
  144.  
  145.  
  146. /*********************************************************/
  147. /* 2. Declare and initialize two SimpleSprite structure: */
  148. /*********************************************************/
  149.  
  150. /* Bottom sprite: */
  151. struct SimpleSprite bottom_sprite=
  152. {
  153.   bottom_sprite_data, /* posctldata, pointer to the sprite data. */
  154.   16,                 /* height, 16 lines tall. */
  155.   40, 80,             /* x, y, position on the screen. */
  156.   -1,                 /* num, this field is automatically initialized */
  157.                       /* when you call the GetSprite() function, so   */
  158.                       /* we set it to -1 for the moment.              */
  159. };
  160.  
  161. /* Top sprite: */
  162. struct SimpleSprite top_sprite=
  163. {
  164.   top_sprite_data, /* posctldata, pointer to the sprite data. */
  165.   16,              /* height, 16 lines tall. */
  166.   40, 80,          /* x, y, position on the screen. */
  167.   -1,              /* num, this field is automatically initialized */
  168.                    /* when you call the GetSprite() function, so   */
  169.                    /* we set it to -1 for the moment.              */
  170. };
  171.  
  172.  
  173.  
  174. void main()
  175. {
  176.   /* Sprite position: (We use only one pair of coordinates since the */
  177.   /* two sprites will be attached to each other.)                    */
  178.   WORD x = bottom_sprite.x;
  179.   WORD y = bottom_sprite.y;
  180.  
  181.   /* Direction of the sprite: */
  182.   WORD x_direction = 0;
  183.   WORD y_direction = 0;
  184.  
  185.   /* Boolean variable used for the while loop: */
  186.   BOOL close_me = FALSE;
  187.  
  188.   ULONG class; /* IDCMP */
  189.   USHORT code; /* Code */
  190.  
  191.   /* Declare a pointer to an IntuiMessage structure: */
  192.   struct IntuiMessage *my_message;
  193.  
  194.  
  195.  
  196.   /* Open the Intuition Library: */
  197.   IntuitionBase = (struct IntuitionBase *)
  198.     OpenLibrary( "intuition.library", 0 );
  199.   
  200.   if( IntuitionBase == NULL )
  201.     free_memory("Could NOT open the Intuition Library!");
  202.  
  203.  
  204.  
  205.   /* Since we are using sprites we need to open the Graphics Library: */
  206.   /* Open the Graphics Library: */
  207.   GfxBase = (struct GfxBase *)
  208.     OpenLibrary( "graphics.library", 0);
  209.  
  210.   if( GfxBase == NULL )
  211.     free_memory("Could NOT open the Graphics Library!");
  212.  
  213.  
  214.  
  215.   /* We will now try to open the window: */
  216.   my_window = (struct Window *) OpenWindow( &my_new_window );
  217.   
  218.   /* Have we opened the window succesfully? */
  219.   if(my_window == NULL)
  220.     free_memory("Could NOT open the Window!");
  221.  
  222.  
  223.  
  224.   /* Change the colour register 17 - 31: */
  225.   /* NOTE! Since we change colour register 17, 18 and 19 we will */
  226.   /* change the colour of Intuition's Pointer (Sprite 0). We do  */
  227.   /* not bother about that in this Example but you should be     */
  228.   /* careful with these three colour registers.                  */
  229.   SetRGB4( &my_window->WScreen->ViewPort, 17, 0x0, 0xF, 0x0 );
  230.   SetRGB4( &my_window->WScreen->ViewPort, 18, 0x0, 0xD, 0x0 );
  231.   SetRGB4( &my_window->WScreen->ViewPort, 19, 0x0, 0xB, 0x0 );
  232.   SetRGB4( &my_window->WScreen->ViewPort, 20, 0x0, 0x9, 0x0 );
  233.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0x0, 0x7, 0x1 );
  234.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0x0, 0x5, 0x3 );
  235.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0x3, 0x5 );
  236.   SetRGB4( &my_window->WScreen->ViewPort, 24, 0x1, 0x1, 0x7 );
  237.   SetRGB4( &my_window->WScreen->ViewPort, 25, 0x3, 0x0, 0x5 );
  238.   SetRGB4( &my_window->WScreen->ViewPort, 26, 0x5, 0x0, 0x3 );
  239.   SetRGB4( &my_window->WScreen->ViewPort, 27, 0x7, 0x0, 0x1 );
  240.   SetRGB4( &my_window->WScreen->ViewPort, 28, 0x9, 0x0, 0x0 );
  241.   SetRGB4( &my_window->WScreen->ViewPort, 29, 0xB, 0x0, 0x0 );
  242.   SetRGB4( &my_window->WScreen->ViewPort, 30, 0xD, 0x0, 0x0 );
  243.   SetRGB4( &my_window->WScreen->ViewPort, 31, 0xF, 0x0, 0x0 );
  244.  
  245.  
  246.  
  247.   /* Reserve sprite 2 as Bottom Sprite: */
  248.   if( GetSprite( &bottom_spri