home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / zendisk2.zip / LST11-33.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  7KB  |  250 lines

  1. ;
  2. ; *** Listing 11-33 ***
  3. ;
  4. ; Illustrates animation based on exclusive-oring.
  5. ; Animates 10 images at once.
  6. ; Not a general animation implementation, but rather an
  7. ; example of the strengths and weaknesses of exclusive-or
  8. ; based animation.
  9. ;
  10. ; Make with LZTIME.BAT, since this program is too long to be
  11. ; handled by the precision Zen timer.
  12. ;
  13.     jmp    Skip
  14. ;
  15. DELAY    equ    0        ;set to higher values to
  16.                 ; slow down for closer
  17.                 ; observation
  18. REPETITIONS equ    500        ;# of times to move and
  19.                 ; redraw the images
  20. DISPLAY_SEGMENT    equ    0b800h    ;display memory segment
  21.                 ; in 320x200 4-color
  22.                 ; graphics mode
  23. SCREEN_WIDTH    equ    80    ;# of bytes per scan line
  24. BANK_OFFSET    equ    2000h    ;offset from the bank
  25.                 ; containing the even-
  26.                 ; numbered lines on the
  27.                 ; screen to the bank
  28.                 ; containing the odd-
  29.                 ; numbered lines
  30. ;
  31. ; Used to count down # of times images are moved.
  32. ;
  33. RepCount    dw    REPETITIONS
  34. ;
  35. ; Complete info about one image that we're animating.
  36. ;
  37. Image    struc
  38. XCoord        dw    ?    ;image X location in pixels
  39. XInc        dw    ?    ;# of pixels to increment
  40.                 ; location by in the X
  41.                 ; direction on each move
  42. YCoord        dw    ?    ;image Y location in pixels
  43. YInc        dw    ?    ;# of pixels to increment
  44.                 ; location by in the Y
  45.                 ; direction on each move
  46. Image    ends
  47. ;
  48. ; List of images to animate.
  49. ;
  50. Images    label    Image
  51.     Image    <64,4,8,4>
  52.     Image    <144,0,56,2>
  53.     Image    <224,-4,104,0>
  54.     Image    <64,4,152,-2>
  55.     Image    <144,0,8,-4>
  56.     Image    <224,-4,56,-2>
  57.     Image    <64,4,104,0>
  58.     Image    <144,0,152,2>
  59.     Image    <224,-4,8,4>
  60.     Image    <64,4,56,2>
  61. ImagesEnd    label    Image
  62. ;
  63. ; Pixel pattern for the one image this program draws,
  64. ; a 32x32 3-color square.
  65. ;
  66. TheImage    label    byte
  67.     rept    32
  68.     dw    0ffffh, 05555h, 0aaaah, 0ffffh
  69.     endm
  70. IMAGE_HEIGHT    equ    32    ;# of rows in the image
  71. IMAGE_WIDTH    equ    8    ;# of bytes across the image
  72. ;
  73. ; Exclusive-ors the image of a 3-color square at the
  74. ; specified screen location. Assumes images start on
  75. ; even-numbered scan lines and are an even number of
  76. ; scan lines high. Always draws images byte-aligned in
  77. ; display memory.
  78. ;
  79. ; Input:
  80. ;    CX = X coordinate of upper left corner at which to
  81. ;        draw image (will be adjusted to nearest
  82. ;        less-than or equal-to multiple of 4 in order
  83. ;        to byte-align)
  84. ;    DX = Y coordinate of upper left corner at which to
  85. ;        draw image
  86. ;    ES = display memory segment
  87. ;
  88. ; Output: none
  89. ;
  90. ; Registers altered: AX, CX, DX, SI, DI, BP
  91. ;
  92. XorImage:
  93.     push    bx    ;preserve the main loop's pointer
  94.     shr    dx,1    ;divide the row # by 2 to compensate
  95.             ; for the 2-bank nature of 320x200
  96.             ; 4-color mode
  97.     mov    ax,SCREEN_WIDTH
  98.     mul    dx    ;start offset of top row of image in
  99.             ; display memory
  100.     shr    cx,1    ;divide the X coordinate by 4
  101.     shr    cx,1    ; because there are 4 pixels per
  102.             ; byte
  103.     add    ax,cx    ;point to the offset at which the
  104.             ; upper left byte of the image will
  105.             ; go
  106.     mov    di,ax
  107.     mov    si,offset TheImage
  108.             ;point to the start of the one image
  109.             ; we always draw
  110.     mov    bx,BANK_OFFSET-IMAGE_WIDTH
  111.             ;offset from the end of an even line
  112.             ; of the image in display memory to
  113.             ; the start of the next odd line of
  114.             ; the image
  115.     mov    dx,IMAGE_HEIGHT/2
  116.             ;# of even/odd numbered row pairs to
  117.             ;  draw in the image
  118.     mov    bp,IMAGE_WIDTH/2
  119.             ;# of words to draw per row of the
  120.             ; image. Note that IMAGE_WIDTH must
  121.             ; be an even number since we XOR
  122.             ; the image a word at a time
  123. XorRowLoop:
  124.     mov    cx,bp    ;# of words to draw per row of the
  125.             ; image
  126. XorColumnLoopEvenRows:
  127.     lodsw        ;next word of the image pattern
  128.     xor    es:[di],ax    ;XOR the next word of the
  129.                 ; image into the screen
  130.     inc    di    ;point to the next word in display
  131.     inc    di    ; memory
  132.     loop    XorColumnLoopEvenRows
  133.     add    di,bx    ;point to the start of the next
  134.             ; (odd) row of the image, which is
  135.             ; in the second bank of display
  136.             ; memory
  137.     mov    cx,bp    ;# of words to draw per row of the
  138.             ; image
  139. XorColumnLoopOddRows:
  140.     lodsw        ;next word of the image pattern
  141.     xor    es:[di],ax    ;XOR the next word of the
  142.                 ; image into the screen
  143.     inc    di    ;point to the next word in display
  144.     inc    di    ; memory
  145.     loop    XorColumnLoopOddRows
  146.     sub    di,BANK_OFFSET-SCREEN_WIDTH+IMAGE_WIDTH
  147.             ;point to the start of the next
  148.             ; (even) row of the image, which is
  149.             ; in the first bank of display
  150.             ; memory
  151.     dec    dx    ;count down the row pairs
  152.     jnz    XorRowLoop
  153.     pop    bx    ;restore the main loop's pointer
  154.     ret
  155. ;
  156. ; Main animation program.
  157. ;
  158. Skip:
  159. ;
  160. ; Set the mode to 320x200 4-color graphics mode.
  161. ;
  162.     mov    ax,0004h    ;AH=0 is mode select fn
  163.                 ;AL=4 selects mode 4,
  164.                 ; 320x200 4-color mode
  165.     int    10h        ;invoke the BIOS video
  166.                 ; interrupt to set the mode
  167. ;
  168. ; Point ES to display memory for the rest of the program.
  169. ;
  170.     mov    ax,DISPLAY_SEGMENT
  171.     mov    es,ax
  172. ;
  173. ; We'll always want to count up.
  174. ;
  175.     cld
  176. ;
  177. ; Start timing.
  178. ;
  179.     call    ZTimerOn
  180. ;
  181. ; Draw all the images initially.
  182. ;
  183.     mov    bx,offset Images    ;list of images
  184. InitialDrawLoop:
  185.     mov    cx,[bx+XCoord]    ;X coordinate
  186.     mov    dx,[bx+YCoord]    ;Y coordinate
  187.     call    XorImage    ;draw this image
  188.     add    bx,size Image    ;point to next image
  189.     cmp    bx,offset ImagesEnd
  190.     jb    InitialDrawLoop    ;draw next image, if
  191.                 ; there is one
  192. ;
  193. ; Erase, move, and redraw each image in turn REPETITIONS
  194. ; times.
  195. ;
  196. MainMoveAndDrawLoop:
  197.     mov    bx,offset Images    ;list of images
  198. ImageMoveLoop:
  199.     mov    cx,[bx+XCoord]    ;X coordinate
  200.     mov    dx,[bx+YCoord]    ;Y coordinate
  201.     call    XorImage    ;erase this image (it's
  202.                 ; already drawn at this
  203.                 ; location, so this XOR
  204.                 ; erases it)
  205.     mov    cx,[bx+XCoord]    ;X coordinate
  206.     cmp    cx,4        ;at left edge?
  207.     ja    CheckRightMargin ;no
  208.     neg    [bx+XInc]    ;yes, so bounce
  209. CheckRightMargin:
  210.     cmp    cx,284        ;at right edge?
  211.     jb    MoveX        ;no
  212.     neg    [bx+XInc]    ;yes, so bounce
  213. MoveX:
  214.     add    cx,[bx+XInc]    ;move horizontally
  215.     mov    [bx+XCoord],cx    ;save the new location
  216.     mov    dx,[bx+YCoord]    ;Y coordinate
  217.     cmp    dx,4        ;at top edge?
  218.     ja    CheckBottomMargin ;no
  219.     neg    [bx+YInc]    ;yes, so bounce
  220. CheckBottomMargin:
  221.     cmp    dx,164        ;at bottom edge?
  222.     jb    MoveY        ;no
  223.     neg    [bx+YInc]    ;yes, so bounce
  224. MoveY:
  225.     add    dx,[bx+YInc]    ;move horizontally
  226.     mov    [bx+YCoord],dx    ;save the new location
  227.     call    XorImage    ;draw the image at its
  228.                 ; new location
  229.     add    bx,size Image    ;point to the next image
  230.     cmp    bx,offset ImagesEnd
  231.     jb    ImageMoveLoop    ;move next image, if there
  232.                 ; is one
  233.  
  234. if DELAY
  235.     mov    cx,DELAY    ;slow down as specified
  236.     loop    $
  237. endif
  238.     dec    [RepCount]    ;animate again?
  239.     jnz    MainMoveAndDrawLoop ;yes
  240.     call    ZTimerOff    ;done timing
  241. ;
  242. ; Return to text mode.
  243. ;
  244.     mov    ax,0003h    ;AH=0 is mode select fn
  245.                 ;AL=3 selects mode 3,
  246.                 ; 80x25 text mode
  247.     int    10h        ;invoke the BIOS video
  248.                 ; interrupt to set the mode
  249.