home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter46 / l46-2.asm < prev    next >
Assembly Source File  |  1997-06-18  |  6KB  |  147 lines

  1. ; Assembly language helper routines for dirty rectangle animation.
  2. ; Tested with TASM 4.0 by Jim Mischel 12/16/94.
  3. ; Fills a rectangle in the specified buffer. C-callable as:  
  4. ;
  5. ;  void FillRect(char far * BufferPtr, int RectHeight, int RectWidth,
  6. ;                   int BufferWidth, int Color);
  7. ;
  8.         .model  small
  9.         .code
  10. parms   struc
  11.                 dw      ?       ;pushed BP
  12.                 dw      ?       ;pushed return address
  13. BufferPtr       dd      ?       ;far pointer to buffer in which to fill
  14. RectHeight      dw      ?       ;height of rectangle to fill
  15. RectWidth       dw      ?       ;width of rectangle to fill
  16. BufferWidth     dw      ?       ;width of buffer in which to fill
  17. Color           dw      ?       ;color with which to fill
  18. parms   ends
  19.         public  _FillRect
  20. _FillRect   proc  near
  21.         cld
  22.         push    bp
  23.         mov     bp,sp
  24.         push    di
  25.  
  26.         les     di,[bp+BufferPtr]
  27.         mov     dx,[bp+RectHeight]
  28.         mov     bx,[bp+BufferWidth]
  29.         sub     bx,[bp+RectWidth]       ;distance from end of one dest scan
  30.                                         ; to start of next
  31.         mov     al,byte ptr [bp+Color]
  32.         mov     ah,al                   ;double the color for REP STOSW
  33. RowLoop:
  34.         mov     cx,[bp+RectWidth]
  35.         shr     cx,1
  36.         rep     stosw
  37.         adc     cx,cx
  38.         rep     stosb
  39.         add     di,bx                   ;point to next scan to fill
  40.         dec     dx                      ;count down rows to fill
  41.         jnz     RowLoop
  42.  
  43.         pop     di
  44.         pop     bp
  45.         ret
  46. _FillRect   endp
  47.  
  48. ; Draws a masked image (a sprite) to the specified buffer. C-callable as:
  49. ;     void DrawMasked(char far * BufferPtr, char * Pixels, char * Mask,
  50. ;                   int ImageHeight, int ImageWidth, int BufferWidth);
  51. parms2  struc
  52.                 dw      ?       ;pushed BP
  53.                 dw      ?       ;pushed return address
  54. BufferPtr2      dd      ?       ;far pointer to buffer in which to draw
  55. Pixels          dw      ?       ;pointer to image pixels
  56. Mask            dw      ?       ;pointer to image mask
  57. ImageHeight     dw      ?       ;height of image to draw
  58. ImageWidth      dw      ?       ;width of image to draw
  59. BufferWidth2    dw      ?       ;width of buffer in which to draw
  60. parms2  ends
  61.         public  _DrawMasked
  62. _DrawMasked     proc    near
  63.         cld
  64.         push    bp
  65.         mov     bp,sp
  66.         push    si
  67.         push    di
  68.  
  69.         les     di,[bp+BufferPtr2]
  70.         mov     si,[bp+Mask]
  71.         mov     bx,[bp+Pixels]
  72.         mov     dx,[bp+ImageHeight]
  73.         mov     ax,[bp+BufferWidth2]
  74.         sub     ax,[bp+ImageWidth]      ;distance from end of one dest scan
  75.         mov     [bp+BufferWidth2],ax    ; to start of next
  76. RowLoop2:
  77.         mov     cx,[bp+ImageWidth]
  78. ColumnLoop:
  79.         lodsb                           ;get the next mask byte
  80.         and     al,al                   ;draw this pixel?
  81.         jz      SkipPixel               ;no
  82.         mov     al,[bx]                 ;yes, draw the pixel
  83.         mov     es:[di],al
  84. SkipPixel:
  85.         inc     bx                      ;point to next source pixel
  86.         inc     di                      ;point to next dest pixel
  87.         dec     cx
  88.         jnz     ColumnLoop
  89.         add     di,[bp+BufferWidth2]    ;point to next scan to fill
  90.         dec     dx                      ;count down rows to fill
  91.         jnz     RowLoop2
  92.  
  93.         pop     di
  94.         pop     si
  95.         pop     bp
  96.         ret
  97. _DrawMasked     endp
  98.  
  99. ; Copies a rectangle from one buffer to another. C-callable as:
  100. ;     void CopyRect(DestBufferPtr, SrcBufferPtr, CopyHeight, CopyWidth,
  101. ;                   DestBufferWidth, SrcBufferWidth);
  102.  
  103. parms3  struc
  104.                 dw      ?       ;pushed BP
  105.                 dw      ?       ;pushed return address
  106. DestBufferPtr   dd      ?       ;far pointer to buffer to which to copy
  107. SrcBufferPtr    dd      ?       ;far pointer to buffer from which to copy
  108. CopyHeight      dw      ?       ;height of rect to copy
  109. CopyWidth       dw      ?       ;width of rect to copy
  110. DestBufferWidth dw      ?       ;width of buffer to which to copy
  111. SrcBufferWidth  dw      ?       ;width of buffer from which to copy
  112. parms3  ends
  113.         public  _CopyRect
  114. _CopyRect       proc    near
  115.         cld
  116.         push    bp
  117.         mov     bp,sp
  118.         push    si
  119.         push    di
  120.         push    ds
  121.  
  122.         les     di,[bp+DestBufferPtr]
  123.         lds     si,[bp+SrcBufferPtr]
  124.         mov     dx,[bp+CopyHeight]
  125.         mov     bx,[bp+DestBufferWidth] ;distance from end of one dest scan
  126.         sub     bx,[bp+CopyWidth]       ; of copy to the next
  127.         mov     ax,[bp+SrcBufferWidth]  ;distance from end of one source scan
  128.         sub     ax,[bp+CopyWidth]       ; of copy to the next
  129. RowLoop3:
  130.         mov     cx,[bp+CopyWidth]       ;# of bytes to copy
  131.         shr     cx,1
  132.         rep     movsw                   ;copy as many words as possible
  133.         adc     cx,cx
  134.         rep     movsb                   ;copy odd byte, if any
  135.         add     si,ax                   ;point to next source scan line
  136.         add     di,bx                   ;point to next dest scan line
  137.         dec     dx                      ;count down rows to fill
  138.         jnz     RowLoop3
  139.  
  140.         pop     ds
  141.         pop     di
  142.         pop     si
  143.         pop     bp
  144.         ret
  145. _CopyRect       endp
  146.         end
  147.