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

  1. ; Mode X (320x240, 256 colors) rectangle fill routine. Works on all
  2. ; VGAs. Uses fast approach that fans data out to up to four planes at
  3. ; once to draw up to four pixels at once. Fills up to but not
  4. ; including the column at EndX and the row at EndY. No clipping is
  5. ; performed.
  6. ; Tested with TASM 4.0 by Jim Mischel 12/16/94.
  7. ; C near-callable as:
  8. ;    void FillRectangleX(int StartX, int StartY, int EndX, int EndY,
  9. ;       unsigned int PageBase, int Color);
  10.  
  11. SC_INDEX equ    03c4h   ;Sequence Controller Index
  12. MAP_MASK equ    02h     ;index in SC of Map Mask register
  13. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  14. SCREEN_WIDTH equ 80     ;width of screen in bytes from one scan line
  15.                         ; to the next
  16. parms   struc
  17.         dw      2 dup (?) ;pushed BP and return address
  18. StartX  dw      ?       ;X coordinate of upper left corner of rect
  19. StartY  dw      ?       ;Y coordinate of upper left corner of rect
  20. EndX    dw      ?       ;X coordinate of lower right corner of rect
  21.                         ; (the row at EndX is not filled)
  22. EndY    dw      ?       ;Y coordinate of lower right corner of rect
  23.                         ; (the column at EndY is not filled)
  24. PageBase dw     ?       ;base offset in display memory of page in
  25.                         ; which to fill rectangle
  26. Color   dw      ?       ;color in which to draw pixel
  27. parms   ends
  28.  
  29.         .model  small
  30.         .data
  31. ; Plane masks for clipping left and right edges of rectangle.
  32. LeftClipPlaneMask       db      00fh,00eh,00ch,008h
  33. RightClipPlaneMask      db      00fh,001h,003h,007h
  34.         .code
  35.         public  _FillRectangleX
  36. _FillRectangleX proc    near
  37.         push    bp      ;preserve caller's stack frame
  38.         mov     bp,sp   ;point to local stack frame
  39.         push    si      ;preserve caller's register variables
  40.         push    di
  41.  
  42.         cld
  43.         mov     ax,SCREEN_WIDTH
  44.         mul     [bp+StartY] ;offset in page of top rectangle scan line
  45.         mov     di,[bp+StartX]
  46.         shr     di,1    ;X/4 = offset of first rectangle pixel in scan
  47.         shr     di,1    ; line
  48.         add     di,ax   ;offset of first rectangle pixel in page
  49.         add     di,[bp+PageBase] ;offset of first rectangle pixel in
  50.                         ; display memory
  51.         mov     ax,SCREEN_SEG   ;point ES:DI to the first rectangle
  52.         mov     es,ax           ; pixel's address
  53.         mov     dx,SC_INDEX ;set the Sequence Controller Index to
  54.         mov     al,MAP_MASK ; point to the Map Mask register
  55.         out     dx,al
  56.         inc     dx      ;point DX to the SC Data register
  57.         mov     si,[bp+StartX]
  58.         and     si,0003h                 ;look up left edge plane mask
  59.         mov     bh,LeftClipPlaneMask[si] ; to clip & put in BH
  60.         mov     si,[bp+EndX]
  61.         and     si,0003h                  ;look up right edge plane
  62.         mov     bl,RightClipPlaneMask[si] ; mask to clip & put in BL
  63.         
  64.         mov     cx,[bp+EndX]    ;calculate # of addresses across rect
  65.         mov     si,[bp+StartX]
  66.         cmp     cx,si
  67.         jle     FillDone        ;skip if 0 or negative width
  68.         dec     cx
  69.         and     si,not 011b
  70.         sub     cx,si
  71.         shr     cx,1
  72.         shr     cx,1    ;# of addresses across rectangle to fill - 1
  73.         jnz     MasksSet ;there's more than one byte to draw
  74.         and     bh,bl   ;there's only one byte, so combine the left
  75.                         ; and right edge clip masks
  76. MasksSet:
  77.         mov     si,[bp+EndY]
  78.         sub     si,[bp+StartY]  ;BX = height of rectangle
  79.         jle     FillDone        ;skip if 0 or negative height
  80.         mov     ah,byte ptr [bp+Color] ;color with which to fill
  81.         mov     bp,SCREEN_WIDTH ;stack frame isn't needed any more
  82.         sub     bp,cx   ;distance from end of one scan line to start
  83.         dec     bp      ; of next
  84. FillRowsLoop:
  85.         push    cx      ;remember width in addresses - 1
  86.         mov     al,bh   ;put left-edge clip mask in AL
  87.         out     dx,al   ;set the left-edge plane (clip) mask
  88.         mov     al,ah   ;put color in AL
  89.         stosb           ;draw the left edge
  90.         dec     cx      ;count off left edge byte
  91.         js      FillLoopBottom ;that's the only byte
  92.         jz      DoRightEdge ;there are only two bytes
  93.         mov     al,00fh ;middle addresses are drawn 4 pixels at a pop
  94.         out     dx,al   ;set the middle pixel mask to no clip
  95.         mov     al,ah   ;put color in AL
  96.         rep     stosb   ;draw the middle addresses four pixels apiece
  97. DoRightEdge:
  98.         mov     al,bl   ;put right-edge clip mask in AL
  99.         out     dx,al   ;set the right-edge plane (clip) mask
  100.         mov     al,ah   ;put color in AL
  101.         stosb           ;draw the right edge
  102. FillLoopBottom:
  103.         add     di,bp   ;point to the start of the next scan line of
  104.                         ; the rectangle
  105.         pop     cx      ;retrieve width in addresses - 1
  106.         dec     si      ;count down scan lines
  107.         jnz     FillRowsLoop
  108. FillDone:
  109.         pop     di      ;restore caller's register variables
  110.         pop     si
  111.         pop     bp      ;restore caller's stack frame
  112.         ret
  113. _FillRectangleX endp
  114.         end
  115.  
  116.