home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / docs / cmanual.lzh / ACM3.lzh / LowLevelGraphics / Example8.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  7KB  |  251 lines

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