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

  1. /* DualPF
  2.    Dual-PlayFields Example
  3.    For Lattice, compile and link with:  LC -b1 -cfist -L -v -y DualPF.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/rastport.h>
  31. #include <graphics/view.h>
  32. #include <hardware/dmabits.h>
  33. #include <hardware/custom.h>
  34. #include <libraries/dos.h>
  35.  
  36. #include <proto/all.h>
  37. #include <stdlib.h>
  38.  
  39. #define DEPTH 2  
  40. #define WIDTH 320 
  41. #define HEIGHT 200 
  42.  
  43. struct GfxBase *GfxBase = NULL;
  44.  
  45. /* Add a second BitMap for dual playfield. */
  46. struct BitMap bitMap, bitMap2;
  47. struct View view, *oldview;
  48.  
  49. #define BLACK  0x000
  50. #define BLUE   0x00f
  51. #define GREEN  0x0f0
  52. #define RED    0xf00
  53. #define VIOLET 0xf0f
  54. #define ORANGE 0xf80
  55. #define WHITE  0xfff
  56.  
  57. #define COLOR0 0
  58. #define COLOR1 1
  59. #define COLOR2 2
  60. #define COLOR3 3
  61.  
  62. VOID cleanExit(int exitStatus);
  63.  
  64. VOID main(VOID)
  65. {
  66. SHORT i, stripe;
  67. LONG color;
  68.  
  69. /* In dual playfield mode, colors 0-7 are dedicated to playfield one
  70.  * and colors 8-15 to playfield two.  Since (in this example)
  71.  * there are only 2 planes in each playfield, colors 4-7 and 12-15
  72.  * won't even get used.  Colors 4-7 are included below to keep the
  73.  * values of colors 8-11 in their proper locations for LoadRGB4().
  74.  */
  75. static UWORD colortable[] =
  76. {
  77.     BLACK, WHITE, ORANGE, RED,
  78.     0, 0, 0, 0,    /* The values for colors 4-7 are placeholders. */
  79.     /* The second playfield's BLACK will be transparent. */
  80.     BLACK, GREEN, VIOLET, BLUE
  81. };
  82.  
  83. /* Add a second RasInfo for dual playfield. */
  84. struct RasInfo rasInfo, rasInfo2;
  85. struct RastPort rastPort, rastPort2;    /* RastPorts for both BitMaps. */
  86. struct ViewPort viewPort;
  87.  
  88. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L);
  89. if (GfxBase == NULL)
  90.     cleanExit(ERROR_INVALID_RESIDENT_LIBRARY);
  91.  
  92. /* Initialize the BitMaps. */
  93. InitBitMap(&bitMap, DEPTH, WIDTH, HEIGHT);
  94. InitBitMap(&bitMap2, DEPTH, WIDTH, HEIGHT);
  95.  
  96. /* Clear the plane pointers. */
  97. for (i=0; i<DEPTH; i++)
  98.     bitMap.Planes[i] = bitMap2.Planes[i] = NULL;
  99.  
  100. /* Allocate space for their Planes. */
  101. for (i=0; i<DEPTH; i++)
  102.     {
  103.     bitMap.Planes[i] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  104.     if (bitMap.Planes[i] == NULL)
  105.         cleanExit(ERROR_NO_FREE_STORE);
  106.  
  107.     bitMap2.Planes[i] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  108.     if (bitMap2.Planes[i] == NULL)
  109.         cleanExit(ERROR_NO_FREE_STORE);
  110.     }
  111.  
  112. /* Initialize the RastPorts and link the BitMaps to them. */
  113. InitRastPort(&rastPort);
  114. InitRastPort(&rastPort2);
  115. rastPort.BitMap = &bitMap;
  116. rastPort2.BitMap = &bitMap2;
  117.  
  118. /* Simple form of setting drawing area to color COLOR0. */
  119. SetRast(&rastPort, COLOR0);
  120. SetRast(&rastPort2, COLOR0);
  121.  
  122. /* Initialize the RasInfos. */
  123. rasInfo.BitMap = &bitMap;    /* Attach the corresponding BitMap. */
  124. rasInfo.RxOffset = 0;    /* Align upper left corners of display */
  125. rasInfo.RyOffset = 0;    /*   with upper left corner of drawing area. */
  126. rasInfo.Next = &rasInfo2;    /* Link second RasInfo structure to the first. */
  127.  
  128. /* Initialize second RasInfo for Dual Playfield. */
  129. rasInfo2.BitMap = &bitMap2;
  130. rasInfo2.RxOffset = 0;
  131. rasInfo2.RyOffset = 0;
  132. rasInfo2.Next = NULL;
  133.  
  134. InitVPort(&viewPort);    /* Initialize the ViewPort. */
  135.  
  136. /* Specify ViewPort characteristics. */
  137. viewPort.DWidth = WIDTH;
  138. viewPort.DHeight = HEIGHT;
  139. viewPort.RasInfo = &rasInfo;
  140. viewPort.Modes = DUALPF;    /* dual-playfield mode */
  141.  
  142. /* Initialize the ViewPort's ColorMap. */
  143. viewPort.ColorMap = GetColorMap(12L);
  144. if (viewPort.ColorMap == NULL)
  145.     cleanExit(ERROR_NO_FREE_STORE);
  146. LoadRGB4(&viewPort, colortable, 12);
  147.  
  148. InitView(&view);    /* Initialize the View. */
  149. view.ViewPort = &viewPort;    /* Attach the ViewPort to the View. */
  150.  
  151. /* Construct (preliminary) Copper instruction list. */
  152. MakeVPort( &view, &viewPort );
  153.  
  154. /* Merge preliminary lists together into a real
  155.  * Copper list in the View structure.
  156.  */
  157. MrgCop( &view );
  158.  
  159. /* Save current view to restore later.  Example steals
  160.  * the screen from the active view, but restores it when done.
  161.  */
  162. oldview = GfxBase->ActiView;
  163.  
  164. /* Load the newly created View. */
  165. LoadView( &view );
  166.  
  167. /* Stripes of color will be drawn in the playfields.
  168.  * Wherever color 0 is used in playfield number 1,
  169.  * the colors of playfield number 2 will show through.
  170.  */
  171.  
  172. /* Playfield number 1.  Vertical stripes. */
  173. stripe = WIDTH/32;
  174. for (color=0L, i=WIDTH-stripe; i>=0; i-=stripe)
  175.     {
  176.     SetAPen(&rastPort, color++ % 4L);    /* Cycle through the four colors. */
  177.  
  178.     /* Create a rectangle.  The coordinates are inset by one on all
  179.      * four edges to allow for the outline created by RectFill.
  180.      */
  181.     RectFill(&rastPort, i+1, 1, i+stripe-2, HEIGHT-1);
  182.  
  183.     /* Delay for 1/4 second between stripes.
  184.      * The +1L prevents the possibility of a Delay(0) which
  185.      * would cause problems due to a bug in the timer.device.
  186.      */
  187.     Delay(TICKS_PER_SECOND / 4L + 1L);
  188.     }
  189.  
  190. Delay(1L * TICKS_PER_SECOND);      /* Pause for one second. */
  191.  
  192. /* Playfield number 2.  Horizontal stripes. */
  193. stripe = HEIGHT/20;
  194. for (color=0L, i=HEIGHT-stripe; i>=0; i-=stripe)
  195.     {
  196.     SetAPen(&rastPort2, color++ % 4L);
  197.     RectFill(&rastPort2, 1, i+1, WIDTH-1, i+stripe-2);
  198.     Delay(TICKS_PER_SECOND / 4L + 1L);
  199.     }
  200.  
  201. Delay(10L * TICKS_PER_SECOND);
  202. cleanExit(RETURN_OK);
  203.  
  204. }    /* end of main() */
  205.  
  206.  
  207. VOID cleanExit(int exitStatus)
  208. {
  209. SHORT i;
  210.  
  211. if (oldview)
  212.     {
  213.     LoadView(oldview);    /* Put the original View back again. */
  214.     WaitTOF();    /* Wait for that View to return. */
  215.     }
  216.  
  217. /* Free the drawing area. */
  218. for(i=0; i<DEPTH; i++)
  219.     {
  220.     if (bitMap.Planes[i])
  221.         FreeRaster(bitMap.Planes[i], WIDTH, HEIGHT);
  222.  
  223.     if (bitMap2.Planes[i])
  224.         FreeRaster(bitMap2.Planes[i], WIDTH, HEIGHT);
  225.     }
  226.  
  227. /* Free the color map created by GetColorMap(). */
  228. if (view.ViewPort->ColorMap)
  229.     FreeColorMap(view.ViewPort->ColorMap);
  230.  
  231. /* Free dynamically created structures. */
  232. if (view.ViewPort)
  233.     FreeVPortCopLists(view.ViewPort);            
  234. if (view.LOFCprList)
  235.     FreeCprList(view.LOFCprList);
  236.  
  237. if (GfxBase)
  238.     CloseLibrary((struct Library *)GfxBase);
  239.  
  240. exit(exitStatus);
  241. }
  242.  
  243.