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

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