home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / RGBBoxes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  9.1 KB  |  301 lines

  1. ;/*  RGBBoxes.c simple ViewPort example -- works with 1.3 and Release 2
  2. LC -b1 -cfistq -v -y -j73 RGBBoxes.c
  3. Blink FROM LIB:c.o,RGBBoxes.o TO RGBBoxes LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. **
  6. ** The following example creates a View consisting of one ViewPort set
  7. ** to an NTSC, high-resolution, interlaced display mode of nominal
  8. ** dimensions.  This example shows both the old 1.3 way of setting up
  9. ** the ViewPort and the new method used in Release 2.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <graphics/gfx.h>
  14. #include <graphics/gfxbase.h>
  15. #include <graphics/gfxmacros.h>
  16. #include <graphics/copper.h>
  17. #include <graphics/view.h>
  18. #include <graphics/displayinfo.h>
  19. #include <graphics/gfxnodes.h>
  20. #include <graphics/videocontrol.h>
  21. #include <libraries/dos.h>
  22. #include <utility/tagitem.h>
  23.  
  24. #include <clib/graphics_protos.h>
  25. #include <clib/exec_protos.h>
  26. #include <clib/dos_protos.h>
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31. #define DEPTH 2     /*  The number of bitplanes.  */
  32. #define WIDTH 640   /*  Nominal width and height  */
  33. #define HEIGHT 400  /*  used in 1.3.              */
  34.  
  35. #ifdef LATTICE
  36. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  37. int chkabort(void) { return(0); }  /* really */
  38. #endif
  39.  
  40. VOID drawFilledBox(WORD , WORD );  /* Function prototypes */
  41. VOID cleanup(int );
  42. VOID fail(STRPTR);
  43.  
  44. struct GfxBase *GfxBase = NULL;
  45.  
  46. /*  Construct a simple display.  These are global to make freeing easier.   */
  47. struct View view, *oldview=NULL;  /*  Pointer to old View we can restore it.*/
  48. struct ViewPort viewPort = { 0 };
  49. struct BitMap bitMap = { 0 };
  50. struct ColorMap *cm=NULL;
  51.  
  52. struct ViewExtra *vextra=NULL;      /* Extended structures used in Release 2 */
  53. struct MonitorSpec *monspec=NULL;
  54. struct ViewPortExtra *vpextra=NULL;
  55. struct DimensionInfo dimquery = { 0 };
  56.  
  57. UBYTE *displaymem = NULL;     /*  Pointer for writing to BitMap memory.  */
  58.  
  59. #define BLACK 0x000           /*  RGB values for the four colors used.   */
  60. #define RED   0xf00
  61. #define GREEN 0x0f0
  62. #define BLUE  0x00f
  63.  
  64. /*
  65.  * main():  create a custom display; works under either 1.3 or Release 2
  66.  */
  67. VOID main(VOID)
  68. {
  69. WORD depth, box;
  70. struct RasInfo rasInfo;
  71. ULONG modeID;
  72.  
  73. struct TagItem vcTags[] =
  74. {
  75.     {VTAG_ATTACH_CM_SET, NULL },
  76.     {VTAG_VIEWPORTEXTRA_SET, NULL },
  77.     {VTAG_NORMAL_DISP_SET, NULL },
  78.     {VTAG_END_CM, NULL }
  79. };
  80.  
  81. /*  Offsets in BitMap where boxes will be drawn.  */
  82. static SHORT boxoffsets[] = { 802, 2010, 3218 };
  83.  
  84. static UWORD colortable[] = { BLACK, RED, GREEN, BLUE };
  85.  
  86. /* Open the graphics library */
  87. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L);
  88. if(GfxBase == NULL)
  89.     fail("Could not open graphics library\n");
  90.  
  91. /*  Example steals screen from Intuition if Intuition is around.      */
  92. oldview = GfxBase->ActiView;   /* Save current View to restore later. */
  93.  
  94. InitView(&view);           /*  Initialize the View and set View.Modes.     */
  95. view.Modes |= LACE;        /*  This is the old 1.3 way (only LACE counts). */
  96.  
  97. if(GfxBase->LibNode.lib_Version >= 36)
  98.     {
  99.     /* Form the ModeID from values in <displayinfo.h> */
  100.     modeID=DEFAULT_MONITOR_ID | HIRESLACE_KEY;
  101.  
  102.     /*  Make the ViewExtra structure   */
  103.     if( vextra=GfxNew(VIEW_EXTRA_TYPE) )
  104.         {
  105.         /* Attach the ViewExtra to the View */
  106.         GfxAssociate(&view , vextra);
  107.         view.Modes |= EXTEND_VSTRUCT;
  108.  
  109.         /* Create and attach a MonitorSpec to the ViewExtra */
  110.         if( monspec=OpenMonitor(NULL,modeID) )
  111.             vextra->Monitor=monspec;
  112.         else
  113.             fail("Could not get MonitorSpec\n");
  114.         }
  115.      else fail("Could not get ViewExtra\n");
  116.      }
  117.  
  118.  
  119. /*  Initialize the BitMap for RasInfo.  */
  120. InitBitMap(&bitMap, DEPTH, WIDTH, HEIGHT);
  121.  
  122. /* Set the plane pointers to NULL so the cleanup routine */
  123. /* will know if they were used.                          */
  124. for(depth=0; depth<DEPTH; depth++)
  125.     bitMap.Planes[depth] = NULL;
  126.  
  127. /*  Allocate space for BitMap.             */
  128. for (depth=0; depth<DEPTH; depth++)
  129.     {
  130.     bitMap.Planes[depth] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  131.     if (bitMap.Planes[depth] == NULL)
  132.         fail("Could not get BitPlanes\n");
  133.     }
  134.  
  135. rasInfo.BitMap = &bitMap;       /*  Initialize the RasInfo.  */
  136. rasInfo.RxOffset = 0;
  137. rasInfo.RyOffset = 0;
  138. rasInfo.Next = NULL;
  139.  
  140. InitVPort(&viewPort);           /*  Initialize the ViewPort.  */
  141. view.ViewPort = &viewPort;      /*  Link the ViewPort into the View.  */
  142. viewPort.RasInfo = &rasInfo;
  143. viewPort.DWidth = WIDTH;
  144. viewPort.DHeight = HEIGHT;
  145.  
  146. /* Set the display mode the old-fashioned way */
  147. viewPort.Modes=HIRES | LACE;
  148.  
  149. if(GfxBase->LibNode.lib_Version >= 36)
  150. {
  151.     /* Make a ViewPortExtra and get ready to attach it */
  152.     if( vpextra = GfxNew(VIEWPORT_EXTRA_TYPE) )
  153.         {
  154.         vcTags[1].ti_Data = (ULONG) vpextra;
  155.  
  156.         /* Initialize the DisplayClip field of the ViewPortExtra */
  157.         if( GetDisplayInfoData( NULL , (UBYTE *) &dimquery ,
  158.                                 sizeof(dimquery) , DTAG_DIMS, modeID) )
  159.             {
  160.             vpextra->DisplayClip = dimquery.Nominal;
  161.  
  162.             /* Make a DisplayInfo and get ready to attach it */
  163.             if( !(vcTags[2].ti_Data = (ULONG) FindDisplayInfo(modeID)) )
  164.                 fail("Could not get DisplayInfo\n");
  165.              }
  166.         else fail("Could not get DimensionInfo \n");
  167.         }
  168.     else fail("Could not get ViewPortExtra\n");
  169.  
  170.     /* This is for backwards compatibility with, for example,   */
  171.     /* a 1.3 screen saver utility that looks at the Modes field */
  172.     viewPort.Modes = (UWORD) (modeID & 0x0000ffff);
  173.     }
  174.  
  175. /*  Initialize the ColorMap.  */
  176. /*  2 planes deep, so 4 entries (2 raised to the #_planes power).  */
  177. cm = GetColorMap(4L);
  178. if(cm == NULL)
  179.     fail("Could not get ColorMap\n");
  180.  
  181. if(GfxBase->LibNode.lib_Version >= 36)
  182.     {
  183.     /* Get ready to attach the ColorMap, Release 2-style */
  184.     vcTags[0].ti_Data = (ULONG) &viewPort;
  185.  
  186.     /* Attach the color map and Release 2 extended structures */
  187.     if( VideoControl(cm,vcTags) )
  188.         fail("Could not attach extended structures\n");
  189.     }
  190. else
  191.     /* Attach the ColorMap, old 1.3-style */
  192.     viewPort.ColorMap = cm;
  193.  
  194. LoadRGB4(&viewPort, colortable, 4);  /* Change colors to those in colortable. */
  195.  
  196. MakeVPort( &view, &viewPort ); /* Construct preliminary Copper instruction list.    */
  197.  
  198. /* Merge preliminary lists into a real Copper list in the View structure. */
  199. MrgCop( &view );
  200.  
  201. /* Clear the ViewPort */
  202. for(depth=0; depth<DEPTH; depth++)
  203.     {
  204.     displaymem = (UBYTE *)bitMap.Planes[depth];
  205.     BltClear(displaymem, (bitMap.BytesPerRow * bitMap.Rows), 1L);
  206.     }
  207.  
  208. LoadView(&view);
  209.  
  210. /*  Now fill some boxes so that user can see something.          */
  211. /*  Always draw into both planes to assure true colors.          */
  212. for (box=1; box<=3; box++)  /* Three boxes; red, green and blue. */
  213.     {
  214.     for (depth=0; depth<DEPTH; depth++)        /*  Two planes.   */
  215.         {
  216.         displaymem = bitMap.Planes[depth] + boxoffsets[box-1];
  217.         drawFilledBox(box, depth);
  218.         }
  219.     }
  220.  
  221. Delay(10L * TICKS_PER_SECOND);   /*  Pause for 10 seconds.                */
  222. LoadView(oldview);               /*  Put back the old View.               */
  223. WaitTOF();                       /*  Wait until the the View is being     */
  224.                                  /*    rendered to free memory.           */
  225. FreeCprList(view.LOFCprList);    /*  Deallocate the hardware Copper list  */
  226. if(view.SHFCprList)              /*    created by MrgCop().  Since this   */
  227.     FreeCprList(view.SHFCprList);/*    is interlace, also check for a     */
  228.                                  /*    short frame copper list to free.   */
  229. FreeVPortCopLists(&viewPort);    /*  Free all intermediate Copper lists   */
  230.                                  /*    from created by MakeVPort().       */
  231. cleanup(RETURN_OK);              /*  Success.                             */
  232. }
  233.  
  234.  
  235. /*
  236.  * fail():  print the error string and call cleanup() to exit
  237.  */
  238. void fail(STRPTR errorstring)
  239. {
  240. printf(errorstring);
  241. cleanup(RETURN_FAIL);
  242. }
  243.  
  244. /*
  245.  * cleanup():  free everything that was allocated.
  246.  */
  247. VOID cleanup(int returncode)
  248. {
  249. WORD depth;
  250.  
  251. /*  Free the color map created by GetColorMap().  */
  252. if(cm) FreeColorMap(cm);
  253.  
  254. /* Free the ViewPortExtra created by GfxNew() */
  255. if(vpextra) GfxFree(vpextra);
  256.  
  257. /*  Free the BitPlanes drawing area.  */
  258. for(depth=0; depth<DEPTH; depth++)
  259.     {
  260.     if (bitMap.Planes[depth])
  261.         FreeRaster(bitMap.Planes[depth], WIDTH, HEIGHT);
  262.     }
  263.  
  264. /* Free the MonitorSpec created with OpenMonitor() */
  265. if(monspec) CloseMonitor( monspec );
  266.  
  267. /* Free the ViewExtra created with GfxNew() */
  268. if(vextra) GfxFree(vextra);
  269.  
  270. /* Close the graphics library */
  271. CloseLibrary((struct Library *)GfxBase);
  272.  
  273. exit(returncode);
  274. }
  275.  
  276.  
  277. /*
  278.  * drawFilledBox(): create a WIDTH/2 by HEIGHT/2 box of color
  279.  *                  "fillcolor" into the given plane.
  280.  */
  281. VOID drawFilledBox(WORD fillcolor, WORD plane)
  282. {
  283. UBYTE value;
  284. WORD boxHeight, boxWidth, width;
  285.  
  286. /*  Divide (WIDTH/2) by eight because each UBYTE that */
  287. /* is written stuffs eight bits into the BitMap.      */
  288. boxWidth = (WIDTH/2)/8;
  289. boxHeight = HEIGHT/2;
  290.  
  291. value = ((fillcolor & (1 << plane)) != 0) ?  0xff : 0x00;
  292.  
  293. for( ; boxHeight; boxHeight--)
  294.     {
  295.     for(width=0 ; width < boxWidth; width++)
  296.         *displaymem++ = value;
  297.  
  298.     displaymem += (bitMap.BytesPerRow - boxWidth);
  299.     }
  300. }
  301.