home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / GWorld / GWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-02  |  4.4 KB  |  146 lines  |  [TEXT/MMCC]

  1. /* =========================================================
  2.    ======================= GWorld.c ========================
  3.    =========================================================
  4.    
  5.    © 1994 by Thomas Reed
  6.    
  7.    GWorld.c contains 4 routines:
  8.    
  9.      UseOffWorld(GWorldPtr)
  10.      
  11.        Sets up a pre-allocated GWorld for drawing.  You must call
  12.        this routine before doing ANY drawing in a GWorld (not
  13.        counting CopyBits calls).  This will set the GWorld as
  14.        the current port.
  15.        
  16.      DoneWithOffWorld(GWorldPtr)
  17.      
  18.        Call this after you've done all the drawing you want in
  19.        the GWorld.  This will restore the port that was set
  20.        before calling UseOffWorld.
  21.        
  22.      CreateOffscreenBitMap(GrafPtr *, Rect *)
  23.      
  24.        Call this to allocate space for an offscreen GrafPort,
  25.        and initialize the fields.  After calling this, you can
  26.        go ahead and start drawing to the buffer.
  27.        
  28.      DestroyOffscreenBitMap(GrafPtr)
  29.      
  30.        De-allocates the offscreen GrafPort set up with
  31.        CreateOffscreenBitMap.  This releases all space taken
  32.        up by the GrafPort.
  33.        
  34.    Notice that there are no routines for setting up and disposing
  35.    a GWorld.  This is because it's gotten much simpler between the
  36.    times of GrafPorts and the current GWorlds.  To allocate a GWorld,
  37.    just call the Toolbox routine:
  38.    
  39.      myErr = NewGWorld(&offscreenGWorld,pixelDepth,&boundsRect,cTable,aGDevice,flags);
  40.      (further described in IM VI chap. 21 pg 12)
  41.    
  42.    To dispose of the GWorld, call:
  43.    
  44.      DisposeGWorld(offscreenGWorld);
  45.      (further described in IM VI chap. 21 pg 19)
  46.    
  47.    Make sure to include the GWorld.h header in your source files that
  48.    call the routines contained herein.
  49.    
  50.    If you use this source, just give me some credit in your About box.
  51.    I ask nothing more...
  52. */
  53.  
  54. #include "GWorld.h"
  55.  
  56. #define NIL        0L
  57.  
  58. /* for BitMap calls */
  59. #define kIsVisible TRUE
  60. #define kNoGoAway FALSE
  61. #define kNoWindowStorage 0L
  62. #define kFrontWindow ((WindowPtr) -1L)
  63.  
  64.  
  65. GDHandle        gOldDevice;
  66. CGrafPtr        gOldPort;
  67.  
  68.  
  69. void UseOffWorld(GWorldPtr offWorlder)
  70. {
  71.   GetGWorld(&gOldPort, &gOldDevice);
  72.   LockPixels(GetGWorldPixMap(offWorlder));
  73.   SetGWorld(offWorlder, NIL);
  74. }
  75.  
  76. void DoneWithOffWorld(GWorldPtr offWorlder)
  77. {
  78.   UnlockPixels(GetGWorldPixMap(offWorlder));
  79.   SetGWorld(gOldPort, gOldDevice);
  80. }
  81.  
  82. /* === BitMap routines === */
  83.  
  84. Boolean CreateOffscreenBitMap(GrafPtr *newOffscreen, Rect *inBounds)
  85. {
  86.   GrafPtr savePort;
  87.   GrafPtr newPort;
  88.  
  89.   GetPort(&savePort); /* need this to restore thePort after OpenPort */
  90.  
  91.   newPort = (GrafPtr)NewPtr(sizeof(GrafPort));/* allocate the grafPort */
  92.   if(MemError() != noErr)
  93.     return FALSE;                                            
  94.     /* failure to allocate off-screen port */
  95.  
  96.     /*    the call to OpenPort does the following:
  97.  
  98.         allocates space for visRgn (set to screenBits.bounds)
  99.         and clipRgn (set wide open)
  100.         sets portBits to screenBits
  101.         sets portRect to screenBits.bounds
  102.         etc. (see Inside Macintosh Volume 1 pages 163-164)
  103.         side effect: does a SetPort (&offScreen)
  104.     */
  105.  
  106.   OpenPort(newPort);
  107.  
  108.   /* make bitmap the size of the bounds that caller supplied */
  109.   newPort->portRect = *inBounds;
  110.   newPort->portBits.bounds = *inBounds;
  111.   RectRgn(newPort->clipRgn, inBounds);
  112.   RectRgn(newPort->visRgn, inBounds);
  113.   /* rowBytes is size of row, must be rounded up to an even number of bytes */
  114.  
  115.   newPort->portBits.rowBytes = ((inBounds->right - inBounds->left + 15) >> 4) << 1;
  116.  
  117.   /* number of bytes in BitMap is rowBytes * number of rows */
  118.   /* see notes at end of example about using NewPtr instead of NewHandle */
  119.   newPort->portBits.baseAddr = 
  120.     NewPtr(newPort->portBits.rowBytes * (long)(inBounds->bottom - inBounds->top));
  121.  
  122.   if(MemError() != noErr)
  123.   {
  124.     SetPort(savePort);
  125.     ClosePort(newPort);            /* dump the visRgn and clipRgn */
  126.     DisposPtr((Ptr)newPort);    /* dump the GrafPort */
  127.     return FALSE;    /* tell caller we failed */
  128.   }
  129.  
  130.   /* since the bits are just memory, let's clear them before we start */
  131.   EraseRect(inBounds);/* OpenPort did a SetPort(newPort) so we are OK*/
  132.   *newOffscreen = newPort;
  133.   SetPort(savePort);
  134.   return TRUE;                        /* success */
  135. }
  136.  
  137. /*
  138.     DestroyOffscreenBitMap - get rid of an off-screen bitmap created
  139.     by CreateOffscreenBitMap
  140. */
  141. void DestroyOffscreenBitMap(GrafPtr oldOffscreen)
  142. {
  143.   ClosePort(oldOffscreen);            /* dump the visRgn and clipRgn */
  144.   DisposPtr(oldOffscreen->portBits.baseAddr);    /* dump the bits */
  145.   DisposPtr((Ptr)oldOffscreen);            /* dump the port */
  146. }