home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 120.lha / AnimBalls / myblit.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  5KB  |  222 lines

  1. /* myblit.c - definition of my blitter code */
  2. /*    It will blit from a source through a mask to a destination bitmap */
  3. /*      The mask must have the word before and after it clear */
  4.  
  5. #include <exec/types.h>
  6. #ifdef BLTDEBUG
  7. #include <stdio.h>
  8. #endif
  9. #include <hardware/custom.h>
  10. #include <hardware/blit.h>
  11. #include <graphics/gfx.h>
  12.  
  13. #define BLTADD (0xdff040)
  14.  
  15. /*
  16.  *   This structure contains everything we need to know.
  17.  *   Do not do a structure copy into this!  Instead, assign
  18.  *   each field.  The last field assigned must be bltsize; that
  19.  *   starts up the blitter.  Also note that all of these are
  20.  *   write only, and you can't read them.
  21.  */
  22.  
  23. struct BltStruct {
  24.     WORD con0;
  25.     WORD con1;
  26.     WORD afwm;
  27.     WORD alwm;
  28.     WORD *csource, *bsource, *asource, *dsource;
  29.     WORD bltsize;
  30.     WORD dmy1, dmy2, dmy3;
  31.     WORD cmod, bmod, amod, dmod;
  32. };
  33. static struct BltStruct *Blitter = (struct BltStruct *)BLTADD;
  34.  
  35. static UBYTE boffset[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
  36.  
  37. void
  38. BltBitMask(sbm,sx,sy,dbm,dx,dy,mbm,mx,my,mpno,xsize,ysize)
  39. struct BitMap *sbm, *dbm, *mbm;
  40. int sx,sy,dx,dy,mx,my,mpno,xsize,ysize;
  41. {
  42.     WORD *splane, *dplane, *mplane;
  43.     WORD *aaddr,*baddr,*daddr;
  44.     int ashft, bshft;
  45.     UWORD afwm,alwm;
  46.     int smodulo, dmodulo, mmodulo;
  47.     int i;
  48.  
  49.     smodulo = ((sbm->BytesPerRow) + 1) >> 1;
  50.     dmodulo = ((dbm->BytesPerRow) + 1) >> 1;
  51.     mmodulo = ((mbm->BytesPerRow) + 1) >> 1;
  52.  
  53.     splane = (WORD *) (sbm->Planes[0]);
  54.     dplane = (WORD *) (dbm->Planes[0]);
  55.     mplane = (WORD *) (mbm->Planes[mpno]);
  56.  
  57.     daddr = dplane + dmodulo * dy + (dx >> 4);
  58.     dx &= 15;
  59.     aaddr = mplane + mmodulo * my + (mx >> 4);
  60.     mx &= 15;
  61.     baddr = splane + smodulo * sy + (sx >> 4);
  62.     sx &= 15;
  63. #ifdef BLTDEBUG
  64. printf("new - spl = %x, dpl = %x, mpl = %x\n",splane,dplane,mplane);
  65. printf("smod = %x, dmod = %x, mmod = %x\n",smodulo,dmodulo,mmodulo);
  66. printf("aa=%x, ba=%x, da=%x, (a)mx=%d, (b)sx=%d, dx=%d, xs=%d\n",
  67.     (aaddr - mplane - mmodulo * my),
  68.     (baddr - splane - smodulo * sy),
  69.     (daddr - dplane - dmodulo * dy),mx,sx,dx,xsize);
  70. #endif
  71.     ashft = dx - mx;
  72.     bshft = dx - sx;
  73.     afwm = ((unsigned) 0xffff) >> mx;
  74.     alwm = 0xffff << (16 - ((mx + xsize) & 15));
  75.     xsize = (xsize + dx + 15) >> 4;
  76.     if ((ashft < 0) || (bshft < 0)) {
  77.         daddr--;
  78.         xsize++;
  79.         if (ashft < 0) ashft += 16;
  80.         else aaddr--;
  81.         if (bshft < 0) bshft += 16;
  82.         else baddr--;
  83.     }
  84. #ifdef BLTDEBUG
  85. printf("aaddr = %x, baddr = %x, daddr = %x\n",aaddr,baddr,daddr);
  86. printf("aa=%x, ba=%x, da=%x, fw=%x, lw=%x, as=%d, bs=%d, xs=%d\n",
  87.     (aaddr - mplane - mmodulo * my),
  88.     (baddr - splane - smodulo * sy),
  89.     (daddr - dplane - dmodulo * dy),afwm,alwm,ashft,bshft,xsize);
  90. #endif
  91.     OwnBlitter();
  92.  
  93.     WaitBlit();
  94.     Blitter->asource = aaddr;
  95.     Blitter->bsource = baddr;
  96.     Blitter->csource = Blitter->dsource = daddr;
  97.     Blitter->amod = (mmodulo - xsize) << 1;
  98.     Blitter->bmod = (smodulo - xsize) << 1;
  99.     Blitter->cmod = Blitter->dmod = (dmodulo - xsize) << 1;
  100.     Blitter->afwm = afwm;
  101.     Blitter->alwm = alwm;
  102.     Blitter->con0 = (ashft << 12) | (0xf << 8) | 0xca;
  103.     Blitter->con1 = (bshft << 12);
  104.     Blitter->bltsize = (ysize << 6) | xsize;
  105.  
  106.     for (i = 1; i < dbm->Depth; i++) {
  107.         WaitBlit();
  108.         Blitter->asource = aaddr;
  109.         Blitter->bsource = baddr +
  110.             (((WORD *)sbm->Planes[i]) - ((WORD *)sbm->Planes[0]));
  111.         Blitter->csource = Blitter->dsource = daddr +
  112.             (((WORD *)dbm->Planes[i]) - ((WORD *)dbm->Planes[0]));
  113.         Blitter->bltsize = (ysize << 6) | xsize;
  114.     }
  115.  
  116.     DisownBlitter();
  117. }
  118.  
  119. void
  120. mywritepixel1(bm,x,y)
  121. struct BitMap *bm;
  122. int x,y;
  123. {
  124.     register int offset;
  125.     register UBYTE bits;
  126.  
  127.     offset = y * bm->BytesPerRow + (x >> 3);
  128.     bits = boffset[x & 7];
  129.  
  130.     bm->Planes[0][offset] |= bits;
  131. }
  132.  
  133. void
  134. mywritepixel(bm,x,y,color)
  135. struct BitMap *bm;
  136. int x,y;
  137. {
  138.     register int offset;
  139.     register int i;
  140.     UBYTE bits,nbits;
  141.  
  142.     offset = y * bm->BytesPerRow + (x >> 3);
  143.     bits = boffset[x & 7];
  144.     nbits = ~bits;
  145.  
  146.     for (i=0; i < bm->Depth; i++) {
  147.         if (color & 0x01) bm->Planes[i][offset] |= bits;
  148.         else bm->Planes[i][offset] &= nbits;
  149.         color >>= 1;
  150.     }
  151. }
  152.  
  153. void
  154. myblank(bm,y1,y2)
  155. struct BitMap *bm;
  156. int y1,y2;
  157. {
  158.     register int offset, bytes;
  159.     register int i;
  160.  
  161.     offset = y1 * bm->BytesPerRow;
  162.     bytes = (y2 - y1) * bm->BytesPerRow;
  163.  
  164.     for (i=0; i < bm->Depth; i++)
  165.         BltClear(&(bm->Planes[i][offset]),bytes,0);
  166. }
  167.  
  168. void
  169. mylinecopy(bm,starty,len,delta)
  170. struct BitMap *bm;
  171. int starty, len, delta;
  172. {
  173.     UBYTE *loc1, *loc2, *stop;
  174.     int i;
  175.  
  176.     for(i=0; i < bm->Depth; i++) {
  177.         loc1 = &(bm->Planes[i][starty*bm->BytesPerRow]);
  178.         stop = loc1 + len*bm->BytesPerRow;
  179.         loc2 = loc1 + delta*bm->BytesPerRow;
  180.         while (loc1 < stop) *(loc2++) = *(loc1++);
  181.     }
  182. }
  183.  
  184. void
  185. myblankc(bm,y1,y2,color)
  186. struct BitMap *bm;
  187. int y1,y2,color;
  188. {
  189.     register int i;
  190.     WORD *daddr;
  191.     int dmodulo;
  192.     
  193.     dmodulo = ((bm->BytesPerRow) + 1) >> 1;
  194.  
  195.     daddr = (WORD *) (bm->Planes[0]);
  196.     daddr += dmodulo * y1;
  197.  
  198.     OwnBlitter();
  199.     WaitBlit();
  200.  
  201.     Blitter->dsource = daddr;
  202.     Blitter->dmod = 0;
  203.     if (color & 1)
  204.         Blitter->con0 = (1 << 8) | (0xff);
  205.     else    Blitter->con0 = (1 << 8);
  206.     Blitter->bltsize = ((y2-y1) << 6) | dmodulo;
  207.     color >>= 1;
  208.     
  209.     for (i = 1; i < bm->Depth; i++) {
  210.         WaitBlit();
  211.         Blitter->dsource = daddr +
  212.             (((WORD *)bm->Planes[i]) - ((WORD *)bm->Planes[0]));
  213.         if (color & 1)
  214.             Blitter->con0 = (1 << 8) | (0xff);
  215.         else    Blitter->con0 = (1 << 8);
  216.         color >>= 1;
  217.         Blitter->bltsize = ((y2 - y1) << 6) | dmodulo;
  218.     }
  219.     
  220.     DisownBlitter();
  221. }
  222.