home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / asm / PJGRAPH.ZIP / CHAP06.1 < prev    next >
Encoding:
Text File  |  1989-09-26  |  7.9 KB  |  212 lines

  1. ; Program to illustrate one use of write mode 2 of the VGA and EGA by
  2. ; animating the image of an "A" drawn by copying it from a chunky
  3. ; bit-map in system memory to a planar bit-map in VGA or EGA memory.
  4. ;
  5. ; Assembled with MASM 5.0, linked with MS-LINK 3.60.
  6. ;
  7. ; By Michael Abrash 11/8/87
  8. ;
  9. stackseg        segment para stack 'STACK'
  10.         db      512 dup(0)
  11. stackseg        ends
  12.  
  13. SCREEN_WIDTH_IN_BYTES   equ     80
  14. DISPLAY_MEMORY_SEGMENT  equ     0a000h
  15. SC_INDEX        equ     3c4h    ;Sequence Controller Index register
  16. MAP_MASK        equ     2       ;index of Map Mask register
  17. GC_INDEX        equ     03ceh   ;Graphics Controller Index reg
  18. GRAPHICS_MODE   equ     5       ;index of Graphics Mode reg
  19. BIT_MASK        equ     8       ;index of Bit Mask reg
  20.  
  21. Data    segment para common 'DATA'
  22. ;
  23. ; Current location of "A" as it is animated across the screen.
  24. ;
  25. CurrentX        dw      ?
  26. CurrentY        dw      ?
  27. RemainingLength dw      ?
  28. ;
  29. ; Chunky bit-map image of a yellow "A" on a bright blue background
  30. ;
  31. AImage          label   byte
  32.                 dw      13, 13          ;width, height in pixels
  33.                 db      000h, 000h, 000h, 000h, 000h, 000h, 000h
  34.                 db      009h, 099h, 099h, 099h, 099h, 099h, 000h
  35.                 db      009h, 099h, 099h, 099h, 099h, 099h, 000h
  36.                 db      009h, 099h, 099h, 0e9h, 099h, 099h, 000h
  37.                 db      009h, 099h, 09eh, 0eeh, 099h, 099h, 000h
  38.                 db      009h, 099h, 0eeh, 09eh, 0e9h, 099h, 000h
  39.                 db      009h, 09eh, 0e9h, 099h, 0eeh, 099h, 000h
  40.                 db      009h, 09eh, 0eeh, 0eeh, 0eeh, 099h, 000h
  41.                 db      009h, 09eh, 0e9h, 099h, 0eeh, 099h, 000h
  42.                 db      009h, 09eh, 0e9h, 099h, 0eeh, 099h, 000h
  43.                 db      009h, 099h, 099h, 099h, 099h, 099h, 000h
  44.                 db      009h, 099h, 099h, 099h, 099h, 099h, 000h
  45.                 db      000h, 000h, 000h, 000h, 000h, 000h, 000h
  46. Data    ends
  47.  
  48. Code    segmentpara public 'CODE'
  49.         assume  cs:Code, ds:Data
  50. Start   proc    near
  51.         mov     ax,Data
  52.         mov     ds,ax
  53.         mov     ax,10h
  54.         int     10h             ;select video mode 10h (640x350)
  55. ;
  56. ; Prepare for animation.
  57. ;
  58.         mov     [CurrentX],0
  59.         mov     [CurrentY],200
  60.         mov     [RemainingLength],600   ;move 600 times
  61. ;
  62. ; Animate, repeating RemainingLength times. It's unnecessary to erase
  63. ; the old image, since the one pixel of blank fringe around the image
  64. ; erases the part of the old image not overlapped by the new image.
  65. ;
  66. AnimationLoop:
  67.         mov     bx,[CurrentX]
  68.         mov     cx,[CurrentY]
  69.         mov     si,offset AImage
  70.         call    DrawFromChunkyBitmap    ;draw the "A" image
  71.         inc     [CurrentX]              ;move one pixel to the right
  72.         dec     [RemainingLength]
  73.         jnz     AnimationLoop
  74. ;
  75. ; Wait for a key before returning to text mode and ending.
  76. ;
  77.         mov     ah,01h
  78.         int     21h
  79.         mov     ax,03h
  80.         int     10h
  81.         mov     ah,4ch
  82.         int     21h
  83. Start   endp
  84. ;
  85. ; Draw an image stored in a chunky-bit map into planar VGA/EGA memory
  86. ; at the specified location.
  87. ;
  88. ; Input:
  89. ;       BX = X screen location at which to draw the upper left corner
  90. ;               of the image
  91. ;       CX = Y screen location at which to draw the upper left corner
  92. ;               of the image
  93. ;       DS:SI = pointer to chunky image to draw, as follows:
  94. ;               word at 0: width of image, in pixels
  95. ;               word at 2: height of image, in pixels
  96. ;               byte at 4: msb/lsb = first & second chunky pixels,
  97. ;                       repeating for the remainder of the scan line
  98. ;                       of the image, then for all scan lines. Images
  99. ;                       with odd widths have an unused null nibble
  100. ;                       padding each scan line out to a byte width
  101. ;
  102. ; AX, BX, CX, DX, SI, DI, ES destroyed.
  103. ;
  104. DrawFromChunkyBitmap    proc    near
  105.         cld
  106. ;
  107. ; Select write mode 2.
  108. ;
  109.         mov     dx,GC_INDEX
  110.         mov     al,GRAPHICS_MODE
  111.         out     dx,al
  112.         inc     dx
  113.         mov     al,02h
  114.         out     dx,al
  115. ;
  116. ; Enable writes to all 4 planes.
  117. ;
  118.         mov     dx,SC_INDEX
  119.         mov     al,MAP_MASK
  120.         out     dx,al
  121.         inc     dx
  122.         mov     al,0fh
  123.         out     dx,al
  124. ;
  125. ; Point ES:DI to the display memory byte in which the first pixel
  126. ; of the image goes, with AH set up as the bit mask to access that
  127. ; pixel within the addressed byte.
  128. ;
  129.         mov     ax,SCREEN_WIDTH_IN_BYTES
  130.         mul     cx              ;offset of start of top scan line
  131.         mov     di,ax
  132.         mov     cl,bl
  133.         and     cl,111b
  134.         mov     ah,80h          ;set AH to the bit mask for the
  135.         shr     ah,cl           ; initial pixel
  136.         shr     bx,1
  137.         shr     bx,1
  138.         shr     bx,1            ;X in bytes
  139.         add     di,bx           ;offset of upper left byte of image
  140.         mov     bx,DISPLAY_MEMORY_SEGMENT
  141.         mov     es,bx           ;ES:DI points to the byte at which the
  142.                                 ; upper left of the image goes
  143. ;
  144. ; Get the width and height of the image.
  145. ;
  146.         mov     cx,[si]         ;get the width
  147.         inc     si
  148.         inc     si
  149.         mov     bx,[si]         ;get the height
  150.         inc     si
  151.         inc     si
  152.         mov     dx,GC_INDEX
  153.         mov     al,BIT_MASK
  154.         out     dx,al           ;leave the GC Index register pointing
  155.         inc     dx              ; to the Bit Mask register
  156. RowLoop:
  157.  
  158.         push    ax              ;preserve the left column's bit mask
  159.         push    cx              ;preserve the width
  160.         push    di              ;preserve the destination offset
  161.  
  162. ColumnLoop:
  163.         mov     al,ah
  164.         out     dx,al           ;set the bit mask to draw this pixel
  165.         mov     al,es:[di]      ;load the latches
  166.         mov     al,[si]         ;get the next two chunky pixels
  167.         shr     al,1
  168.         shr     al,1
  169.         shr     al,1
  170.         shr     al,1            ;move the first pixel into the lsb
  171.         stosb                   ;draw the first pixel
  172.         ror     ah,1            ;move mask to next pixel position
  173.         jc      CheckMorePixels ;is next pixel in the adjacent byte?
  174.         dec     di              ;no
  175.  
  176. CheckMorePixels:
  177.         dec     cx              ;see if there are any more pixels
  178.                                 ; across in image
  179.         jz      AdvanceToNextScanLine
  180.         mov     al,ah
  181.         out     dx,al           ;set the bit mask to draw this pixel
  182.         mov     al,es:[di]      ;load the latches
  183.         lodsb                   ;get the same two chunky pixels again
  184.                                 ; and advance pointer to the next
  185.                                 ; two pixels
  186.         stosb                   ;draw the second of the two pixels
  187.         ror     ah,1            ;move mask to next pixel position
  188.         jc      CheckMorePixels2 ;is next pixel in the adjacent byte?
  189.         dec     di              ;no
  190.  
  191. CheckMorePixels2:
  192.         loop    ColumnLoop      ;see if there are any more pixels
  193.                                 ; across in the image
  194.         jmp     short CheckMoreScanLines
  195.  
  196. AdvanceToNextScanLine:
  197.         inc     si              ;advance to the start of the next
  198.                                 ; scan line in the image
  199.  
  200. CheckMoreScanLines:
  201.         pop     di              ;get back the destination offset
  202.         pop     cx              ;get back the width
  203.         pop     ax              ;get back the left column's bit mask
  204.         add     di,SCREEN_WIDTH_IN_BYTES
  205.                                 ;point to the start of the next scan
  206.                                 ; line of the image
  207.         dec     bx              ;see if there are any more scan lines
  208.         jnz     RowLoop         ; in the image
  209.         ret
  210. DrawFromChunkyBitmap    endp
  211. Code    ends
  212.         end     Start