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

  1. /* doublebuffer.c
  2. ** show the use of a double-buffered screen.
  3. **
  4. ** SAS/C 5.10a
  5. ** lc -b1 -cfist -v -y doublebuffer
  6. ** blink FROM LIB:c.o doublebuffer.o TO doublebuffer LIB LIB:lc.lib LIB:amiga.lib
  7. */
  8.  
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/screens.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20.  
  21. #ifdef LATTICE
  22. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  23. int chkabort(void) { return(0); }  /* really */
  24. #endif
  25.  
  26.  
  27. /* characteristics of the screen */
  28. #define SCR_WIDTH  (320)
  29. #define SCR_HEIGHT (200)
  30. #define SCR_DEPTH    (2)
  31.  
  32.  
  33. /* Prototypes for our functions */
  34. VOID   runDBuff(struct Screen *, struct BitMap ** );
  35. struct BitMap **setupBitMaps( LONG, LONG, LONG );
  36. VOID   freeBitMaps(struct BitMap **,LONG, LONG, LONG );
  37. LONG   setupPlanes(struct BitMap *, LONG, LONG, LONG );
  38. VOID    freePlanes(struct BitMap *, LONG, LONG, LONG );
  39.  
  40.  
  41. struct Library *IntuitionBase = NULL;
  42. struct Library *GfxBase       = NULL;
  43.  
  44. /*
  45. ** Main routine.  Setup for using the double buffered screen.
  46. ** Clean up all resources when done or on any error.
  47. */
  48.  
  49. VOID main(int argc, char **argv)
  50. {
  51. struct BitMap **myBitMaps;
  52. struct Screen  *screen;
  53. struct NewScreen myNewScreen;
  54.  
  55. IntuitionBase = OpenLibrary("intuition.library", 33L);
  56. if ( IntuitionBase != NULL )
  57.     {
  58.     GfxBase = OpenLibrary("graphics.library", 33L);
  59.     if ( GfxBase != NULL )
  60.         {
  61.         myBitMaps = setupBitMaps(SCR_DEPTH, SCR_WIDTH, SCR_HEIGHT);
  62.         if ( myBitMaps != NULL )
  63.             {
  64.             /* Open a simple quiet screen that is using the first
  65.             ** of the two bitmaps.
  66.             */
  67.             myNewScreen.LeftEdge=0;
  68.             myNewScreen.TopEdge=0;
  69.             myNewScreen.Width=SCR_WIDTH;
  70.             myNewScreen.Height=SCR_HEIGHT;
  71.             myNewScreen.Depth=SCR_DEPTH;
  72.             myNewScreen.DetailPen=0;
  73.             myNewScreen.BlockPen=1;
  74.             myNewScreen.ViewModes=HIRES;
  75.             myNewScreen.Type=CUSTOMSCREEN | CUSTOMBITMAP | SCREENQUIET;
  76.             myNewScreen.Font=NULL;
  77.             myNewScreen.DefaultTitle=NULL;
  78.             myNewScreen.Gadgets=NULL;
  79.             myNewScreen.CustomBitMap=myBitMaps[0];
  80.  
  81.             screen = OpenScreen(&myNewScreen);
  82.             if (screen != NULL)
  83.                 {
  84.                 /* Indicate that the rastport is double buffered. */
  85.                 screen->RastPort.Flags = DBUFFER;
  86.  
  87.                 runDBuff(screen, myBitMaps);
  88.  
  89.                 CloseScreen(screen);
  90.                 }
  91.             freeBitMaps(myBitMaps, SCR_DEPTH, SCR_WIDTH, SCR_HEIGHT);
  92.             }
  93.         CloseLibrary(GfxBase);
  94.         }
  95.     CloseLibrary(IntuitionBase);
  96.     }
  97. }
  98.  
  99. /*
  100. ** setupBitMaps(): allocate the bit maps for a double buffered screen.
  101. */
  102. struct BitMap **setupBitMaps(LONG depth, LONG width, LONG height)
  103. {
  104. /* this must be static -- it cannot go away when the routine exits. */
  105. static struct BitMap *myBitMaps[2];
  106.  
  107. myBitMaps[0] = (struct BitMap *) AllocMem((LONG)sizeof(struct BitMap), MEMF_CLEAR);
  108. if (myBitMaps[0] != NULL)
  109.     {
  110.     myBitMaps[1] = (struct BitMap *)AllocMem((LONG)sizeof(struct BitMap), MEMF_CLEAR);
  111.     if (myBitMaps[1] != NULL)
  112.         {
  113.         InitBitMap(myBitMaps[0], depth, width, height);
  114.         InitBitMap(myBitMaps[1], depth, width, height);
  115.  
  116.         if (NULL != setupPlanes(myBitMaps[0], depth, width, height))
  117.             {
  118.             if (NULL != setupPlanes(myBitMaps[1], depth, width, height))
  119.                 return(myBitMaps);
  120.  
  121.             freePlanes(myBitMaps[0], depth, width, height);
  122.             }
  123.         FreeMem(myBitMaps[1], (LONG)sizeof(struct BitMap));
  124.         }
  125.     FreeMem(myBitMaps[0], (LONG)sizeof(struct BitMap));
  126.     }
  127. return(NULL);
  128. }
  129.  
  130. /*
  131. ** runDBuff(): loop through a number of iterations of drawing into
  132. ** alternate frames of the double-buffered screen.  Note that the
  133. ** object is drawn in color 1.
  134. */
  135. VOID runDBuff(struct Screen *screen, struct BitMap **myBitMaps)
  136. {
  137. WORD ktr, xpos, ypos;
  138. WORD toggleFrame;
  139.  
  140. toggleFrame = 0;
  141. SetAPen(&(screen->RastPort), 1);
  142.  
  143. for (ktr = 1; ktr < 200; ktr++)
  144.     {
  145.     /* Calculate a position to place the object, these
  146.     ** calculations insure the object will stay on the screen
  147.     ** given the range of ktr and the size of the object.
  148.     */
  149.     xpos = ktr;
  150.     if ((ktr % 100) >= 50)
  151.         ypos = 50 - (ktr % 50);
  152.     else
  153.         ypos = ktr % 50;
  154.  
  155.     /* switch the bitmap so that we are drawing into the correct place */
  156.     screen->RastPort.BitMap          = myBitMaps[toggleFrame];
  157.     screen->ViewPort.RasInfo->BitMap = myBitMaps[toggleFrame];
  158.  
  159.     /* Draw the objects.
  160.     ** Here we clear the old frame and draw a simple filled rectangle.
  161.     */
  162.     SetRast(&(screen->RastPort), 0);
  163.     RectFill(&(screen->RastPort), xpos, ypos, xpos+100, ypos+100);
  164.  
  165.     /* update the physical display to match the newly drawn bitmap. */
  166.     MakeScreen(screen); /* Tell intuition to do its stuff.          */
  167.     RethinkDisplay();   /* Intuition compatible MrgCop & LoadView   */
  168.                         /*               it also does a WaitTOF().  */
  169.  
  170.     /* switch the frame number for next time through */
  171.     toggleFrame ^= 1;
  172.     }
  173. }
  174.  
  175. /*
  176. ** freeBitMaps(): free up the memory allocated by setupBitMaps().
  177. */
  178. VOID freeBitMaps(struct BitMap **myBitMaps, LONG depth, LONG width, LONG height)
  179. {
  180. freePlanes(myBitMaps[0], depth, width, height);
  181. freePlanes(myBitMaps[1], depth, width, height);
  182.  
  183. FreeMem(myBitMaps[0], (LONG)sizeof(struct BitMap));
  184. FreeMem(myBitMaps[1], (LONG)sizeof(struct BitMap));
  185. }
  186.  
  187. /*
  188. ** setupPlanes(): allocate the bit planes for a screen bit map.
  189. */
  190. LONG setupPlanes(struct BitMap *bitMap, LONG depth, LONG width, LONG height)
  191. {
  192. SHORT plane_num ;
  193.  
  194. for (plane_num = 0; plane_num < depth; plane_num++)
  195.     {
  196.     bitMap->Planes[plane_num] = (PLANEPTR)AllocRaster(width, height);
  197.     if (bitMap->Planes[plane_num] != NULL )
  198.         BltClear(bitMap->Planes[plane_num], (width / 8) * height, 1);
  199.     else
  200.         {
  201.         freePlanes(bitMap, depth, width, height);
  202.         return(NULL);
  203.         }
  204.     }
  205. return(TRUE);
  206. }
  207.  
  208. /*
  209. ** freePlanes(): free up the memory allocated by setupPlanes().
  210. */
  211. VOID freePlanes(struct BitMap *bitMap, LONG depth, LONG width, LONG height)
  212. {
  213. SHORT plane_num ;
  214.  
  215. for (plane_num = 0; plane_num < depth; plane_num++)
  216.     {
  217.     if (bitMap->Planes[plane_num] != NULL)
  218.         FreeRaster(bitMap->Planes[plane_num], width, height);
  219.     }
  220. }
  221.