home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter41 / l41-3.asm < prev    next >
Assembly Source File  |  1997-06-18  |  4KB  |  93 lines

  1. ; Draws all pixels in list of horizontal lines passed in, in mode 13h, VGA's 
  2. ; 320x200 256-color mode. Uses REP STOS to fill each line.
  3. ; ******************************************************************
  4. ; NOTE: is able to reverse the X coords for a scan line, if necessary to make 
  5. ; XStart < XEnd. Expects whichever edge is rightmost on any scan line to be in
  6. ; +1 format; that is, XEnd is 1 greater than rightmost pixel to draw. if 
  7. ; XStart == XEnd, nothing is drawn on that scan line.
  8. ; ******************************************************************
  9. ; C near-callable as:
  10. ;     void DrawHorizontalLineList(struct HLineList * HLineListPtr, int Color);
  11. ; Tested with TASM 4.0 by Jim Mischel 12/16/94.
  12. ; See build instructions in L24-1.C.
  13.  
  14. SCREEN_WIDTH    equ     320
  15. SCREEN_SEGMENT  equ     0a000h
  16.  
  17. HLine   struc
  18. XStart  dw      ?       ;X coordinate of leftmost pixel in line
  19. XEnd    dw      ?       ;X coordinate of rightmost pixel in line
  20. HLine   ends
  21.  
  22. HLineList struc
  23. Lngth   dw      ?       ;# of horizontal lines
  24. YStart  dw      ?       ;Y coordinate of topmost line
  25. HLinePtr dw     ?       ;pointer to list of horz lines
  26. HLineList ends
  27.  
  28. Parms   struc
  29.                 dw      2 dup(?) ;return address & pushed BP
  30. HLineListPtr    dw      ?       ;pointer to HLineList structure
  31. Color           dw      ?       ;color with which to fill
  32. Parms   ends
  33.         .model small
  34.         .code
  35.         public _DrawHorizontalLineList
  36.         align   2
  37. _DrawHorizontalLineList proc
  38.         push    bp              ;preserve caller's stack frame
  39.         mov     bp,sp           ;point to our stack frame
  40.         push    si              ;preserve caller's register variables
  41.         push    di
  42.         cld                     ;make string instructions inc pointers
  43.  
  44.         mov     ax,SCREEN_SEGMENT
  45.         mov     es,ax   ;point ES to display memory for REP STOS
  46.  
  47.         mov     si,[bp+HLineListPtr] ;point to the line list
  48.         mov     ax,SCREEN_WIDTH ;point to the start of the first scan
  49.         mul     [si+YStart]     ; line in which to draw
  50.         mov     dx,ax           ;ES:DX points to first scan line to draw
  51.         mov     bx,[si+HLinePtr] ;point to the XStart/XEnd descriptor
  52.                                 ; for the first (top) horizontal line
  53.         mov     si,[si+Lngth]   ;# of scan lines to draw
  54.         and     si,si           ;are there any lines to draw?
  55.         jz      FillDone        ;no, so we're done
  56.         mov     al,byte ptr [bp+Color] ;color with which to fill
  57.         mov     ah,al           ;duplicate color for STOSW
  58. FillLoop:
  59.         mov     di,[bx+XStart]  ;left edge of fill on this line
  60.         mov     cx,[bx+XEnd]    ;right edge of fill
  61.         cmp     di,cx           ;is XStart > XEnd?
  62.         jle     NoSwap          ;no, we're all set
  63.         xchg    di,cx           ;yes, so swap edges
  64. NoSwap:
  65.         sub     cx,di           ;width of fill on this line
  66.         jz      LineFillDone    ;skip if zero width
  67.         add     di,dx           ;offset of left edge of fill
  68.         test    di,1            ;does fill start at an odd address?
  69.         jz      MainFill        ;no
  70.         stosb                   ;yes, draw the odd leading byte to
  71.                                 ; word-align the rest of the fill
  72.         dec     cx              ;count off the odd leading byte
  73.         jz      LineFillDone    ;done if that was the only byte
  74. MainFill:
  75.         shr     cx,1            ;# of words in fill
  76.         rep     stosw           ;fill as many words as possible
  77.         adc     cx,cx           ;1 if there's an odd trailing byte to
  78.                                 ; do, 0 otherwise
  79.         rep     stosb           ;fill any odd trailing byte
  80. LineFillDone:
  81.         add     bx,size HLine   ;point to the next line descriptor
  82.         add     dx,SCREEN_WIDTH ;point to the next scan line
  83.         dec     si              ;count off lines to fill
  84.         jnz     FillLoop
  85. FillDone:
  86.         pop     di              ;restore caller's register variables
  87.         pop     si
  88.         pop     bp              ;restore caller's stack frame
  89.         ret
  90. _DrawHorizontalLineList endp
  91.         end
  92.  
  93.