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

  1. /* For the sake of brevity, the example is a single task.  No Layer
  2. ** locking is done.  Also note that the routine myLabelLayer() is used
  3. ** to redraw a given layer.  It is called only when a layer needs
  4. ** refreshing.
  5. **
  6. ** Layers.c
  7. **
  8. ** SAS/C 5.10a
  9. ** lc -b1 -cfist -v -y layers
  10. ** blink FROM LIB:c.o layers.o TO layers LIB LIB:lc.lib LIB:amiga.lib
  11. */
  12.  
  13. /* Force use of new variable names to help prevent errors */
  14. #define INTUI_V36_NAMES_ONLY
  15.  
  16. #include <exec/types.h>
  17. #include <graphics/gfxbase.h>
  18. #include <graphics/layers.h>
  19. #include <graphics/displayinfo.h>
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/graphics_protos.h>
  24. #include <clib/layers_protos.h>
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #ifdef LATTICE
  31. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  32. int chkabort(void) { return(0); }  /* really */
  33. #endif
  34.  
  35. #define L_DELAY  (100)
  36. #define S_DELAY   (50)
  37.  
  38. #define DUMMY      (0L)
  39.  
  40. #define RED_PEN    (1)
  41. #define GREEN_PEN  (2)
  42. #define BLUE_PEN   (3)
  43.  
  44. #define SCREEN_D   (2)
  45. #define SCREEN_W (320)
  46. #define SCREEN_H (200)
  47.  
  48. /* the starting size of example layers, offsets are used for placement */
  49. #define W_H (50)
  50. #define W_T (5)
  51. #define W_B ((W_T+W_H)-1)
  52. #define W_W (80)
  53. #define W_L ((SCREEN_W/2) - (W_W/2))
  54. #define W_R ((W_L+W_W)-1)
  55.  
  56. /* size of the superbitmap */
  57. #define SUPER_H SCREEN_H
  58. #define SUPER_W SCREEN_W
  59.  
  60. /* starting size of the message layer */
  61. #define M_H (10)
  62. #define M_T (SCREEN_H-M_H)
  63. #define M_B ((M_T+M_H)-1)
  64. #define M_W (SCREEN_W)
  65. #define M_L (0)
  66. #define M_R ((M_L+M_W)-1)
  67.  
  68. struct GfxBase *GfxBase;
  69. struct Library *LayersBase;
  70.  
  71. /* global constant data for initializing the layers */
  72. LONG  theLayerFlags[3] = { LAYERSUPER, LAYERSMART, LAYERSIMPLE };
  73. UWORD colortable[]     = { 0x000, 0xf44, 0x4f4, 0x44f };
  74.  
  75.  
  76. /*
  77. ** Clear the layer then draw in a text string.
  78. */
  79. VOID myLabelLayer(struct Layer *layer, LONG color, UBYTE *string)
  80. {
  81. /* fill layer with color */
  82. SetRast(layer->rp, color);
  83.  
  84. /* set up for writing text into layer */
  85. SetDrMd(layer->rp,JAM1);
  86. SetAPen(layer->rp,0);
  87. Move(layer->rp, 5, 7);
  88.  
  89. /* write into layer */
  90. Text(layer->rp, string, strlen(string));
  91. }
  92.  
  93.  
  94. /*
  95. ** write a message into a layer with a delay.
  96. */
  97. VOID pMessage(struct Layer *layer, UBYTE *string)
  98. {
  99. Delay(S_DELAY);
  100. myLabelLayer(layer, GREEN_PEN, string);
  101. }
  102.  
  103. /*
  104. ** write an error message into a layer with a delay.
  105. */
  106. VOID error(struct Layer *layer, UBYTE *string)
  107. {
  108. myLabelLayer(layer, RED_PEN, string);
  109. Delay(L_DELAY);
  110. }
  111.  
  112.  
  113. /*
  114. ** do some layers manipulations to demonstrate their abilities.
  115. */
  116. VOID doLayers(struct Layer *msgLayer, struct Layer *layer_array[])
  117. {
  118. WORD ktr;
  119. WORD ktr_2;
  120.  
  121. pMessage(msgLayer, "Label all Layers");
  122. myLabelLayer(layer_array[0], RED_PEN,   "Super");
  123. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  124. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  125.  
  126. pMessage(msgLayer, "MoveLayer 1 InFrontOf 0");
  127. if (!MoveLayerInFrontOf(layer_array[1], layer_array[0]))
  128.     error(msgLayer, "MoveLayerInFrontOf() failed.");
  129.  
  130. pMessage(msgLayer, "MoveLayer 2 InFrontOf 1");
  131. if (!MoveLayerInFrontOf(layer_array[2], layer_array[1]))
  132.     error(msgLayer, "MoveLayerInFrontOf() failed.");
  133.  
  134. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  135. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  136.  
  137. pMessage(msgLayer, "Incrementally MoveLayers...");
  138. for(ktr = 0; ktr < 30; ktr++)
  139.     {
  140.     if (!MoveLayer(DUMMY, layer_array[1], -1, 0))
  141.         error(msgLayer, "MoveLayer() failed.");
  142.     if (!MoveLayer(DUMMY, layer_array[2], -2, 0))
  143.         error(msgLayer, "MoveLayer() failed.");
  144.     }
  145.  
  146. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  147. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  148.  
  149. pMessage(msgLayer, "make Layer 0 the UpfrontLayer");
  150. if (!UpfrontLayer(DUMMY, layer_array[0]))
  151.     error(msgLayer, "UpfrontLayer() failed.");
  152.  
  153. pMessage(msgLayer, "make Layer 2 the BehindLayer");
  154. if (!BehindLayer(DUMMY, layer_array[2]))
  155.     error(msgLayer, "BehindLayer() failed.");
  156.  
  157. pMessage(msgLayer, "Incrementally MoveLayers again...");
  158. for(ktr = 0; ktr < 30; ktr++)
  159.     {
  160.     if (!MoveLayer(DUMMY, layer_array[1], 0, 1))
  161.         error(msgLayer, "MoveLayer() failed.");
  162.     if (!MoveLayer(DUMMY, layer_array[2], 0, 2))
  163.         error(msgLayer, "MoveLayer() failed.");
  164.     }
  165.  
  166. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  167. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  168.  
  169. pMessage(msgLayer, "Big MoveLayer");
  170. for(ktr = 0; ktr < 3; ktr++)
  171.     {
  172.     if (!MoveLayer(DUMMY, layer_array[ktr], -layer_array[ktr]->bounds.MinX, 0))
  173.         error(msgLayer, "MoveLayer() failed.");
  174.     }
  175.  
  176.  
  177. pMessage(msgLayer, "Incrementally increase size");
  178. for(ktr = 0; ktr < 5; ktr++)
  179.     {
  180.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  181.         {
  182.         if (!SizeLayer(DUMMY, layer_array[ktr_2], 1, 1))
  183.             error(msgLayer, "SizeLayer() failed.");
  184.         }
  185.     }
  186.  
  187. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  188. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  189. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  190. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  191.  
  192. pMessage(msgLayer, "Big SizeLayer");
  193. for(ktr = 0; ktr < 3; ktr++)
  194.     {
  195.     if (!SizeLayer(DUMMY,layer_array[ktr],
  196.                 SCREEN_W-(layer_array[ktr]->bounds.MaxX)-1,0))
  197.         error(msgLayer, "SizeLayer() failed.");
  198.     }
  199.  
  200. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  201. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  202. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  203. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  204.  
  205. pMessage(msgLayer, "ScrollLayer down");
  206. for(ktr = 0; ktr < 30; ktr++)
  207.     {
  208.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  209.         {
  210.         ScrollLayer(DUMMY, layer_array[ktr_2], 0, -1);
  211.         }
  212.     }
  213.  
  214. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  215. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  216. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  217. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  218.  
  219. pMessage(msgLayer, "ScrollLayer up");
  220. for(ktr = 0; ktr < 30; ktr++)
  221.     {
  222.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  223.         {
  224.         ScrollLayer(DUMMY, layer_array[ktr_2], 0, 1);
  225.         }
  226.     }
  227.  
  228. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  229. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  230. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  231. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  232.  
  233. Delay(L_DELAY);
  234. }
  235.  
  236.  
  237. /*
  238. ** delete the layer array created by allocLayers().
  239. */
  240. VOID disposeLayers(struct Layer *msgLayer, struct Layer *layer_array[])
  241. {
  242. WORD ktr;
  243.  
  244. for (ktr = 0; ktr < 3; ktr++)
  245.     {
  246.     if (layer_array[ktr] != NULL)
  247.         {
  248.         if (!DeleteLayer(DUMMY, layer_array[ktr]))
  249.             error(msgLayer, "Error deleting layer");
  250.         }
  251.     }
  252. }
  253.  
  254.  
  255. /*
  256. ** Create some hard-coded layers.  The first must be super-bitmap, with
  257. ** the bitmap passed as an argument.  The others must not be super-bitmap.
  258. ** The pointers to the created layers are returned in layer_array.
  259. **
  260. ** Return FALSE on failure.  On a FALSE return, the layers are
  261. ** properly cleaned up.
  262. */
  263. BOOL allocLayers(struct Layer *msgLayer, struct Layer *layer_array[],
  264.     struct BitMap *super_bitmap, struct Layer_Info *theLayerInfo,
  265.     struct BitMap *theBitMap)
  266. {
  267. WORD ktr;
  268. BOOL create_layer_ok = TRUE;
  269.  
  270. for (ktr = 0;
  271.      (ktr < 3) && (create_layer_ok);
  272.      ktr++)
  273.     {
  274.     pMessage(msgLayer, "Create BehindLayer");
  275.     if (ktr == 0)
  276.         {
  277.         if ((layer_array[ktr] = CreateBehindLayer(theLayerInfo, theBitMap,
  278.                   W_L+(ktr*30), W_T+(ktr*30), W_R+(ktr*30), W_B+(ktr*30),
  279.                   theLayerFlags[ktr], super_bitmap)) == NULL)
  280.             create_layer_ok = FALSE;
  281.         }
  282.      else
  283.         {
  284.         if ((layer_array[ktr] = CreateBehindLayer(theLayerInfo, theBitMap,
  285.                   W_L+(ktr*30), W_T+(ktr*30), W_R+(ktr*30), W_B+(ktr*30),
  286.                   theLayerFlags[ktr], NULL)) == NULL)
  287.             create_layer_ok = FALSE;
  288.         }
  289.  
  290.     if (create_layer_ok)
  291.         {
  292.         pMessage(msgLayer, "Fill the RastPort");
  293.         SetRast(layer_array[ktr]->rp, ktr + 1);
  294.         }
  295.     }
  296.  
  297. if (!create_layer_ok)
  298.     disposeLayers(msgLayer, layer_array);
  299.  
  300. return(create_layer_ok);
  301. }
  302.  
  303.  
  304. /*
  305. ** Free the bitmap and all bitplanes created by allocBitMap().
  306. */
  307. VOID disposeBitMap(struct BitMap *bitmap, LONG depth, LONG width, LONG height)
  308. {
  309. WORD ktr;
  310.  
  311. if (NULL != bitmap)
  312.     {
  313.     for (ktr = 0; ktr < depth; ktr++)
  314.         {
  315.         if (NULL != bitmap->Planes[ktr])
  316.             FreeRaster(bitmap->Planes[ktr], width, height);
  317.         }
  318.  
  319.     FreeMem(bitmap, sizeof(*bitmap));
  320.     }
  321. }
  322.  
  323.  
  324. /*
  325. ** Allocate and initialize a bitmap structure.
  326. */
  327. struct BitMap *allocBitMap(LONG depth, LONG width, LONG height)
  328. {
  329. WORD ktr;
  330. BOOL bit_map_failed = FALSE;
  331. struct BitMap *bitmap = NULL;
  332.  
  333. if (NULL != (bitmap = AllocMem(sizeof(*bitmap),NULL)))
  334.     {
  335.     InitBitMap(bitmap,depth,width,height);
  336.  
  337.     for (ktr = 0; ktr < depth; ktr++)
  338.         {
  339.         if (NULL == (bitmap->Planes[ktr] = (PLANEPTR)AllocRaster(width,height)))
  340.             bit_map_failed = TRUE;
  341.         else
  342.             BltClear(bitmap->Planes[ktr], RASSIZE(width,height), 1);
  343.         }
  344.     if (bit_map_failed)
  345.         {
  346.         disposeBitMap(bitmap,depth,width,height);
  347.         bitmap = NULL;
  348.         }
  349.     }
  350. return(bitmap);
  351. }
  352.  
  353. /*
  354. ** Set up to run the layers example, doLayers(). Clean up when done.
  355. */
  356. VOID startLayers(struct Layer_Info *theLayerInfo, struct BitMap *theBitMap)
  357. {
  358. struct Layer  *msgLayer;
  359. struct BitMap *theSuperBitMap;
  360. struct Layer  *theLayers[3] = { NULL, NULL, NULL, };
  361.  
  362. if (NULL != (msgLayer = CreateUpfrontLayer(theLayerInfo, theBitMap,
  363.                      M_L, M_T, M_R, M_B, LAYERSMART, NULL)))
  364.     {
  365.     pMessage(msgLayer, "Setting up Layers");
  366.  
  367.     if (NULL != (theSuperBitMap = allocBitMap(SCREEN_D, SUPER_W, SUPER_H)))
  368.         {
  369.         if (allocLayers(msgLayer, theLayers, theSuperBitMap, theLayerInfo, theBitMap))
  370.             {
  371.             doLayers(msgLayer, theLayers);
  372.  
  373.             disposeLayers(msgLayer, theLayers);
  374.             }
  375.         disposeBitMap(theSuperBitMap, SCREEN_D, SUPER_W, SUPER_H);
  376.         }
  377.     if (!DeleteLayer(DUMMY, msgLayer))
  378.         error(msgLayer, "Error deleting layer");
  379.     }
  380. }
  381.  
  382. /*
  383. ** Set up a low-level graphics display for layers to work on.  Layers
  384. ** should not be built directly on Intuition screens, use a low-level
  385. ** graphics view.  If you need mouse or other events for the layers
  386. ** display, you have to get them directly from the input device.  The
  387. ** only supported method of using layers library calls with Intuition
  388. ** (other than the InstallClipRegion() call) is through Intuition windows.
  389. **
  390. ** See graphics primitives chapter for details on creating and using the
  391. ** low-level graphics calls.
  392. */
  393. VOID runNewView(VOID)
  394. {
  395. struct View        theView;
  396. struct View       *oldview;
  397. struct ViewPort    theViewPort;
  398. struct RasInfo     theRasInfo;
  399. struct ColorMap   *theColorMap;
  400. struct Layer_Info *theLayerInfo;
  401. struct BitMap     *theBitMap;
  402. UWORD             *colorpalette;
  403. WORD               ktr;
  404.  
  405. /* save current view, to be restored when done */
  406. if (NULL != (oldview = GfxBase->ActiView))
  407.     {
  408.     /* get a LayerInfo structure */
  409.     if (NULL != (theLayerInfo = NewLayerInfo()))
  410.         {
  411.         if (NULL != (theColorMap = GetColorMap(4)))
  412.             {
  413.             colorpalette = (UWORD *)theColorMap->ColorTable;
  414.             for(ktr = 0; ktr < 4; ktr++)
  415.                 *colorpalette++ = colortable[ktr];
  416.  
  417.             if (NULL != (theBitMap = allocBitMap(SCREEN_D, SCREEN_W, SCREEN_H)))
  418.                 {
  419.                 InitView(&theView);
  420.                 InitVPort(&theViewPort);
  421.  
  422.                 theView.ViewPort = &theViewPort;
  423.  
  424.                 theViewPort.DWidth   = SCREEN_W;
  425.                 theViewPort.DHeight  = SCREEN_H;
  426.                 theViewPort.RasInfo  = &theRasInfo;
  427.                 theViewPort.ColorMap = theColorMap;
  428.  
  429.                 theRasInfo.BitMap   = theBitMap;
  430.                 theRasInfo.RxOffset = 0;
  431.                 theRasInfo.RyOffset = 0;
  432.                 theRasInfo.Next     = NULL;
  433.  
  434.                 MakeVPort(&theView, &theViewPort);
  435.                 MrgCop(&theView);
  436.                 LoadView(&theView);
  437.                 WaitTOF();
  438.  
  439.                 startLayers(theLayerInfo, theBitMap);
  440.  
  441.                 /* put back the old view, wait for it to become
  442.                 ** active before freeing any of our display
  443.                 */
  444.                 LoadView(oldview);
  445.                 WaitTOF();
  446.  
  447.                 /* free dynamically created structures */
  448.                 FreeVPortCopLists(&theViewPort);
  449.                 FreeCprList(theView.LOFCprList);
  450.  
  451.                 disposeBitMap(theBitMap, SCREEN_D, SCREEN_W, SCREEN_H);
  452.                 }
  453.             FreeColorMap(theColorMap);       /* free the color map */
  454.             }
  455.         DisposeLayerInfo(theLayerInfo);
  456.         }
  457.     }
  458. }
  459.  
  460. /*
  461. ** Open the libraries used by the example.  Clean up when done.
  462. */
  463. VOID main(int argc, char **argv)
  464. {
  465. if (NULL != (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33L)))
  466.     {
  467.     if (NULL != (LayersBase = OpenLibrary("layers.library",33L)))
  468.         {
  469.         runNewView();
  470.  
  471.         CloseLibrary((struct Library *)LayersBase);
  472.         }
  473.     CloseLibrary((struct Library *)GfxBase);
  474.     }
  475. }
  476.