home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Concentration / Lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  3.7 KB  |  168 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Concentration library routines
  3.  */
  4.  
  5. # include    "TransSkel.h"
  6. # if ENABLE_DEBUG
  7. # include    "Debug.h"
  8. # endif
  9.  
  10. # include    "Concentration.h"
  11.  
  12.  
  13. /* ---------------------------------------------------------------- */
  14. /*                            Blob Operations                            */
  15. /* ---------------------------------------------------------------- */
  16.  
  17.  
  18.  
  19. /*
  20.  * Blobs are used in a way that may not be immediately obvious.
  21.  *
  22.  * Procedure blobs are used.  A blob is either an ICON or a SICN item
  23.  * (which comprises the drag region), with a frame around it (which
  24.  * comprises the static region).  The drawing procedure is called to
  25.  * draw one region at a time.   If the region to be drawn is the static 
  26.  * region, then just the frame is drawn.  If the region to be drawn
  27.  * is the drag region, then it's drawn either black (piece is covered)
  28.  * or the ICON/SICN item is drawn.  A piece is considered covered when
  29.  * it's glued to itself.  To uncover it, it's unglued from itself.
  30.  * Thus, the drawing procedure need only check whether the blob
  31.  * has a glob or not, and respond appropriately.  If the blob is
  32.  * uncovered, the ICON or SICN item to draw is indicated by the
  33.  * reference constant.
  34.  */
  35.  
  36.  
  37. # define    kFrameInset    (-2)
  38.  
  39.  
  40. /*
  41.  * Draw an icon blob
  42.  *
  43.  * bDst        blob to draw in
  44.  * bSrc        blob to be drawn in bDst
  45.  */
  46.  
  47. static pascal void
  48. DrawIconBlob (BlobHandle bDst, BlobHandle bSrc, short partCode)
  49. {
  50. Rect    r;
  51. Piece    *p;
  52.  
  53.     if (partCode ==inStatBlob)
  54.     {
  55.         r = BStatBox (bDst);
  56.         FrameRect (&r);
  57.     }
  58.     else
  59.     {
  60.         r = BDragBox (bDst);
  61.         if (BGlob (bDst) != nil)    /* covered */
  62.             PaintRect (&r);
  63.         else                        /* draw image according to resource type */
  64.         {
  65.             p = &gb->piece[GetBRefCon (bDst)];
  66.             switch (p->pieceType)
  67.             {
  68.             case iconType:
  69.                 PlotIcon (&r, p->pieceData);
  70.                 break;
  71.             case cicnType:
  72.                 EraseRect (&r);
  73.                 PlotCIcon (&r, (CIconHandle) p->pieceData);
  74.                 break;
  75.             default:
  76.                 Bail ("\pDrawIconBlob: unknown resource type");
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84.  * Plot SICN item in given rectangle.
  85.  * This is exactly analogous to the ToolBox PlotIcon routine,
  86.  * except that the item number within the SICN must be specified.
  87.  */
  88.  
  89.  
  90. static void
  91. PlotSicn (Rect *r, SitmHandle s)
  92. {
  93. GrafPtr    thePort;
  94. BitMap    bm;
  95.  
  96.     /* create a small bitmap that points to the SICN item */
  97.     HLock ((Handle) s);
  98.     bm.baseAddr = (Ptr) *s;
  99.     bm.rowBytes = sitmSize / 8;            /* items are 2 bytes wide */
  100.     SetRect (&bm.bounds, 0, 0, sitmSize, sitmSize);
  101.     GetPort (&thePort);
  102.     CopyBits (&bm, &thePort->portBits, &bm.bounds, r, srcCopy, nil);
  103.     HUnlock ((Handle) s);
  104. }
  105.  
  106.  
  107. static pascal void
  108. DrawSicnBlob (BlobHandle bDst, BlobHandle bSrc, short partCode)
  109. {
  110. Rect    r;
  111. Piece    *p;
  112.  
  113.     if (partCode == inStatBlob)
  114.     {
  115.         r = BStatBox (bDst);
  116.         FrameRect (&r);
  117.     }
  118.     else
  119.     {
  120.         r = BDragBox (bDst);
  121.         if (BGlob (bDst) != nil)        /* covered */
  122.             PaintRect (&r);
  123.         else
  124.         {
  125.             p = &gb->piece[GetBRefCon (bDst)];
  126.             PlotSicn (&r, (SitmHandle) p->pieceData);
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. /*
  133.  * Make a blob to display an icon or sicn item with a frame around it.
  134.  * Pass the blob set to attach the blob to, and the horizontal and
  135.  * vertical coordinates of the upper left of the drag region.  (The
  136.  * static region is drawn outside that.)  The reference number of the
  137.  * icon or sicn item to draw in the blob is assigned later.
  138.  */
  139.  
  140. void
  141. MakeBlob (BlobSetHandle bSet, short h, short v, short sizeType)
  142. {
  143. BlobHandle    b;
  144. Rect        dr, sr;        /* drag, static rectangles */
  145. short        size;
  146. BDrawProcPtr    p;
  147.  
  148.     switch (sizeType)
  149.     {
  150.     case largePiece:
  151.         size = iconSize;
  152.         p = DrawIconBlob;
  153.         break;
  154.     case smallPiece:
  155.         size = sitmSize;
  156.         p = DrawSicnBlob;
  157.         break;
  158.     default:
  159.         Bail ("\pMakeBlob: unknown blob type.");
  160.     }
  161.     
  162.     b = NewBlob (bSet, false, infiniteGlue, false, 0L);
  163.     SetRect (&dr, h, v, h + size, v + size);
  164.     sr = dr;
  165.     InsetRect (&sr, kFrameInset, kFrameInset);        /* for frame around image */
  166.     SetProcRectBlob (b, p, &dr, &sr);
  167. }
  168.