home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / hacks / copper.c < prev    next >
C/C++ Source or Header  |  1991-01-13  |  7KB  |  241 lines

  1. /* Copper.c                                               */
  2. /* This program demonstrates how to play with the Copper. */
  3.  
  4.  
  5. #include <intuition/intuition.h> /* Intuition Library           */
  6. #include <graphics/gfxbase.h>    /* Graphics Library [ActiView] */
  7. #include <exec/memory.h>         /* AllocMem() [MEMF_PUBLIC]    */
  8. #include <graphics/view.h>       /* struct ViewPort             */
  9. #include <graphics/gfxmacros.h>  /* CINIT(), CMOVE(), etc       */
  10. #include <hardware/custom.h>     /* struct Custom               */
  11.  
  12.  
  13. #define WIDTH  640 /* 640 pixels wide (high resolution)              */
  14. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)   */ 
  15. #define DEPTH    1 /* 1 BitPlanes should be used, gives two colours. */
  16. #define COLOURS  2 /* 2^1 = 2                                        */
  17.  
  18.  
  19. struct IntuitionBase *IntuitionBase;
  20. struct GfxBase *GfxBase;
  21.  
  22.  
  23. /* 1. Declare a Custom structure. This structure will automatically */
  24. /*    be initialized!                                               */
  25. extern struct Custom far custom;
  26.  
  27.  
  28. struct View my_view;
  29. struct View *my_old_view;
  30. struct ViewPort my_view_port;
  31. struct RasInfo my_ras_info;
  32. struct BitMap my_bit_map;
  33. struct RastPort my_rast_port;
  34.  
  35.  
  36. UWORD my_color_table[] =
  37. {
  38.   0x000, /* Colour 0, Black */
  39.   0xFFF  /* Colour 1, White */
  40. };
  41.  
  42.  
  43. void clean_up();
  44. void _main();
  45.  
  46.  
  47. /* NOTE! Since we have declared our main() function as _main(), */
  48. /* no Consol window will be opened if it is run from the        */
  49. /* Workbench. The disadvantage is that we must NEVER use the    */
  50. /* printf() or similar console functions. It would crash the    */
  51. /* system.                                                      */
  52. void _main()
  53. {
  54.   /* 2. Declare a pointer to a use copper list structure: */
  55.   struct UCopList *copper;
  56.   /* Colour value: */
  57.   int colour = 0;
  58.  
  59.   UWORD *pointer;
  60.   int loop;
  61.   
  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.   /* 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.   /* 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 = HIRES;          /* High resolution.              */
  93.  
  94.  
  95.   /* 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.   /* 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.   /* 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.  
  132.  
  133.   /* 3. Allocate memory for a UCopList structure:            */
  134.     /*    [Must be Chip memory!]                               */
  135.   /*    The Amiga will automatically deallocate this memory  */
  136.     /*    when the program terminates, so we should not do it. */
  137.   copper = (struct UCopList *)
  138.       AllocMem( sizeof(struct UCopList), MEMF_PUBLIC|MEMF_CHIP|MEMF_CLEAR );
  139.  
  140.  
  141.   /* 4. Initialize the copper list: */
  142.     CINIT( copper, 1 );
  143.  
  144.  
  145.   /* 5. Modify the copper list:                                  */
  146.     /*    [0 - 15 - 0 - 15 and so on, change blue, green and red.] */
  147.   for( loop=0; loop < HEIGHT; loop++ )
  148.     {
  149.         (loop / 15) % 2 ? colour-- : colour++ ;
  150.  
  151.     /* Blue:  (Bits 0 - 3)  */
  152.     CWAIT( copper, loop, 0 );
  153.       CMOVE( copper, custom.color[0], colour );
  154.  
  155.     /* Green: (Bits 4 - 7)  */
  156.     CWAIT( copper, loop, 110 );
  157.       CMOVE( copper, custom.color[0], colour<<4 );
  158.  
  159.     /* Red:   (Bits 8 - 11) */
  160.     CWAIT( copper, loop, 170 );
  161.       CMOVE( copper, custom.color[0], colour<<8 );
  162.     }
  163.  
  164.   /* The last lines should be black: */
  165.   CWAIT( copper, loop, 0 );
  166.   CMOVE( copper, custom.color[0], 0 );
  167.  
  168.  
  169.   /* 6. Tell the Copper that no more instructions will be given: */
  170.   CEND( copper );
  171.  
  172.  
  173.   /* 7. Link our own copperlist to our ViewPort: */
  174.   my_view_port.UCopIns = copper;
  175.  
  176.  
  177.   /* 8. Create the display: */
  178.   MakeVPort( &my_view, &my_view_port );
  179.   MrgCop( &my_view );
  180.  
  181.  
  182.  
  183.   /* Prepare the RastPort, and give it a pointer to the BitMap. */
  184.   InitRastPort( &my_rast_port );
  185.   my_rast_port.BitMap = &my_bit_map;
  186.   
  187.  
  188.   /* Show the new View: */
  189.   LoadView( &my_view );
  190.  
  191.  
  192.  
  193.   /* Wait 10 seconds: */
  194.   Delay( 10 * 50 );
  195.  
  196.  
  197.  
  198.   /* Restore the old View: */
  199.   LoadView( my_old_view );
  200.  
  201.  
  202.   /* Free all allocated resources and leave. */
  203.   clean_up( "THE END" );
  204. }
  205.  
  206.  
  207. /* Returns all allocated resources: */
  208. void clean_up( message )
  209. STRPTR message;
  210. {
  211.   int loop;
  212.  
  213.  
  214.   /* Free automatically allocated display structures: */
  215.   FreeVPortCopLists( &my_view_port );
  216.   FreeCprList( my_view.LOFCprList );
  217.   
  218.   /* Deallocate the display memory, BitPlane for BitPlane: */
  219.   for( loop = 0; loop < DEPTH; loop++ )
  220.     if( my_bit_map.Planes[ loop ] )
  221.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  222.  
  223.   /* Deallocate the ColorMap: */
  224.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  225.  
  226.   /* Close the Graphics library: */
  227.   if( GfxBase ) CloseLibrary( GfxBase );
  228.  
  229.   /* Close the Intuition library: */
  230.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  231.  
  232.   /* Print the message and leave:   */
  233.     /* NO! As I said above, we must   */
  234.     /* not use any console functions! */
  235.     /* If you want to use printf(),   */
  236.     /* declare the main function as   */
  237.     /* main() and not as _main().     */
  238.   /* printf( "%s\n", message );     */
  239.   exit();
  240. }
  241.