home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d457 / cmanual / acm3.lzh / VSprites / Example3.c < prev    next >
C/C++ Source or Header  |  1991-01-27  |  11KB  |  414 lines

  1. /* Example 3                                                      */
  2. /* This program demonstrates how to animate several (!) VSprites. */
  3.  
  4.  
  5.  
  6. /* Since we use Intuition, include this file: */
  7. #include <intuition/intuition.h>
  8.  
  9. /* Include this file since you are using sprites: */
  10. #include <graphics/gels.h>
  11.  
  12.  
  13.  
  14. /* We will use 32 VSprites: */
  15. #define MAXVSPRITES 32
  16.  
  17. /* They will move one pixel each time: */
  18. #define SPEED 1
  19.  
  20.  
  21.  
  22. /* Declare the functions we are going to use: */
  23. void main();
  24. void clean_up();
  25.  
  26.  
  27.  
  28. struct IntuitionBase *IntuitionBase = NULL;
  29. /* We need to open the Graphics library since we are using sprites: */
  30. struct GfxBase *GfxBase = NULL;
  31.  
  32.  
  33. /* Declare a pointer to a Screen structure: */ 
  34. struct Screen *my_screen;
  35.  
  36. /* Declare and initialize your NewScreen structure: */
  37. struct NewScreen my_new_screen=
  38. {
  39.   0,            /* LeftEdge  Should always be 0. */
  40.   0,            /* TopEdge   Top of the display.*/
  41.   320,          /* Width     We are using a low-resolution screen. */
  42.   200,          /* Height    Non-Interlaced NTSC (American) display. */
  43.   2,            /* Depth     4 colours. */
  44.   0,            /* DetailPen Text should be drawn with colour reg. 0 */
  45.   1,            /* BlockPen  Blocks should be drawn with colour reg. 1 */
  46.   SPRITES,      /* ViewModes No special modes. (Low-res, Non-Interlaced) */
  47.   CUSTOMSCREEN, /* Type      Your own customized screen. */
  48.   NULL,         /* Font      Default font. */
  49.   "MY SCREEN",  /* Title     The screen' title. */
  50.   NULL,         /* Gadget    Must for the moment be NULL. */
  51.   NULL          /* BitMap    No special CustomBitMap. */
  52. };
  53.  
  54.  
  55.  
  56. /* Declare a pointer to a Window structure: */ 
  57. struct Window *my_window = NULL;
  58.  
  59. /* Declare and initialize your NewWindow structure: */
  60. struct NewWindow my_new_window=
  61. {
  62.   0,             /* LeftEdge    x position of the window. */
  63.   0,             /* TopEdge     y positio of the window. */
  64.   320,           /* Width       320 pixels wide. */
  65.   200,           /* Height      200 lines high. */
  66.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  67.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  68.   CLOSEWINDOW,   /* IDCMPFlags  The window will give us a message if the */
  69.                  /*             user has selected the Close window gad. */
  70.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  71.   WINDOWCLOSE|   /*             Close Gadget. */
  72.   WINDOWDRAG|    /*             Drag gadget. */
  73.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  74.   WINDOWSIZING|  /*             Sizing Gadget. */
  75.   ACTIVATE,      /*             The window should be Active when opened. */
  76.   NULL,          /* FirstGadget No Custom gadgets. */
  77.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  78.   "VSprites with no limitations!", /* Title */
  79.   NULL,          /* Screen      Will later be connected to a custom scr. */
  80.   NULL,          /* BitMap      No Custom BitMap. */
  81.   80,            /* MinWidth    We will not allow the window to become */
  82.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  83.   320,           /* MaxWidth    than 320 x 200. */
  84.   200,           /* MaxHeight */
  85.   CUSTOMSCREEN   /* Type        Connected to the Workbench Screen. */
  86. };
  87.  
  88.  
  89.  
  90. /* 1. Declare and initialize some sprite data: */
  91. /* (6 frames, 4 different images: 1 2 3 4 3 2) */
  92. UWORD chip ship_data[6][28]=
  93. {
  94.   {
  95.     0xFFF8, 0x0000,
  96.     0x0200, 0x0000,
  97.     0x877C, 0x0000,
  98.     0x8786, 0x027C,
  99.     0xBFBF, 0x02C6,
  100.     0xEDFF, 0x1AC2,
  101.     0xA57D, 0x1AFE,
  102.     0xBF19, 0x02FE,
  103.     0x8F12, 0x00FC,
  104.     0x04FC, 0x0000,
  105.     0x0809, 0x0000,
  106.     0x3FFE, 0x0000,
  107.   },
  108.   {
  109.     0x7FF0, 0x0000,
  110.     0x0200, 0x0000,
  111.     0x077C, 0x0000,
  112.     0x8786, 0x027C,
  113.     0xBFBF, 0x02C6,
  114.     0xEDFF, 0x1AC2,
  115.     0xA57D, 0x1AFE,
  116.     0xBF19, 0x02FE,
  117.     0x0F12, 0x00FC,
  118.     0x04FC, 0x0000,
  119.     0x0809, 0x0000,
  120.     0x3FFE, 0x0000,
  121.   },
  122.   {
  123.     0x3FE0, 0x0000,
  124.     0x0200, 0x0000,
  125.     0x877C, 0x0000,
  126.     0x8786, 0x027C,
  127.     0xBFBF, 0x02C6,
  128.     0xEDFF, 0x1AC2,
  129.     0xA57D, 0x1AFE,
  130.     0xBF19, 0x02FE,
  131.     0x8F12, 0x00FC,
  132.     0x04FC, 0x0000,
  133.     0x0809, 0x0000,
  134.     0x3FFE, 0x0000,
  135.   },
  136.   {
  137.     0x1FC0, 0x0000,
  138.     0x0200, 0x0000,
  139.     0x077C, 0x0000,
  140.     0x8786, 0x027C,
  141.     0xBFBF, 0x02C6,
  142.     0xEDFF, 0x1AC2,
  143.     0xA57D, 0x1AFE,
  144.     0xBF19, 0x02FE,
  145.     0x0F12, 0x00FC,
  146.     0x04FC, 0x0000,
  147.     0x0809, 0x0000,
  148.     0x3FFE, 0x0000,
  149.   },
  150.   {
  151.     0x3FE0, 0x0000,
  152.     0x0200, 0x0000,
  153.     0x877C, 0x0000,
  154.     0x8786, 0x027C,
  155.     0xBFBF, 0x02C6,
  156.     0xEDFF, 0x1AC2,
  157.     0xA57D, 0x1AFE,
  158.     0xBF19, 0x02FE,
  159.     0x8F12, 0x00FC,
  160.     0x04FC, 0x0000,
  161.     0x0809, 0x0000,
  162.     0x3FFE, 0x0000,
  163.   },
  164.   {
  165.     0x7FF0, 0x0000,
  166.     0x0200, 0x0000,
  167.     0x077C, 0x0000,
  168.     0x8786, 0x027C,
  169.     0xBFBF, 0x02C6,
  170.     0xEDFF, 0x1AC2,
  171.     0xA57D, 0x1AFE,
  172.     0xBF19, 0x02FE,
  173.     0x0F12, 0x00FC,
  174.     0x04FC, 0x0000,
  175.     0x0809, 0x0000,
  176.     0x3FFE, 0x0000,
  177.   }
  178. };
  179.  
  180.  
  181.  
  182. /* 2. Declare VSprite structures: */
  183. struct VSprite head, tail, vsprite[ MAXVSPRITES ];
  184.  
  185.  
  186. /* 3. Declare the VSprites' colour tables:     */
  187. WORD colour_table[ MAXVSPRITES ][ 3 ];
  188.  
  189.  
  190. /* 4. Declare a GelsInfo structure: */
  191. struct GelsInfo ginfo;
  192.  
  193.  
  194. /* This boolean variable will tell us if the VSprites are */
  195. /* in the list or not:                                    */
  196. BOOL vsprite_on = FALSE;
  197.  
  198.  
  199. /* This program will not open any console window if run from */
  200. /* Workbench, but we must therefore not print anything.      */
  201. /* Functions like printf() must therefore not be used.       */
  202. void _main()
  203. {
  204.   /* The GelsInfo structure needs the following arrays: */
  205.   WORD nextline[ 8 ];
  206.   WORD *lastcolor[ 8 ];
  207.  
  208.   /* Direction of the sprite: */
  209.   WORD x_direction[ MAXVSPRITES ];
  210.  
  211.   /* Boolean variable used for the while loop: */
  212.   BOOL close_me = FALSE;
  213.  
  214.   /* Declare a pointer to an IntuiMessage structure: */
  215.   struct IntuiMessage *my_message;
  216.  
  217.   UBYTE loop;      /* Used as counter in the for loop: */
  218.   UBYTE image = 0; /* Which image is used, 1-6.        */
  219.   UBYTE x = 0;     /* X and Y position.                */
  220.   UBYTE y = 0;
  221.  
  222.  
  223.  
  224.   /* Open the Intuition Library: */
  225.   IntuitionBase = (struct IntuitionBase *)
  226.     OpenLibrary( "intuition.library", 0 );
  227.   
  228.   if( IntuitionBase == NULL )
  229.     clean_up(); /* Could NOT open the Intuition Library! */
  230.  
  231.  
  232.  
  233.   /* Since we are using sprites we need to open the Graphics Library: */
  234.   /* Open the Graphics Library: */
  235.   GfxBase = (struct GfxBase *)
  236.     OpenLibrary( "graphics.library", 0);
  237.  
  238.   if( GfxBase == NULL )
  239.     clean_up(); /* Could NOT open the Graphics Library! */
  240.  
  241.  
  242.  
  243.   /* We will now try to open the screen: */
  244.   my_screen = (struct Screen *) OpenScreen( &my_new_screen );
  245.  
  246.   /* Have we opened the screen succesfully? */
  247.   if(my_screen == NULL)
  248.     clean_up();
  249.  
  250.  
  251.   my_new_window.Screen = my_screen;
  252.  
  253.  
  254.   /* We will now try to open the window: */
  255.   my_window = (struct Window *) OpenWindow( &my_new_window );
  256.   
  257.   /* Have we opened the window succesfully? */
  258.   if(my_window == NULL)
  259.     clean_up(); /* Could NOT open the Window! */
  260.  
  261.  
  262.  
  263.  
  264.   /* 6. Initialize the GelsInfo structure: */
  265.  
  266.   /* All sprites may be used to draw the VSprites: */
  267.   /* ( 11111111 = 0xFF )                           */
  268.   ginfo.sprRsrvd = 0xFF;
  269.   /* If we do not exclude the first two sprites, the mouse */
  270.   /* pointer's colours may be affected.                    */
  271.  
  272.  
  273.   /* Give the GelsInfo structure some memory: */
  274.   ginfo.nextLine = nextline;
  275.   ginfo.lastColor = lastcolor;
  276.  
  277.  
  278.   /* Give the Rastport a pointer to the GelsInfo structure: */
  279.   my_window->RPort->GelsInfo = &ginfo;
  280.  
  281.   
  282.   /* Give the GelsInfo structure to the system: */
  283.   InitGels( &head, &tail, &ginfo );
  284.  
  285.  
  286.  
  287.   /* 7. Initialize the VSprite structures: */
  288.   for( loop = 0; loop < MAXVSPRITES; loop++ )
  289.   {
  290.     /* Set the VSprite's colours: */
  291.     colour_table[ loop ][ 0 ] = 0x0000; /* Black */
  292.     colour_table[ loop ][ 1 ] = 0x0080; /* Dark green */
  293.     colour_table[ loop ][ 2 ] = 0x00D0; /* Green */
  294.     
  295.     /* Set the speed and horizontal direction of the VSprite: */
  296.     x_direction[ loop ] = SPEED;
  297.  
  298.     vsprite[ loop ].Flags = VSPRITE; /* It is a VSprite.    */
  299.     vsprite[ loop ].X = 10 + 20 * x; /* X position.         */
  300.     vsprite[ loop ].Y = 30 + 20 * y; /* Y position.         */
  301.     vsprite[ loop ].Height = 12;     /* 16 lines tall.      */
  302.     vsprite[ loop ].Width = 2;       /* 2 words wide.       */
  303.     vsprite[ loop ].Depth = 2;       /* 2 bitpl, 4 colours. */
  304.  
  305.     /* Pointer to the sprite data: */
  306.     vsprite[ loop ].ImageData = ship_data[ image ];
  307.  
  308.     /* Pointer to the colour table: */
  309.     vsprite[ loop ].SprColors = colour_table[ loop ];
  310.  
  311.  
  312.     /* 8. Add the VSprites to the VSprite list: */
  313.     AddVSprite( &vsprite[ loop ], my_window->RPort );
  314.  
  315.  
  316.     /* Position of the VSprites: */
  317.     y++;
  318.     if( y > 7 )
  319.     {
  320.       y = 0;
  321.       x++;
  322.     }
  323.   }
  324.   
  325.   
  326.   /* The VSprites are in the list. */
  327.   vsprite_on = TRUE;
  328.  
  329.  
  330.   /* Stay in the while loop until the user has selected the Close window */
  331.   /* gadget: */
  332.   while( close_me == FALSE )
  333.   {
  334.     /* Stay in the while loop as long as we can collect messages: */
  335.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  336.     {
  337.       if( my_message->Class == CLOSEWINDOW)
  338.         close_me=TRUE;
  339.  
  340.       ReplyMsg( my_message );
  341.     }
  342.  
  343.  
  344.     /* Affect all VSprites: */
  345.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  346.     {
  347.       /* Change the x position of the VSprite: */
  348.       vsprite[ loop ].X += x_direction[ loop ];
  349.  
  350.  
  351.       /* Check that the sprite does not move outside the screen: */
  352.       if(vsprite[ loop ].X > 300)
  353.         x_direction[ loop ] = -SPEED;
  354.  
  355.       if(vsprite[ loop ].X < 0)
  356.         x_direction[ loop ] = SPEED;
  357.  
  358.  
  359.       /* Change the image of the VSprite: */
  360.       vsprite[ loop ].ImageData = ship_data[ image ];
  361.     }
  362.     
  363.  
  364.     /* Image counter: */
  365.     image++;
  366.     if( image > 5 )
  367.       image = 0;
  368.   
  369.  
  370.     /* 9. Sort the Gels list: */
  371.     SortGList( my_window->RPort );
  372.  
  373.     /* 10. Draw the Gels list: */
  374.     DrawGList( my_window->RPort, &(my_screen->ViewPort) );
  375.  
  376.     /* 11. Set the Copper and redraw the display: */
  377.     MakeScreen( my_screen );
  378.     RethinkDisplay();    
  379.   }
  380.  
  381.  
  382.  
  383.   /* Free all allocated memory: (Close the window, libraries etc) */
  384.   clean_up();
  385.  
  386.   /* THE END */
  387. }
  388.  
  389.  
  390.  
  391. /* This function frees all allocated memory. */
  392. void clean_up()
  393. {
  394.   UBYTE loop;
  395.   
  396.   if( vsprite_on )
  397.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  398.       RemVSprite( &vsprite[ loop ] );
  399.  
  400.   if( my_window )
  401.     CloseWindow( my_window );
  402.   
  403.   if(my_screen )
  404.     CloseScreen( my_screen );
  405.  
  406.   if( GfxBase )
  407.     CloseLibrary( GfxBase );
  408.  
  409.   if( IntuitionBase )
  410.     CloseLibrary( IntuitionBase );
  411.  
  412.   exit();
  413. }
  414.