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

  1. /* Example 5                                                           */
  2. /* This example demonstrates how to open a ViewPort in interlace mode. */
  3.  
  4.  
  5. #include <intuition/intuition.h>
  6. #include <graphics/gfxbase.h>
  7.  
  8.  
  9. #define WIDTH  640 /* 640 pixels wide (high resolution)                */
  10. #define HEIGHT 400 /* 400 lines high (interlaced NTSC display)         */ 
  11. #define DEPTH    3 /* 3 BitPlanes should be used, gives eight colours. */
  12. #define COLOURS  8 /* 2^3 = 8                                          */
  13.  
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16. struct GfxBase *GfxBase;
  17.  
  18.  
  19. struct View my_view;
  20. struct View *my_old_view;
  21. struct ViewPort my_view_port;
  22. struct RasInfo my_ras_info;
  23. struct BitMap my_bit_map;
  24. struct RastPort my_rast_port;
  25.  
  26.  
  27. UWORD my_color_table[] =
  28. {
  29.   0x000, /* Colour 0, Black       */
  30.   0x800, /* Colour 1, Red         */
  31.   0xF00, /* Colour 2, Light red   */
  32.   0x080, /* Colour 3, Green       */
  33.   0x0F0, /* Colour 4, Light green */
  34.   0x008, /* Colour 5, Blue        */
  35.   0x00F, /* Colour 6, Light Blue  */
  36.   0xFFF, /* Colour 7, White       */
  37. };
  38.  
  39.  
  40. void clean_up();
  41. void main();
  42.  
  43.  
  44. void main()
  45. {
  46.   UWORD *pointer;
  47.   int loop;
  48.   
  49.  
  50.   /* Open the Intuition library: */
  51.   IntuitionBase = (struct IntuitionBase *)
  52.     OpenLibrary( "intuition.library", 0 );
  53.   if( !IntuitionBase )
  54.     clean_up( "Could NOT open the Intuition library!" );
  55.  
  56.   /* Open the Graphics library: */
  57.   GfxBase = (struct GfxBase *)
  58.     OpenLibrary( "graphics.library", 0 );
  59.   if( !GfxBase )
  60.     clean_up( "Could NOT open the Graphics library!" );
  61.  
  62.  
  63.   /* Save the current View, so we can restore it later: */
  64.   my_old_view = GfxBase->ActiView;
  65.  
  66.  
  67.   /* 1. Prepare the View structure, and give it a pointer to */
  68.   /*    the first ViewPort:                                  */
  69.   InitView( &my_view );
  70.   my_view.ViewPort = &my_view_port;
  71.   /* The View should be interlaced: */
  72.   my_view.Modes = LACE;
  73.  
  74.  
  75.   /* 2. Prepare the ViewPort structure, and set some important values: */
  76.   InitVPort( &my_view_port );
  77.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  78.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  79.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  80.   my_view_port.Modes = HIRES|LACE;     /* High resolution interlace.    */
  81.  
  82.  
  83.   /* IMPORTANT! If you want a ViewPort to be interlaced you have to set */
  84.   /* the LACE flag in both the ViewPort as well as in the View          */
  85.   /* structure. If the ViewPort is interlaced but the View is non-      */
  86.   /* interlaced, only every second line in the ViewPort would be drawn. */
  87.   /* If the ViewPort is non-interlaced but the View is interlaced,      */
  88.   /* each line in the ViewPort would be drawn twice.                    */
  89.  
  90.  
  91.   /* 3. 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.   /* 4. 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.   /* 5. 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.   /* 6. Create the display: */
  128.   MakeVPort( &my_view, &my_view_port );
  129.   MrgCop( &my_view );
  130.  
  131.  
  132.   /* 7. 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.   /* 8. Show the new View: */
  138.   LoadView( &my_view );
  139.  
  140.  
  141.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  142.   SetDrMd( &my_rast_port, JAM1 );
  143.  
  144.   /* Draw 10000 lines in eight different colours, randomly. */ 
  145.   /* Position the pen: */
  146.   Move( &my_rast_port, rand() % WIDTH, rand() % HEIGHT );
  147.   for( loop = 0; loop < 10000; loop++ )
  148.   {
  149.     /* Set FgPen's colour (0-7): */
  150.     SetAPen( &my_rast_port, rand() % COLOURS );
  151.     /* Draw a line: */
  152.     Draw( &my_rast_port, rand() % WIDTH, rand() % HEIGHT );
  153.   }
  154.  
  155.  
  156.   /* 9. Restore the old View: */
  157.   LoadView( my_old_view );
  158.  
  159.  
  160.   /* Free all allocated resources and leave. */
  161.   clean_up( "THE END" );
  162. }
  163.  
  164.  
  165. /* Returns all allocated resources: */
  166. void clean_up( message )
  167. STRPTR message;
  168. {
  169.   int loop;
  170.  
  171.   /* Free automatically allocated display structures: */
  172.   FreeVPortCopLists( &my_view_port );
  173.   FreeCprList( my_view.LOFCprList );
  174.   FreeCprList( my_view.SHFCprList ); /* ! */
  175.  
  176.   /* An interlaced display use two copper lists (the normal LOF plus   */
  177.   /* the special SHF). When your program closes an interlaced ViewPort */
  178.   /* you must therefore deallocate both lists!                         */
  179.  
  180.  
  181.   /* Deallocate the display memory, BitPlane for BitPlane: */
  182.   for( loop = 0; loop < DEPTH; loop++ )
  183.     if( my_bit_map.Planes[ loop ] )
  184.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  185.  
  186.   /* Deallocate the ColorMap: */
  187.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  188.  
  189.   /* Close the Graphics library: */
  190.   if( GfxBase ) CloseLibrary( GfxBase );
  191.  
  192.   /* Close the Intuition library: */
  193.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  194.  
  195.   /* Print the message and leave: */
  196.   printf( "%s\n", message ); 
  197.   exit();
  198. }
  199.