home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / graphics_libraries / layers / layers.c < prev   
C/C++ Source or Header  |  1992-09-03  |  14KB  |  504 lines

  1. /* Layers.c
  2. **
  3. ** SAS/C 5.10a
  4. ** lc -b1 -cfist -v -y layers
  5. ** blink FROM LIB:c.o layers.o TO layers LIB LIB:lc.lib LIB:amiga.lib
  6.  
  7.  
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /* Force use of new variable names to help prevent errors */
  34. #define INTUI_V36_NAMES_ONLY
  35.  
  36. #include <exec/types.h>
  37. #include <graphics/gfxbase.h>
  38. #include <graphics/layers.h>
  39. #include <graphics/displayinfo.h>
  40.  
  41. #include <clib/exec_protos.h>
  42. #include <clib/dos_protos.h>
  43. #include <clib/graphics_protos.h>
  44. #include <clib/layers_protos.h>
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49.  
  50. #ifdef LATTICE
  51. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #endif
  54.  
  55. #define L_DELAY  (100)
  56. #define S_DELAY   (50)
  57.  
  58. #define DUMMY      (0L)
  59.  
  60. #define RED_PEN    (1)
  61. #define GREEN_PEN  (2)
  62. #define BLUE_PEN   (3)
  63.  
  64. #define SCREEN_D   (2)
  65. #define SCREEN_W (320)
  66. #define SCREEN_H (200)
  67.  
  68. /* the starting size of example layers, offsets are used for placement */
  69. #define W_H (50)
  70. #define W_T (5)
  71. #define W_B ((W_T+W_H)-1)
  72. #define W_W (80)
  73. #define W_L ((SCREEN_W/2) - (W_W/2))
  74. #define W_R ((W_L+W_W)-1)
  75.  
  76. /* size of the superbitmap */
  77. #define SUPER_H SCREEN_H
  78. #define SUPER_W SCREEN_W
  79.  
  80. /* starting size of the message layer */
  81. #define M_H (10)
  82. #define M_T (SCREEN_H-M_H)
  83. #define M_B ((M_T+M_H)-1)
  84. #define M_W (SCREEN_W)
  85. #define M_L (0)
  86. #define M_R ((M_L+M_W)-1)
  87.  
  88. struct GfxBase *GfxBase;
  89. struct Library *LayersBase;
  90.  
  91. /* global constant data for initializing the layers */
  92. LONG  theLayerFlags[3] = { LAYERSUPER, LAYERSMART, LAYERSIMPLE };
  93. UWORD colortable[]     = { 0x000, 0xf44, 0x4f4, 0x44f };
  94.  
  95.  
  96. /*
  97. ** Clear the layer then draw in a text string.
  98. */
  99. VOID myLabelLayer(struct Layer *layer, LONG color, UBYTE *string)
  100. {
  101. /* fill layer with color */
  102. SetRast(layer->rp, color);
  103.  
  104. /* set up for writing text into layer */
  105. SetDrMd(layer->rp,JAM1);
  106. SetAPen(layer->rp,0);
  107. Move(layer->rp, 5, 7);
  108.  
  109. /* write into layer */
  110. Text(layer->rp, string, strlen(string));
  111. }
  112.  
  113.  
  114.  
  115. /*
  116. ** write a message into a layer with a delay.
  117. */
  118. VOID pMessage(struct Layer *layer, UBYTE *string)
  119. {
  120. Delay(S_DELAY);
  121. myLabelLayer(layer, GREEN_PEN, string);
  122. }
  123.  
  124. /*
  125. ** write an error message into a layer with a delay.
  126. */
  127. VOID error(struct Layer *layer, UBYTE *string)
  128. {
  129. myLabelLayer(layer, RED_PEN, string);
  130. Delay(L_DELAY);
  131. }
  132.  
  133.  
  134.  
  135. /*
  136. ** do some layers manipulations to demonstrate their abilities.
  137. */
  138. VOID doLayers(struct Layer *msgLayer, struct Layer *layer_array[])
  139. {
  140. WORD ktr;
  141. WORD ktr_2;
  142.  
  143. pMessage(msgLayer, "Label all Layers");
  144. myLabelLayer(layer_array[0], RED_PEN,   "Super");
  145. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  146. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  147.  
  148. pMessage(msgLayer, "MoveLayer 1 InFrontOf 0");
  149. if (!MoveLayerInFrontOf(layer_array[1], layer_array[0]))
  150.     error(msgLayer, "MoveLayerInFrontOf() failed.");
  151.  
  152. pMessage(msgLayer, "MoveLayer 2 InFrontOf 1");
  153. if (!MoveLayerInFrontOf(layer_array[2], layer_array[1]))
  154.     error(msgLayer, "MoveLayerInFrontOf() failed.");
  155.  
  156. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  157. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  158.  
  159. pMessage(msgLayer, "Incrementally MoveLayers...");
  160. for(ktr = 0; ktr < 30; ktr++)
  161.     {
  162.     if (!MoveLayer(DUMMY, layer_array[1], -1, 0))
  163.         error(msgLayer, "MoveLayer() failed.");
  164.     if (!MoveLayer(DUMMY, layer_array[2], -2, 0))
  165.         error(msgLayer, "MoveLayer() failed.");
  166.     }
  167.  
  168. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  169. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  170.  
  171. pMessage(msgLayer, "make Layer 0 the UpfrontLayer");
  172. if (!UpfrontLayer(DUMMY, layer_array[0]))
  173.     error(msgLayer, "UpfrontLayer() failed.");
  174.  
  175. pMessage(msgLayer, "make Layer 2 the BehindLayer");
  176. if (!BehindLayer(DUMMY, layer_array[2]))
  177.     error(msgLayer, "BehindLayer() failed.");
  178.  
  179. pMessage(msgLayer, "Incrementally MoveLayers again...");
  180. for(ktr = 0; ktr < 30; ktr++)
  181.     {
  182.     if (!MoveLayer(DUMMY, layer_array[1], 0, 1))
  183.         error(msgLayer, "MoveLayer() failed.");
  184.     if (!MoveLayer(DUMMY, layer_array[2], 0, 2))
  185.         error(msgLayer, "MoveLayer() failed.");
  186.     }
  187.  
  188. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  189. myLabelLayer(layer_array[2], BLUE_PEN, "Simple");
  190.  
  191. pMessage(msgLayer, "Big MoveLayer");
  192. for(ktr = 0; ktr < 3; ktr++)
  193.     {
  194.     if (!MoveLayer(DUMMY, layer_array[ktr], -layer_array[ktr]->bounds.MinX, 0))
  195.         error(msgLayer, "MoveLayer() failed.");
  196.     }
  197.  
  198.  
  199. pMessage(msgLayer, "Incrementally increase size");
  200. for(ktr = 0; ktr < 5; ktr++)
  201.     {
  202.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  203.         {
  204.         if (!SizeLayer(DUMMY, layer_array[ktr_2], 1, 1))
  205.             error(msgLayer, "SizeLayer() failed.");
  206.         }
  207.     }
  208.  
  209. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  210. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  211. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  212. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  213.  
  214. pMessage(msgLayer, "Big SizeLayer");
  215. for(ktr = 0; ktr < 3; ktr++)
  216.     {
  217.     if (!SizeLayer(DUMMY,layer_array[ktr],
  218.                 SCREEN_W-(layer_array[ktr]->bounds.MaxX)-1,0))
  219.         error(msgLayer, "SizeLayer() failed.");
  220.     }
  221.  
  222. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  223. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  224. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  225. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  226.  
  227. pMessage(msgLayer, "ScrollLayer down");
  228. for(ktr = 0; ktr < 30; ktr++)
  229.     {
  230.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  231.         {
  232.         ScrollLayer(DUMMY, layer_array[ktr_2], 0, -1);
  233.         }
  234.     }
  235.  
  236. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  237. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  238. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  239. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  240.  
  241. pMessage(msgLayer, "ScrollLayer up");
  242. for(ktr = 0; ktr < 30; ktr++)
  243.     {
  244.     for(ktr_2 = 0; ktr_2 < 3; ktr_2++)
  245.         {
  246.         ScrollLayer(DUMMY, layer_array[ktr_2], 0, 1);
  247.         }
  248.     }
  249.  
  250. pMessage(msgLayer, "Refresh Smart Refresh Layer");
  251. myLabelLayer(layer_array[1], GREEN_PEN, "Smart");
  252. pMessage(msgLayer, "Refresh Simple Refresh Layer");
  253. myLabelLayer(layer_array[2], BLUE_PEN,  "Simple");
  254.  
  255. Delay(L_DELAY);
  256. }
  257.  
  258.  
  259. /*
  260. ** delete the layer array created by allocLayers().
  261. */
  262. VOID disposeLayers(struct Layer *msgLayer, struct Layer *layer_array[])
  263. {
  264. WORD ktr;
  265.  
  266. for (ktr = 0; ktr < 3; ktr++)
  267.     {
  268.     if (layer_array[ktr] != NULL)
  269.         {
  270.         if (!DeleteLayer(DUMMY, layer_array[ktr]))
  271.             error(msgLayer, "Error deleting layer");
  272.         }
  273.     }
  274. }
  275.  
  276.  
  277. /*
  278. ** Create some hard-coded layers.  The first must be super-bitmap, with
  279. ** the bitmap passed as an argument.  The others must not be super-bitmap.
  280. ** The pointers to the created layers are returned in layer_array.
  281. **
  282. ** Return FALSE on failure.  On a FALSE return, the layers are
  283. ** properly cleaned up.
  284. */
  285. BOOL allocLayers(struct Layer *msgLayer, struct Layer *layer_array[],
  286.     struct BitMap *super_bitmap, struct Layer_Info *theLayerInfo,
  287.     struct BitMap *theBitMap)
  288. {
  289. WORD ktr;
  290. BOOL create_layer_ok = TRUE;
  291.  
  292. for (ktr = 0;
  293.      (ktr < 3) && (create_layer_ok);
  294.      ktr++)
  295.     {
  296.