home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / acm3.lzh / LowLevelGraphics / Example10.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  7KB  |  247 lines

  1. /* Example 10                                                    */
  2. /* This example demonstrates how to use the Area Fill functions. */
  3. /* [ AreaMove(), AreaDraw() and AreaEnd(). ]                     */
  4.  
  5.  
  6. #include <intuition/intuition.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfxmacros.h>
  9.  
  10.  
  11. #define WIDTH  320 /* 320 pixels wide (low resolution)                */
  12. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)    */ 
  13. #define DEPTH    2 /* 2 BitPlanes should be used, gives four colours. */
  14. #define COLOURS  4 /* 2^2 = 4                                         */
  15.  
  16.  
  17. #define MAX_VERTICES 100 /* 100 vertices, 5 bytes each = 500 bytes. */ 
  18. #define BUFFERT_SIZE 250 /* 500 bytes = 250 words.                  */
  19.  
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22. struct GfxBase *GfxBase;
  23.  
  24.  
  25. struct View my_view;
  26. struct View *my_old_view;
  27. struct ViewPort my_view_port;
  28. struct RasInfo my_ras_info;
  29. struct BitMap my_bit_map;
  30.  
  31. struct RastPort my_rast_port;
  32. struct TmpRas  my_temp_ras;
  33. struct AreaInfo my_area_info;
  34.  
  35.  
  36. UWORD my_color_table[] =
  37. {
  38.   0x000, /* Colour 0, Black */
  39.   0xF00, /* Colour 1, Red   */
  40.   0x0F0, /* Colour 2, Green */
  41.   0x00F  /* Colour 3, Blue  */
  42. };
  43.  
  44.  
  45. /* The buffert must start on a word boundary: */
  46. UWORD buffert[ BUFFERT_SIZE ];
  47. PLANEPTR extra_space;
  48.  
  49.  
  50. void clean_up();
  51. void main();
  52.  
  53.  
  54. void main()
  55. {
  56.   UWORD *pointer;
  57.   int loop;
  58.   
  59.  
  60.   /* Open the Intuition library: */
  61.   IntuitionBase = (struct IntuitionBase *)
  62.     OpenLibrary( "intuition.library", 0 );
  63.   if( !IntuitionBase )
  64.     clean_up( "Could NOT open the Intuition library!" );
  65.  
  66.   /* Open the Graphics library: */
  67.   GfxBase = (struct GfxBase *)
  68.     OpenLibrary( "graphics.library", 0 );
  69.   if( !GfxBase )
  70.     clean_up( "Could NOT open the Graphics library!" );
  71.  
  72.  
  73.   /* Save the current View, so we can restore it later: */
  74.   my_old_view = GfxBase->ActiView;
  75.  
  76.  
  77.   /* Prepare the View structure, and give it a pointer to */
  78.   /* the first ViewPort:                                  */
  79.   InitView( &my_view );
  80.   my_view.ViewPort = &my_view_port;
  81.  
  82.  
  83.   /* Prepare the ViewPort structure, and set some important values:     */
  84.   InitVPort( &my_view_port );
  85.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  86.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  87.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  88.   my_view_port.Modes = NULL;           /* Low resolution.               */
  89.  
  90.  
  91.   /* Get a colour map, link it to the ViewPort, and prepare it: */
  92.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  93.   if( my_view_port.ColorMap == NULL )
  94.     clean_up( "Could NOT get a ColorMap!" );
  95.  
  96.   /* Get a pointer to the colour map: */
  97.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  98.  
  99.   /* Set the colours: */
  100.   for( loop = 0; loop < COLOURS; loop++ )
  101.     *pointer++ = my_color_table[ loop ];
  102.  
  103.  
  104.   /* Prepare the BitMap: */
  105.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  106.  
  107.   /* Allocate memory for the Raster: */ 
  108.   for( loop = 0; loop < DEPTH; loop++ )
  109.   {
  110.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  111.     if( my_bit_map.Planes[ loop ] == NULL )
  112.       clean_up( "Could NOT allocate enough memory for the raster!" );
  113.  
  114.     /* Clear the display memory with help of the Blitter: */
  115.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  116.   }
  117.  
  118.   
  119.   /* Prepare the RasInfo structure: */
  120.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  121.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  122.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  123.                                     /* of the display.                   */
  124.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  125.                                     /* RasInfo structure is necessary.   */
  126.  
  127.   /* Create the display: */
  128.   MakeVPort( &my_view, &my_view_port );
  129.   MrgCop( &my_view );
  130.  
  131.  
  132.   /* Prepare the RastPort, and give it a pointer to the BitMap. */
  133.   InitRastPort( &my_rast_port );
  134.   my_rast_port.BitMap = &my_bit_map;
  135.  
  136.  
  137.  
  138.   /* 1. Get some space for the vertices and initialize the AreaInfo ptr: */
  139.   InitArea( &my_area_info, buffert, MAX_VERTICES );
  140.   my_rast_port.AreaInfo = &my_area_info;
  141.  
  142.  
  143.   /* 2. Allocate some space that is needed to build up the objects: */
  144.   extra_space = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  145.   if( extra_space == NULL )
  146.     clean_up( "Could NOT allocate enough memory for the temp raster!" );
  147.  
  148.  
  149.   /* 3. Initialize the TmpRas structure: */
  150.   my_rast_port.TmpRas = (struct TmpRas *)
  151.     InitTmpRas( &my_temp_ras, extra_space, RASSIZE( WIDTH, HEIGHT ) );
  152.  
  153.  
  154.  
  155.   /* Show the new View: */
  156.   LoadView( &my_view );
  157.  
  158.  
  159.  
  160.   SetAPen( &my_rast_port, 1 ); /* Red   */
  161.   SetBPen( &my_rast_port, 0 ); /* Black */
  162.   SetOPen( &my_rast_port, 2 ); /* Green */
  163.   SetDrMd( &my_rast_port, JAM1 );
  164.  
  165.  
  166.  
  167.   /* New position: */
  168.   AreaMove( &my_rast_port,  10,  10 );
  169.  
  170.   /* Add the vertices: */
  171.   AreaDraw( &my_rast_port, 310,  10 );
  172.   AreaDraw( &my_rast_port, 310, 100 );
  173.   AreaDraw( &my_rast_port, 290, 100 );
  174.   AreaDraw( &my_rast_port, 290,  30 );
  175.   AreaDraw( &my_rast_port,  30,  30 );
  176.   AreaDraw( &my_rast_port,  30, 100 );
  177.   AreaDraw( &my_rast_port,  10, 100 );
  178.  
  179.   /* End this object. The last line will be set automatically in order */
  180.   /* to close the object, and the figure will be filled. The Outline   */
  181.   /* pen (green) will be used to draw a line around the whole object.  */
  182.   AreaEnd( &my_rast_port );
  183.  
  184.  
  185.   /* Turn off the outline function: */
  186.   BNDRYOFF( &my_rast_port );
  187.  
  188.  
  189.   /* New position: (This figure will not be outlined.) */
  190.   AreaMove( &my_rast_port,  10,  190 );
  191.  
  192.   /* Add the vertices: */
  193.   AreaDraw( &my_rast_port,  10, 150);
  194.   AreaDraw( &my_rast_port, 310, 190);
  195.   AreaDraw( &my_rast_port, 310, 150);
  196.  
  197.   /* End this object: */
  198.   AreaEnd( &my_rast_port );
  199.  
  200.  
  201.  
  202.   /* Wait 10 seconds: */
  203.   Delay( 50 * 10 );
  204.  
  205.  
  206.   /* Restore the old View: */
  207.   LoadView( my_old_view );
  208.  
  209.  
  210.   /* Free all allocated resources and leave. */
  211.   clean_up( "THE END" );
  212. }
  213.  
  214.  
  215. /* Returns all allocated resources: */
  216. void clean_up( message )
  217. STRPTR message;
  218. {
  219.   int loop;
  220.  
  221.   /* Deallocate memory used for the objects: */
  222.   if( extra_space )
  223.     FreeRaster( extra_space, WIDTH, HEIGHT );
  224.  
  225.   /* Free automatically allocated display structures: */
  226.   FreeVPortCopLists( &my_view_port );
  227.   FreeCprList( my_view.LOFCprList );
  228.   
  229.   /* Deallocate the display memory, BitPlane for BitPlane: */
  230.   for( loop = 0; loop < DEPTH; loop++ )
  231.     if( my_bit_map.Planes[ loop ] )
  232.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  233.  
  234.   /* Deallocate the ColorMap: */
  235.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  236.  
  237.   /* Close the Graphics library: */
  238.   if( GfxBase ) CloseLibrary( GfxBase );
  239.  
  240.   /* Close the Intuition library: */
  241.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  242.  
  243.   /* Print the message and leave: */
  244.   printf( "%s\n", message ); 
  245.   exit();
  246. }
  247.