home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 274.lha / SimGen_Src / STENCIL.c < prev    next >
C/C++ Source or Header  |  1989-07-26  |  3KB  |  154 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <functions.h>
  5. #include "pdefines.h"
  6. #include "globals.h"
  7.  
  8. #define    CHEAT    1
  9.  
  10. struct Picture *CreateStencilPic (rp, stencilcolor)
  11.     struct RastPort *rp;
  12.     UBYTE  stencilcolor;
  13. {
  14.     struct    RastPort *mrp;
  15.     struct    Picture *newpic;
  16.     WORD    height, width;
  17.     WORD    left, top;
  18.     WORD    depth;
  19.     int    i;
  20.     UBYTE    oldmask;
  21.  
  22.     if (( newpic = (struct Picture *)AllocMem (sizeof (struct Picture), MEMF_CLEAR)) == NULL) {
  23.         SYSMESS ("Couldn't allocate an Picture");
  24.         return (NULL);
  25.     }
  26.  
  27.     InitRastPort (&newpic->RastPort);
  28.     newpic->RastPort.BitMap = &newpic->BitMap;
  29.  
  30.     if (rp->Layer) {
  31.         left   = rp->Layer->bounds.MinX;
  32.         top    = rp->Layer->bounds.MinY;
  33.         width  = rp->Layer->bounds.MaxX - left + 1;
  34.         height = rp->Layer->bounds.MaxY - top + 1;
  35.     } else {
  36.         left   = 0;
  37.         top    = 0;
  38.         width  = rp->BitMap->BytesPerRow * 8;
  39.         height = rp->BitMap->Rows;
  40.     }
  41.     depth = rp->BitMap->Depth;
  42.  
  43.     newpic->ilbmframe.bmHdr.nPlanes = depth;    
  44.     newpic->ilbmframe.bmHdr.w = width;
  45.     newpic->ilbmframe.bmHdr.h = height;
  46.  
  47.     InitBitMap (&newpic->BitMap, rp->BitMap->Depth, width, height);
  48.  
  49.     if (!(newpic->BitMap.Planes[0] = AllocRaster(width, height))) {
  50.         DeleteStencilPic (newpic);
  51.         SYSMESS ("Couldn't allocate stencil mem");
  52.         return NULL;
  53.     }
  54.  
  55.     for (i=1; i<depth; i++) {
  56.         newpic->BitMap.Planes[i] = newpic->BitMap.Planes[0];
  57.     }
  58.  
  59.     mrp = &newpic->RastPort;
  60.     oldmask   = mrp->Mask;
  61.     mrp->Mask = 0x01;
  62.     ClipCopy (rp, left, top,
  63.           newpic, 0, 0, width, height, 0xFF, USECLIPBLIT);
  64.     mrp->Mask = stencilcolor;
  65.     ClipCopy (rp, left, top,
  66.           newpic, 0, 0, width, height, 0x80, USECLIPBLIT);
  67.     mrp->Mask = 0xFF^stencilcolor;
  68.     ClipCopy (rp, left, top,
  69.           newpic, 0, 0, width, height, 0x20, USECLIPBLIT);
  70.     mrp->Mask = oldmask;
  71.  
  72.     return (newpic);
  73. }    
  74.  
  75. DeleteStencilPic (pic)
  76. struct Picture *pic;
  77. {
  78.     if (pic) {
  79.         if (pic->BitMap.Planes[0]) {
  80.             FreeMem (pic->BitMap.Planes[0],
  81.                  pic->BitMap.BytesPerRow * 
  82.                  pic->BitMap.Rows);
  83.         }
  84.         FreeMem (pic, sizeof (struct Picture));
  85.     }
  86. }
  87.  
  88. struct Shape *CreateStencil (rp, color)
  89.     struct RastPort *rp;
  90.     UBYTE color;
  91. {
  92.     struct Shape *newshape;
  93.     struct Picture *newpic;
  94.     struct BitMap *bm;
  95.     int depth, width, height;
  96.     int i;
  97.     UBYTE tcolor;
  98.  
  99.     if (( newshape = (struct Shape *)AllocMem (sizeof (struct Shape), MEMF_CLEAR)) == NULL) {
  100.         SYSMESS ("Couldn't allocate a Shape");
  101.         return (NULL);
  102.     }
  103.  
  104.     if (!( newpic = CreateStencilPic (rp, color))) {
  105.         DeleteStencil (newshape);
  106.         return (NULL);
  107.     }
  108.  
  109.     newshape->Pic = newpic;
  110.  
  111.     depth  = newpic->ilbmframe.bmHdr.nPlanes;    
  112.     width  = newpic->ilbmframe.bmHdr.w;
  113.     height = newpic->ilbmframe.bmHdr.h;
  114.     tcolor = newpic->ilbmframe.bmHdr.transparentColor;
  115.  
  116.     /*** Create a bitmap for mask ***/
  117.  
  118.     bm = &newshape->MaskBm;
  119.  
  120.     InitBitMap (bm, depth, width, height);
  121.  
  122.     bm->Planes[0] = newpic->BitMap.Planes[0];
  123.  
  124.     for (i = 1; i < depth; i++) {
  125.         bm->Planes[i] = bm->Planes[0];
  126.     }
  127.  
  128. #if NOT CHEAT
  129.     /*** the mask is inverted so fix it            ***/
  130.     /*** Now the mask will have ones were the shapes are    ***/
  131.     /*** and zeros everywhere else                ***/
  132.     BltBitMap (bm, 0, 0,
  133.            bm, 0, 0,
  134.            width, height,
  135.            0x30, 0x01, NULL);
  136.  
  137. #endif
  138.     /*** Finally Create a RastPort for the mask ***/
  139.  
  140.     InitRastPort (&newshape->MaskRp);
  141.     newshape->MaskRp.BitMap = bm;
  142.  
  143.     return (newshape);
  144. }
  145.  
  146. DeleteStencil (shape)
  147. struct Shape *shape;
  148. {
  149.     if (shape) {
  150.         DeleteStencilPic (shape->Pic);
  151.         FreeMem (shape, sizeof (struct Shape));
  152.     }
  153. }
  154.