home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Graphics Amiga C Club */
- /* Chapter: VSprites Tulevagen 22 */
- /* File: Example4.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-04-28 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This example demonstrates how to use a VSprite */
- /* on a display you have created yourself. We must */
- /* now use the low level functions MrgCop(), and */
- /* LoadView(), instead of the more sophisticated */
- /* functions MakeScreen(), RethinkDisplay(). */
-
-
- #include <intuition/intuition.h>
- #include <graphics/gfxbase.h>
- #include <graphics/gels.h>
-
-
- #define WIDTH 320 /* 320 pixels wide (low resolution) */
- #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display) */
- #define DEPTH 3 /* 3 BitPlanes should be used, gives eight colours. */
- #define COLOURS 8 /* 2^3 = 8 */
-
-
- struct IntuitionBase *IntuitionBase;
- struct GfxBase *GfxBase;
-
-
- struct View my_view;
- struct View *my_old_view;
- struct ViewPort my_view_port;
- struct RasInfo my_ras_info;
- struct BitMap my_bit_map;
- struct RastPort my_rast_port;
-
-
- UWORD my_color_table[] =
- {
- 0x000, /* Colour 0, Black */
- 0x800, /* Colour 1, Red */
- 0xF00, /* Colour 2, Light red */
- 0x080, /* Colour 3, Green */
- 0x0F0, /* Colour 4, Light green */
- 0x008, /* Colour 5, Blue */
- 0x00F, /* Colour 6, Light Blue */
- 0xFFF, /* Colour 7, White */
- };
-
-
- /* Declare and initialize some sprite data: */
- UWORD chip vsprite_data[]=
- {
- 0x0180, 0x0000,
- 0x03C0, 0x0000,
- 0x07E0, 0x0000,
- 0x0FF0, 0x0000,
- 0x1FF8, 0x0000,
- 0x3FFC, 0x0000,
- 0x7FFE, 0x0000,
- 0x0000, 0xFFFF,
- 0x0000, 0xFFFF,
- 0x7FFE, 0x7FFE,
- 0x3FFC, 0x3FFC,
- 0x1FF8, 0x1FF8,
- 0x0FF0, 0x0FF0,
- 0x07E0, 0x07E0,
- 0x03C0, 0x03C0,
- 0x0180, 0x0180,
- };
-
-
-
- /* Declare three VSprite structures. One will be used, */
- /* the other two are "dummies": */
- struct VSprite head, tail, vsprite;
-
-
- /* Decide the VSprite's colours: */
- /* RGB RGB RGB */
- WORD colour_table[] = { 0x000F, 0x00F0, 0x0F00 };
-
-
- /* Declare a GelsInfo structure: */
- struct GelsInfo ginfo;
-
-
- /* This boolean variable will tell us if the VSprite is in */
- /* the list or not: */
- BOOL vsprite_on = FALSE;
-
-
- void clean_up();
- void main();
-
- void main()
- {
- UWORD *pointer;
- int loop;
-
- /* The GelsInfo structure needs the following arrays: */
- WORD nextline[ 8 ];
- WORD *lastcolor[ 8 ];
-
- /* Sprite position: */
- WORD x = 1;
- WORD y = 2;
-
- /* Direction of the sprite: */
- WORD x_direction = 1;
- WORD y_direction = 1;
-
-
- /* Open the Intuition library: */
- IntuitionBase = (struct IntuitionBase *)
- OpenLibrary( "intuition.library", 0 );
- if( !IntuitionBase )
- clean_up( "Could NOT open the Intuition library!" );
-
- /* Open the Graphics library: */
- GfxBase = (struct GfxBase *)
- OpenLibrary( "graphics.library", 0 );
- if( !GfxBase )
- clean_up( "Could NOT open the Graphics library!" );
-
-
- /* Save the current View, so we can restore it later: */
- my_old_view = GfxBase->ActiView;
-
-
- /* Prepare the View structure, and give it a pointer to */
- /* the first ViewPort: */
- InitView( &my_view );
- my_view.ViewPort = &my_view_port;
-
-
- /* Prepare the ViewPort structure, and set some important values: */
- InitVPort( &my_view_port );
- my_view_port.DWidth = WIDTH; /* Set the width. */
- my_view_port.DHeight = HEIGHT; /* Set the height. */
- my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
- my_view_port.Modes = SPRITES; /* Using sprites. */
-
-
- /* Get a colour map, link it to the ViewPort, and prepare it: */
- my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
- if( my_view_port.ColorMap == NULL )
- clean_up( "Could NOT get a ColorMap!" );
-
- /* Get a pointer to the colour map: */
- pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
-
- /* Set the colours: */
- for( loop = 0; loop < COLOURS; loop++ )
- *pointer++ = my_color_table[ loop ];
-
-
- /* Prepare the BitMap: */
- InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
-
- /* Allocate memory for the Raster: */
- for( loop = 0; loop < DEPTH; loop++ )
- {
- my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
- if( my_bit_map.Planes[ loop ] == NULL )
- clean_up( "Could NOT allocate enough memory for the raster!" );
-
- /* Clear the display memory with help of the Blitter: */
- BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
- }
-
-
- /* Prepare the RasInfo structure: */
- my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure. */
- my_ras_info.RxOffset = 0; /* The top left corner of the Raster */
- my_ras_info.RyOffset = 0; /* should be at the top left corner */
- /* of the display. */
- my_ras_info.Next = NULL; /* Single playfield - only one */
- /* RasInfo structure is necessary. */
-
- /* Create the display: */
- MakeVPort( &my_view, &my_view_port );
- MrgCop( &my_view );
-
-
- /* Prepare the RastPort, and give it a pointer to the BitMap. */
- InitRastPort( &my_rast_port );
- my_rast_port.BitMap = &my_bit_map;
-
-
- /* Show the new View: */
- LoadView( &my_view );
-
-
- /* Initialize the GelsInfo structure: */
-
- /* All sprites except the first two may be used to draw */
- /* the VSprites: ( 11111100 = 0xFC ) */
- ginfo.sprRsrvd = 0xFC;
- /* If we do not exclude the first two sprites, the mouse */
- /* pointer's colours may be affected. */
-
-
- /* Give the GelsInfo structure some memory: */
- ginfo.nextLine = nextline;
- ginfo.lastColor = lastcolor;
-
-
- /* Give the Rastport a pointer to the GelsInfo structure: */
- my_rast_port.GelsInfo = &ginfo;
-
-
- /* Give the GelsInfo structure to the system: */
- InitGels( &head, &tail, &ginfo );
-
-
- /* Initialize the VSprite structure: */
-
- vsprite.Flags = VSPRITE; /* It is a VSprite. */
- vsprite.X = x; /* X position. */
- vsprite.Y = y; /* Y position. */
- vsprite.Height = 16; /* 16 lines tall. */
- vsprite.Width = 2; /* Two bytes (16 pixels) wide. */
- vsprite.Depth = 2; /* Two bitplanes, 4 colours. */
-
-
- /* Pointer to the sprite data: */
- vsprite.ImageData = vsprite_data;
-
- /* Pointer to the colour table: */
- vsprite.SprColors = colour_table;
-
-
-
- /* Add the VSprites to the VSprite list: */
- AddVSprite( &vsprite, &my_rast_port );
-
- /* The VSprite is in the list. */
- vsprite_on = TRUE;
-
-
- for( loop = 0; loop < 1000; loop++ )
- {
- /* Change the x/y position: */
- x += x_direction;
- y += y_direction;
-
- /* Check that the sprite does not move outside the screen: */
- if(x > 300)
- {
- x_direction = -1;
- x = 300;
- }
- if(x < 1)
- {
- x_direction = 1;
- x = 1;
- }
- if(y > 180)
- {
- y_direction = -1;
- y = 180;
- }
- if(y < 10)
- {
- y_direction = 1;
- y = 10;
- }
-
- vsprite.X = x;
- vsprite.Y = y;
-
- /* Sort the Gels list: */
- SortGList( &my_rast_port );
-
- /* Draw the Gels list: */
- DrawGList( &my_rast_port, &my_view_port );
-
- /* Adjust the copper list: */
- MrgCop( &my_view );
-
- /* Show the new View: */
- LoadView( &my_view );
-
- /* Move it smoothly: */
- WaitTOF();
- }
-
-
- /* Restore the old View: */
- LoadView( my_old_view );
-
-
- /* Free all allocated resources and leave. */
- clean_up( "THE END" );
- }
-
-
- /* Returns all allocated resources: */
- void clean_up( message )
- STRPTR message;
- {
- int loop;
-
- /* Remove the VSprites: */
- if( vsprite_on )
- RemVSprite( &vsprite );
-
- /* Free automatically allocated display structures: */
- FreeVPortCopLists( &my_view_port );
- FreeCprList( my_view.LOFCprList );
-
- /* Deallocate the display memory, BitPlane for BitPlane: */
- for( loop = 0; loop < DEPTH; loop++ )
- if( my_bit_map.Planes[ loop ] )
- FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
-
- /* Deallocate the ColorMap: */
- if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
-
- /* Close the Graphics library: */
- if( GfxBase ) CloseLibrary( GfxBase );
-
- /* Close the Intuition library: */
- if( IntuitionBase ) CloseLibrary( IntuitionBase );
-
- /* Print the message and leave: */
- printf( "%s\n", message );
- exit();
- }
-