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

  1. ; Program to demonstrate bit-plane animation. Performs
  2. ; flicker-free animation with image transparency and
  3. ; image precedence across four distinct planes, with
  4. ; 13 32x32 images kept in motion at once.
  5. ;
  6. ; Updated 6/29/89.
  7. ;
  8. ; Set to higher values to slow down on faster computers.
  9. ; 0 is fine for a PC. 500 is a reasonable setting for an AT.
  10. ; Slowing animation further allows a good look at
  11. ; transparency and the lack of flicker and color effects
  12. ; when images cross.
  13. ;
  14. SLOWDOWN        equ     0
  15. ;
  16. ; Plane selects for the four colors we're using.
  17. ;
  18. RED     equ     01h
  19. GREEN   equ     02h
  20. BLUE    equ     04h
  21. WHITE   equ     08h
  22. ;
  23. VGA_SEGMENT     equ     0a000h  ;mode 10h display memory
  24.                                 ; segment
  25. SC_INDEX        equ     3c4h    ;Sequence Controller Index
  26.                                 ; register
  27. MAP_MASK        equ     2       ;Map Mask register index in
  28.                                 ; Sequence Controller
  29. SCREEN_WIDTH    equ     80      ;# of bytes across screen
  30. SCREEN_HEIGHT   equ     350     ;# of scan lines on screen
  31. WORD_OUTS_OK    equ     1       ;set to 0 to assemble for
  32.                                 ; computers that can't
  33.                                 ; handle word outs to
  34.                                 ; indexed VGA regs
  35. ;
  36. stackseg        segment para stack 'STACK'
  37.         db      512 dup (?)
  38. stackseg        ends
  39. ;
  40. ; Complete info about one object that we're animating.
  41. ;
  42. ObjectStructure struc
  43. Delay           dw      ?       ;used to delay for n passes
  44.                                 ; through the loop to
  45.                                 ; control animation speed
  46. BaseDelay       dw      ?       ;reset value for Delay
  47. Image           dw      ?       ;pointer to drawing info
  48.                                 ; for object
  49. XCoord          dw      ?       ;object X location in pixels
  50. XInc            dw      ?       ;# of pixels to increment
  51.                                 ; location by in the X
  52.                                 ; direction on each move
  53. XLeftLimit      dw      ?       ;left limit of X motion
  54. XRightLimit     dw      ?       ;right limit of X motion
  55. YCoord          dw      ?       ;object Y location in pixels
  56. YInc            dw      ?       ;# of pixels to increment
  57.                                 ; location by in the Y
  58.                                 ; direction on each move
  59. YTopLimit       dw      ?       ;top limit of Y motion
  60. YBottomLimit    dw      ?       ;bottom limit of Y motion
  61. PlaneSelect     db      ?       ;mask to select plane to
  62.                                 ; which object is drawn
  63.                 db      ?       ;to make an even # of words
  64.                                 ; long, for better 286
  65.                                 ; performance (keeps the
  66.                                 ; following structure
  67.                                 ; word-aligned)
  68. ObjectStructure ends
  69. ;
  70. Data    segment word 'DATA'
  71. ;
  72. ; Palette settings to give plane 0 precedence, followed by
  73. ; planes 1, 2, and 3. Plane 3 has the lowest precedence (is
  74. ; obscured by any other plane), while plane 0 has the
  75. ; highest precedence (displays in front of any other plane).
  76. ;
  77. Colors  db      000h ;background color=black
  78.         db      03ch ;plane 0 only=red
  79.         db      03ah ;plane 1 only=green
  80.         db      03ch ;planes 0&1=red (plane 0 priority)
  81.         db      039h ;plane 2 only=blue
  82.         db      03ch ;planes 0&2=red (plane 0 priority)
  83.         db      03ah ;planes 1&2=green (plane 1 priority)
  84.         db      03ch ;planes 0&1&2=red (plane 0 priority)
  85.         db      03fh ;plane 3 only=white
  86.         db      03ch ;planes 0&3=red (plane 0 priority)
  87.         db      03ah ;planes 1&3=green (plane 1 priority)
  88.         db      03ch ;planes 0&1&3=red (plane 0 priority)
  89.         db      039h ;planes 2&3=blue (plane 2 priority)
  90.         db      03ch ;planes 0&2&3=red (plane 0 priority)
  91.         db      03ah ;planes 1&2&3=green (plane 1 priority)
  92.         db      03ch ;planes 0&1&2&3=red (plane 0 priority)
  93.         db      000h ;border color=black
  94. ;
  95. ; Image of a hollow square.
  96. ; There's an 8-pixel-wide blank border around all edges
  97. ; so that the image erases the old version of itself as
  98. ; it's moved and redrawn.
  99. ;
  100. Square  label   byte
  101.         dw      48,6    ;height in pixels, width in bytes
  102.         rept    8
  103.         db      0,0,0,0,0,0     ;top blank border
  104.         endm
  105.         .radix  2
  106.         db      0,11111111,11111111,11111111,11111111,0
  107.         db      0,11111111,11111111,11111111,11111111,0
  108.         db      0,11111111,11111111,11111111,11111111,0
  109.         db      0,11111111,11111111,11111111,11111111,0
  110.         db      0,11111111,11111111,11111111,11111111,0
  111.         db      0,11111111,11111111,11111111,11111111,0
  112.         db      0,11111111,11111111,11111111,11111111,0
  113.         db      0,11111111,11111111,11111111,11111111,0
  114.         db      0,11111111,00000000,00000000,11111111,0
  115.         db      0,11111111,00000000,00000000,11111111,0
  116.         db      0,11111111,00000000,00000000,11111111,0
  117.         db      0,11111111,00000000,00000000,11111111,0
  118.         db      0,11111111,00000000,00000000,11111111,0
  119.         db      0,11111111,00000000,00000000,11111111,0
  120.         db      0,11111111,00000000,00000000,11111111,0
  121.         db      0,11111111,00000000,00000000,11111111,0
  122.         db      0,11111111,00000000,00000000,11111111,0
  123.         db      0,11111111,00000000,00000000,11111111,0
  124.         db      0,11111111,00000000,00000000,11111111,0
  125.         db      0,11111111,00000000,00000000,11111111,0
  126.         db      0,11111111,00000000,00000000,11111111,0
  127.         db      0,11111111,00000000,00000000,11111111,0
  128.         db      0,11111111,00000000,00000000,11111111,0
  129.         db      0,11111111,00000000,00000000,11111111,0
  130.         db      0,11111111,11111111,11111111,11111111,0
  131.         db      0,11111111,11111111,11111111,11111111,0
  132.         db      0,11111111,11111111,11111111,11111111,0
  133.         db      0,11111111,11111111,11111111,11111111,0
  134.         db      0,11111111,11111111,11111111,11111111,0
  135.         db      0,11111111,11111111,11111111,11111111,0
  136.         db      0,11111111,11111111,11111111,11111111,0
  137.         db      0,11111111,11111111,11111111,11111111,0
  138.         .radix  10
  139.         rept    8
  140.         db      0,0,0,0,0,0     ;bottom blank border
  141.         endm
  142. ;
  143. ; Image of a hollow diamond with a smaller diamond in the
  144. ; middle.
  145. ; There's an 8-pixel-wide blank border around all edges
  146. ; so that the image erases the old version of itself as
  147. ; it's moved and redrawn.
  148. ;
  149. Diamond label   byte
  150.         dw      48,6    ;height in pixels, width in bytes
  151.         rept    8
  152.         db      0,0,0,0,0,0     ;top blank border
  153.         endm
  154.         .radix  2
  155.         db      0,00000000,00000001,10000000,00000000,0
  156.         db      0,00000000,00000011,11000000,00000000,0
  157.         db      0,00000000,00000111,11100000,00000000,0
  158.         db      0,00000000,00001111,11110000,00000000,0
  159.         db      0,00000000,00011111,11111000,00000000,0
  160.         db      0,00000000,00111110,01111100,00000000,0
  161.         db      0,00000000,01111100,00111110,00000000,0
  162.         db      0,00000000,11111000,00011111,00000000,0
  163.         db      0,00000001,11110000,00001111,10000000,0
  164.         db      0,00000011,11100000,00000111,11000000,0
  165.         db      0,00000111,11000000,00000011,11100000,0
  166.         db      0,00001111,10000001,10000001,11110000,0
  167.         db      0,00011111,00000011,11000000,11111000,0
  168.         db      0,00111110,00000111,11100000,01111100,0
  169.         db      0,01111100,00001111,11110000,00111110,0
  170.         db      0,11111000,00011111,11111000,00011111,0
  171.         db      0,11111000,00011111,11111000,00011111,0
  172.         db      0,01111100,00001111,11110000,00111110,0
  173.         db      0,00111110,00000111,11100000,01111100,0
  174.         db      0,00011111,00000011,11000000,11111000,0
  175.         db      0,00001111,10000001,10000001,11110000,0
  176.         db      0,00000111,11000000,00000011,11100000,0
  177.         db      0,00000011,11100000,00000111,11000000,0
  178.         db      0,00000001,11110000,00001111,10000000,0
  179.         db      0,00000000,11111000,00011111,00000000,0
  180.         db      0,00000000,01111100,00111110,00000000,0
  181.         db      0,00000000,00111110,01111100,00000000,0
  182.         db      0,00000000,00011111,11111000,00000000,0
  183.         db      0,00000000,00001111,11110000,00000000,0
  184.         db      0,00000000,00000111,11100000,00000000,0
  185.         db      0,00000000,00000011,11000000,00000000,0
  186.         db      0,00000000,00000001,10000000,00000000,0
  187.         .radix  10
  188.         rept    8
  189.         db      0,0,0,0,0,0     ;bottom blank border
  190.         endm
  191. ;
  192. ; List of objects to animate.
  193. ;
  194.         even    ;word-align for better 286 performance
  195. ;
  196. ObjectList      label   ObjectStructure
  197.  ObjectStructure <1,21,Diamond,88,8,80,512,16,0,0,350,RED>
  198.  ObjectStructure <1,15,Square,296,8,112,480,144,0,0,350,RED>
  199.  ObjectStructure <1,23,Diamond,88,8,80,512,256,0,0,350,RED>
  200.  ObjectStructure <1,13,Square,120,0,0,640,144,4,0,280,BLUE>
  201.  ObjectStructure <1,11,Diamond,208,0,0,640,144,4,0,280,BLUE>
  202.  ObjectStructure <1,8,Square,296,0,0,640,144,4,0,288,BLUE>
  203.  ObjectStructure <1,9,Diamond,384,0,0,640,144,4,0,288,BLUE>
  204.  ObjectStructure <1,14,Square,472,0,0,640,144,4,0,280,BLUE>
  205.  ObjectStructure <1,8,Diamond,200,8,0,576,48,6,0,280,GREEN>
  206.  ObjectStructure <1,8,Square,248,8,0,576,96,6,0,280,GREEN>
  207.  ObjectStructure <1,8,Diamond,296,8,0,576,144,6,0,280,GREEN>
  208.  ObjectStructure <1,8,Square,344,8,0,576,192,6,0,280,GREEN>
  209.  ObjectStructure <1,8,Diamond,392,8,0,576,240,6,0,280,GREEN>
  210. ObjectListEnd   label   ObjectStructure
  211. ;
  212. Data    ends
  213. ;
  214. ; Macro to output a word value to a port.
  215. ;
  216. OUT_WORD        macro
  217. if WORD_OUTS_OK
  218.         out     dx,ax
  219. else
  220.         out     dx,al
  221.         inc     dx
  222.         xchg    ah,al
  223.         out     dx,al
  224.         dec     dx
  225.         xchg    ah,al
  226. endif
  227.         endm
  228. ;
  229. ; Macro to output a constant value to an indexed VGA
  230. ; register.
  231. ;
  232. CONSTANT_TO_INDEXED_REGISTER    macro ADDRESS, INDEX, VALUE
  233.         mov     dx,ADDRESS
  234.         mov     ax,(VALUE shl 8) + INDEX
  235.         OUT_WORD
  236.         endm
  237. ;
  238. Code    segment
  239.         assume  cs:Code, ds:Data
  240. Start   proc    near
  241.         cld
  242.         mov     ax,Data
  243.         mov     ds,ax
  244. ;
  245. ; Set 640x350 16-color mode.
  246. ;
  247.         mov     ax,0010h        ;AH=0 means select mode
  248.                                 ;AL=10h means select
  249.                                 ; mode 10h
  250.         int     10h             ;BIOS video interrupt
  251. ;
  252. ; Set the palette up to provide bit-plane precedence. If
  253. ; planes 0 & 1 overlap, the plane 0 color will be shown;
  254. ; if planes 1 & 2 overlap, the plane 1 color will be
  255. ; shown; and so on.
  256. ;
  257.         mov     ax,(10h shl 8) + 2      ;AH = 10h means
  258.                                         ; set palette
  259.                                         ; registers fn
  260.                                         ;AL = 2 means set
  261.                                         ; all palette
  262.                                         ; registers
  263.         push    ds                      ;ES:DX points to
  264.         pop     es                      ; the palette
  265.         mov     dx,offset Colors        ; settings
  266.         int     10h                     ;call the BIOS to
  267.                                         ; set the palette
  268. ;
  269. ; Draw the static backdrop in plane 3. All the moving images
  270. ; will appear to be in front of this backdrop, since plane 3
  271. ; has the lowest precedence the way the palette is set up.
  272. ;
  273.         CONSTANT_TO_INDEXED_REGISTER SC_INDEX, MAP_MASK, 08h
  274.                                 ;allow data to go to
  275.                                 ; plane 3 only
  276. ;
  277. ; Point ES to display memory for the rest of the program.
  278. ;
  279.         mov     ax,VGA_SEGMENT
  280.         mov     es,ax
  281. ;
  282.         sub     di,di
  283.         mov     bp,SCREEN_HEIGHT/16     ;fill in the screen
  284.                                         ; 16 lines at a time
  285. BackdropBlockLoop:
  286.         call    DrawGridCross           ;draw a cross piece
  287.         call    DrawGridVert            ;draw the rest of a
  288.                                         ; 15-high block
  289.         dec     bp
  290.         jnz     BackdropBlockLoop
  291.         call    DrawGridCross           ;bottom line of grid
  292. ;
  293. ; Start animating!
  294. ;
  295. AnimationLoop:
  296.         mov     bx,offset ObjectList    ;point to the first
  297.                                         ; object in the list
  298. ;
  299. ; For each object, see if it's time to move and draw that
  300. ; object.
  301. ;
  302. ObjectLoop:
  303. ;
  304. ; See if it's time to move this object.
  305. ;
  306.         dec     [bx+Delay]      ;count down delay
  307.         jnz     DoNextObject    ;still delaying--don't move
  308.         mov     ax,[bx+BaseDelay]
  309.         mov     [bx+Delay],ax   ;reset delay for next time
  310. ;
  311. ; Select the plane that this object will be drawn in.
  312. ;
  313.         mov     dx,SC_INDEX
  314.         mov     ah,[bx+PlaneSelect]
  315.         mov     al,MAP_MASK
  316.         OUT_WORD
  317. ;
  318. ; Advance the X coordinate, reversing direction if either
  319. ; of the X margins has been reached.
  320. ;
  321.         mov     cx,[bx+XCoord]          ;current X location
  322.         cmp     cx,[bx+XLeftLimit]      ;at left limit?
  323.         ja      CheckXRightLimit        ;no
  324.         neg     [bx+XInc]               ;yes-reverse
  325. CheckXRightLimit:
  326.         cmp     cx,[bx+XRightLimit]     ;at right limit?
  327.         jb      SetNewX                 ;no
  328.         neg     [bx+XInc]               ;yes-reverse
  329. SetNewX:
  330.         add     cx,[bx+XInc]            ;move the X coord
  331.         mov     [bx+XCoord],cx          ; & save it
  332. ;
  333. ; Advance the Y coordinate, reversing direction if either
  334. ; of the Y margins has been reached.
  335. ;
  336.         mov     dx,[bx+YCoord]          ;current Y location
  337.         cmp     dx,[bx+YTopLimit]       ;at top limit?
  338.         ja      CheckYBottomLimit       ;no
  339.         neg     [bx+YInc]               ;yes-reverse
  340. CheckYBottomLimit:
  341.         cmp     dx,[bx+YBottomLimit]    ;at bottom limit?
  342.         jb      SetNewY                 ;no
  343.         neg     [bx+YInc]               ;yes-reverse
  344. SetNewY:
  345.         add     dx,[bx+YInc]            ;move the Y coord
  346.         mov     [bx+YCoord],dx          ; & save it
  347. ;
  348. ; Draw at the new location. Because of the plane select
  349. ; above, only one plane will be affected.
  350. ;
  351.         mov     si,[bx+Image]           ;point to the
  352.                                         ; object's image
  353.                                         ; info
  354.         call    DrawObject
  355. ;
  356. ; Point to the next object in the list until we run out of
  357. ; objects.
  358. ;
  359. DoNextObject:
  360.         add     bx,size ObjectStructure
  361.         cmp     bx,offset ObjectListEnd
  362.         jb      ObjectLoop
  363. ;
  364. ; Delay as specified to slow things down.
  365. ;
  366. if SLOWDOWN
  367.         mov     cx,SLOWDOWN
  368. DelayLoop:
  369.         loop    DelayLoop
  370. endif
  371. ;
  372. ; If a key's been pressed, we're done, otherwise animate
  373. ; again.
  374. ;
  375. CheckKey:
  376.         mov     ah,1
  377.         int     16h             ;is a key waiting?
  378.         jz      AnimationLoop   ;no
  379.         sub     ah,ah
  380.         int     16h             ;yes-clear the key & done
  381. ;
  382. ; Back to text mode.
  383. ;
  384.         mov     ax,0003h        ;AH=0 means select mode
  385.                                 ;AL=03h means select
  386.                                 ; mode 03h
  387.         int     10h
  388. ;
  389. ; Back to DOS.
  390. ;
  391.         mov     ah,4ch          ;DOS terminate function
  392.         int     21h             ;done
  393. ;
  394. Start   endp
  395. ;
  396. ; Draws a single grid cross-element at the display memory
  397. ; location pointed to by ES:DI. 1 horizontal line is drawn
  398. ; across the screen.
  399. ;
  400. ; Input: ES:DI points to the address at which to draw
  401. ;
  402. ; Output: ES:DI points to the address following the
  403. ;               line drawn
  404. ;
  405. ; Registers altered: AX, CX, DI
  406. ;
  407. DrawGridCross   proc    near
  408.         mov     ax,0ffffh       ;draw a solid line
  409.         mov     cx,SCREEN_WIDTH/2-1
  410.         rep     stosw           ;draw all but the rightmost
  411.                                 ; edge
  412.         mov     ax,0080h
  413.         stosw                   ;draw the right edge of the
  414.                                 ; grid
  415.         ret
  416. DrawGridCross   endp
  417. ;
  418. ; Draws the noncross part of the grid at the display-memory
  419. ; location pointed to by ES:DI. 15 scan lines are filled.
  420. ;
  421. ; Input: ES:DI points to the address at which to draw
  422. ;
  423. ; Output: ES:DI points to the address following the
  424. ;               part of the grid drawn
  425. ;
  426. ; Registers altered: AX, CX, DX, DI
  427. ;
  428. DrawGridVert    proc    near
  429.         mov     ax,0080h        ;pattern for a vertical line
  430.         mov     dx,15           ;draw 15 scan lines (all of
  431.                                 ; a grid block except the
  432.                                 ; solid cross line)
  433. BackdropRowLoop:
  434.         mov     cx,SCREEN_WIDTH/2
  435.         rep     stosw           ;draw this scan line's bit
  436.                                 ; of all the vertical lines
  437.                                 ; on the screen
  438.         dec     dx
  439.         jnz     BackdropRowLoop
  440.         ret
  441. DrawGridVert    endp
  442. ;
  443. ; Draws the specified image at the specified location.
  444. ; Images are drawn on byte boundaries horizontally, pixel
  445. ; boundaries vertically.
  446. ; The Map Mask register must already have been set to enable
  447. ; access to the desired plane.
  448. ;
  449. ; Input:
  450. ;       CX - X coordinate of upper left corner
  451. ;       DX - Y coordinate of upper left corner
  452. ;       DS:SI - pointer to draw info for image
  453. ;       ES - display memory segment
  454. ;
  455. ; Output: none
  456. ;
  457. ; Registers altered: AX, CX, DX, SI, DI, BP
  458. ;
  459. DrawObject      proc    near
  460.         mov     ax,SCREEN_WIDTH
  461.         mul     dx      ;calculate the start offset in
  462.                         ; display memory of the row the
  463.                         ; image will be drawn at
  464.         shr     cx,1
  465.         shr     cx,1
  466.         shr     cx,1    ;divide the X coordinate in pixels
  467.                         ; by 8 to get the X coordinate in
  468.                         ; bytes
  469.         add     ax,cx   ;destination offset in display
  470.                         ; memory for the image
  471.         mov     di,ax   ;point ES:DI to the address to
  472.                         ; which the image will be copied
  473.                         ; in display memory
  474.         lodsw
  475.         mov     dx,ax   ;# of lines in the image
  476.         lodsw           ;# of bytes across the image
  477.         mov     bp,SCREEN_WIDTH
  478.         sub     bp,ax   ;# of bytes to add to the display
  479.                         ; memory offset after copying a line
  480.                         ; of the image to display memory in
  481.                         ; order to point to the address
  482.                         ; where the next line of the image
  483.                         ; will go in display memory
  484. DrawLoop:
  485.         mov     cx,ax   ;width of the image
  486.         rep     movsb   ;copy the next line of the image
  487.                         ; into display memory
  488.         add     di,bp   ;point to the address at which the
  489.                         ; next line will go in display
  490.                         ; memory
  491.         dec     dx      ;count down the lines of the image
  492.         jnz     DrawLoop
  493.         ret
  494. DrawObject      endp
  495. ;
  496. Code    ends
  497.         end     Start