home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / graphics / primitives / RGBBoxes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  6.0 KB  |  226 lines

  1. /*  RGBBoxes
  2.     A self-contained example of a single playfield display.
  3.     For Lattice, compile and link with:  LC -b1 -cfist -L -v -y RGBBoxes.c
  4.  
  5.    Copyright (c) 1990 Commodore-Amiga, Inc.
  6.   
  7.    This example is provided in electronic form by Commodore-Amiga, Inc. for
  8.    use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  9.    The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  10.    information on the correct usage of the techniques and operating system
  11.    functions presented in this example.  The source and executable code of
  12.    this example may only be distributed in free electronic form, via bulletin
  13.    board or as part of a fully non-commercial and freely redistributable
  14.    diskette.  Both the source and executable code (including comments) must
  15.    be included, without modification, in any copy.  This example may not be
  16.    published in printed form or distributed with any commercial product.
  17.    However, the programming techniques and support routines set forth in
  18.    this example may be used in the development of original executable
  19.    software products for Commodore Amiga computers.
  20.    All other rights reserved.
  21.    This example is provided "as-is" and is subject to change; no warranties
  22.    are made.  All use is at your own risk.  No liability or responsibility
  23.    is assumed.
  24. */
  25.  
  26. #include <exec/types.h>
  27. #include <graphics/gfx.h>
  28. #include <graphics/gfxbase.h>
  29. #include <graphics/gfxmacros.h>
  30. #include <graphics/copper.h>
  31. #include <graphics/view.h>
  32. #include <libraries/dos.h>
  33.  
  34. #include <proto/all.h>
  35. #include <stdlib.h>
  36.  
  37. #define DEPTH 2    /*  The number of bitplanes.  */
  38. #define WIDTH 320
  39. #define HEIGHT 200
  40.  
  41. struct GfxBase *GfxBase = NULL;
  42.  
  43. /*  Construct a simple display.  */
  44. struct View view;
  45. struct ViewPort viewPort;
  46. struct BitMap bitMap;
  47.  
  48. /*  Pointer for writing to BitMap memory.  */
  49. UBYTE *displaymem = NULL;
  50.  
  51. /*  Pointer to old View so it can be restored.  */
  52. struct View *oldview = NULL;
  53.  
  54. /*  RGB values for the four colors used.  */
  55. #define BLACK 0x000
  56. #define RED   0xf00
  57. #define GREEN 0x0f0
  58. #define BLUE  0x00f
  59.  
  60.  
  61. /*
  62.    Draw a WIDTH/2 by HEIGHT/2 box of color "fillcolor" into the given plane.
  63. */
  64. VOID drawFilledBox(WORD fillcolor, WORD plane)
  65. {
  66. UBYTE value;
  67. WORD boxHeight, boxWidth, width;
  68.  
  69. /*
  70.    Divide (WIDTH/2) by eight because each UBYTE that
  71.    is written stuffs eight bits into the BitMap.
  72. */
  73. boxWidth = (WIDTH/2)/8;
  74. boxHeight = HEIGHT/2;
  75.  
  76. value = ((fillcolor & (1 << plane)) != 0) ?  0xff : 0x00;
  77.  
  78. for( ; boxHeight; boxHeight--)
  79.     {
  80.     for(width=0 ; width < boxWidth; width++)
  81.         *displaymem++ = value;
  82.  
  83.     displaymem += (bitMap.BytesPerRow - boxWidth);
  84.     }
  85. }
  86.  
  87.  
  88. /*
  89.    Return user- and system-allocated memory to system memory manager.
  90. */
  91. VOID freeMemory(VOID)
  92. {
  93. WORD depth;
  94.  
  95. /*  Free the drawing area.  */
  96. for(depth=0; depth<DEPTH; depth++)
  97.     {
  98.     if (bitMap.Planes[depth])
  99.         FreeRaster(bitMap.Planes[depth], WIDTH, HEIGHT);
  100.     }
  101.  
  102. /*  Free the color map created by GetColorMap().  */
  103. if (viewPort.ColorMap)
  104.     FreeColorMap(viewPort.ColorMap);
  105.  
  106. /*  Free all intermediate Copper lists from ViewPort.  */
  107. FreeVPortCopLists(&viewPort);
  108.  
  109. /*  Deallocate the hardware Copper list.  */
  110. if (view.LOFCprList)
  111.     FreeCprList(view.LOFCprList);
  112. }
  113.  
  114.  
  115. /*
  116.    Clean up and exit.
  117. */
  118. VOID cleanExit(int exitStatus)
  119. {
  120. if (oldview)
  121.     {
  122.     LoadView(oldview);    /*  Put back the old View.  */
  123.     /*  Wait until the the view is being rendered to free our memory.  */
  124.     WaitTOF();
  125.     }
  126.  
  127. freeMemory();    /*  Give back what was borrowed.  */
  128.  
  129. if (GfxBase)
  130.     CloseLibrary((struct Library *)GfxBase);
  131.  
  132. exit(exitStatus);
  133. }
  134.  
  135.  
  136. VOID main(VOID)
  137. {
  138. WORD depth, box;
  139. /*  Offsets in BitMap where boxes will be drawn.  */
  140. static SHORT boxoffsets[] = { 802, 2010, 3218 };
  141. static UWORD colortable[] = { BLACK, RED, GREEN, BLUE };
  142. struct RasInfo rasInfo;
  143.  
  144. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L);
  145. if (GfxBase == NULL)
  146.     cleanExit(RETURN_FAIL);
  147.  
  148. /*  Example steals screen from Intuition if Intuition is around.  */
  149. oldview = GfxBase->ActiView;    /* Save current View to restore later. */
  150.  
  151. InitView(&view);                /*  Initialize the View.  */
  152. InitVPort(&viewPort);           /*  Initialize the ViewPort.  */
  153. view.ViewPort = &viewPort;      /*  Link the ViewPort into the View.  */
  154.  
  155. /*  Init BitMap for RasInfo.  */
  156. InitBitMap(&bitMap, DEPTH, WIDTH, HEIGHT);
  157.  
  158. /*
  159.    Set the plane pointers to NULL so the cleanup
  160.    routine will know if they were used.
  161. */
  162. for(depth=0; depth<DEPTH; depth++)
  163.     bitMap.Planes[depth] = NULL;
  164.  
  165.  
  166. /*  Init RasInfo.  */
  167. rasInfo.BitMap = &bitMap;
  168. rasInfo.RxOffset = 0;
  169. rasInfo.RyOffset = 0;
  170. rasInfo.Next = NULL;
  171.  
  172. viewPort.RasInfo = &rasInfo;
  173. viewPort.DWidth = WIDTH;
  174. viewPort.DHeight = HEIGHT;
  175.  
  176. /*  Init ColorMap.  */
  177. /*  2 planes deep, so 4 entries (2 raised to the #_planes power).  */
  178. viewPort.ColorMap = GetColorMap(4L);
  179. if (viewPort.ColorMap == NULL)
  180.     cleanExit(RETURN_WARN);
  181.  
  182. /*  Change colors to those in colortable.  */
  183. LoadRGB4(&viewPort, colortable, 4);
  184.  
  185. /*  Allocate space for BitMap.  */
  186. for (depth=0; depth<DEPTH; depth++)
  187.     {
  188.     bitMap.Planes[depth] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  189.     if (bitMap.Planes[depth] == NULL)
  190.         cleanExit(RETURN_WARN);
  191.     }
  192.  
  193. /*  Construct preliminary Copper instruction list.  */
  194. MakeVPort( &view, &viewPort );
  195.  
  196. /*  Merge preliminary lists into a real Copper list in the view structure.  */
  197. MrgCop( &view );
  198.  
  199. for(depth=0; depth<DEPTH; depth++)
  200.     {
  201.     displaymem = (UBYTE *)bitMap.Planes[depth];
  202.     BltClear(displaymem, RASSIZE(WIDTH, HEIGHT), 0);
  203.     }
  204.  
  205. LoadView(&view);
  206.  
  207. /*
  208.    Now fill some boxes so that user can see something.
  209.    Always draw into both planes to assure true colors.
  210. */
  211. for (box=1; box<=3; box++)    /*  Three boxes; red, green, and blue.  */
  212.     {
  213.     for (depth=0; depth<DEPTH; depth++)    /*  Two planes.  */
  214.         {
  215.         displaymem = bitMap.Planes[depth] + boxoffsets[box-1];
  216.         drawFilledBox(box, depth);
  217.         }
  218.     }
  219.  
  220. Delay(10L * TICKS_PER_SECOND);    /*  Pause for 10 seconds,  */
  221.  
  222. cleanExit(RETURN_OK);    /*  then exit.  */
  223.  
  224. }    /*  End of main().  */
  225.  
  226.