home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 86 / asm / source / sprmove.asm < prev    next >
Encoding:
Assembly Source File  |  2001-07-01  |  12.7 KB  |  566 lines

  1. ; SPRMOVE -- sprite moving demo for TI-86 in Z80 assembler
  2. ; by David Phillips <electrum@tfs.net>   08/31/98
  3. ;
  4. ; This is just a simple example of graphics and other stuff in assembler.
  5. ; I tried to include elements important to writting a game such as reading
  6. ; keys using ports, drawing masked sprites, saving and restoring the
  7. ; background, and displaying intro/splatch screens.
  8. ;
  9. ; Note:  The picture was created with Windows Paintbrush, converted to an
  10. ;        asm file using Trent Lillehaugen's BMP2ASM utility, compiled to
  11. ;         an .86p file, then finally compressed using my PIC2RLE utility.
  12. ;        PIC2RLE includes BMP2ASM and is available at www.ticalc.org.
  13. ;         The bitmap files must be saved as monochrome to work correctly.
  14. ;
  15. ; Note2: The ASCR routines originally clipped at the 15th column (8x8 sprites),
  16. ;          but I changed it to clip at the entire screen.  This is because
  17. ;         they were used by Mardell in Sqrxz, and the 16th column was
  18. ;          used for the status bars.
  19.  
  20. #include "ti86asm.inc"
  21.  
  22. .org _asm_exec_ram
  23.  
  24.  
  25. clipmask     = _textShadow        ; [1] used by ASCR
  26. rows2put     = _textShadow + 1    ; [1] ...
  27. bitmask      = _textShadow + 2    ; [1] ...
  28. currspr         = _textShadow + 3    ; [2] current sprite to draw
  29. background     = _textShadow + 5    ; [8] background behind the sprite 
  30.  
  31.  
  32.  call _flushallmenus            ; close any open menus
  33.  res indicRun,(iy+indicflags)    ; turn off the run indicator
  34.  ld hl,Pic                        ; we will display the RLE at Pic
  35.  ld de,$fc00                    ; we will decompress it to video memory
  36.  call DispRLE                    ; show our cool picture
  37.  
  38.  ld b,57                        ; set starting X coord
  39.  ld c,53                        ; set starting Y coord
  40.  ld hl,SprRight                    ; the first time the right sprite is drawn
  41.  ld (currspr),hl                ; set starting sprite
  42.  ld de,background                ; we will save the background at this address
  43.  call PutSprite_MSB                ; draw the sprite for the first time
  44.  
  45. Loop:
  46.  halt                ; wait for the interrupt
  47.  ld a,%01111110        ; load the row for the arrow keys (bit 0 is 0)
  48.  out (1),a            ; tell the port to check
  49.  nop \ nop            ; give the port time to check the hardware
  50.  in a,(1)            ; read the key status
  51.  rra                ; move last bit to carry flag
  52.  jr nc,Down            ; bit 0 is for down
  53.  rra                ; check the next key
  54.  jr nc,Left            ; bit 1 is for left
  55.  rra                ; check the next key
  56.  jr nc,Right        ; bit 2 is for right
  57.  rra                ; check the last key
  58.  jr nc,Up            ; bit 3 is for up
  59.  ld a,%00111111        ; load the row for exit (bit 6 is 0)
  60.  out (1),a            ; tell the port to check
  61.  nop \ nop            ; give the port time to check the hardware
  62.  in a,(1)            ; read the key status
  63.  bit 6,a            ; test the bit for the exit key
  64.  jr nz,Loop            ; if it's not set, we can loop
  65.  call _clrLCD        ; clear the screen
  66.  ret                ; return to the TI-OS or shell
  67.  
  68. Up:
  69.  ld a,c                ; load the coord for check
  70.  or a                ; is it 0?
  71.  jr z,Loop            ; then we don't want to move
  72.  call Erase            ; erase the old sprite
  73.  dec c                ; decrease Y coord
  74.  jr Draw            ; draw sprite
  75.  
  76. Down:
  77.  ld a,c                ; load the coord for check
  78.  cp 56                ; is it 56?
  79.  jr z,Loop            ; then we don't want to move
  80.  call Erase            ; erase the old sprite
  81.  inc c                ; increase Y coord
  82.  jr Draw            ; draw sprite
  83.  
  84. Left:
  85.  ld a,b                ; load the coord for check
  86.  or a                ; is it 0?
  87.  jr z,Loop            ; then we don't want to move
  88.  call Erase            ; erase the old sprite
  89.  dec b                ; decrease X coord
  90.  ld hl,SprLeft        ; point to sprite for left direction
  91.  ld (currspr),hl    ; save new sprite
  92.  jr Draw            ; draw sprite
  93.  
  94. Right:
  95.  ld a,b                ; load the coord for check
  96.  cp 120                ; is it 120?
  97.  jr z,Loop            ; then we don't want to move
  98.  call Erase            ; erase the old sprite
  99.  inc b                ; increase X coord
  100.  ld hl,SprRight        ; point to sprite for right direction
  101.  ld (currspr),hl    ; save new sprite
  102.                     ; a JR DRAW is not needed, we fall through...                
  103.  
  104. Draw:
  105.  push bc            ; save coords
  106.  ld hl,(currspr)    ; load the pointer of the sprite to draw
  107.  ld de,background    ; we will save the background at this address
  108.  call PutSprite_MSB    ; draw the masked sprite and save the background
  109.  pop bc                ; restore coords
  110.  jr Loop            ; loop through the program
  111.  
  112. Erase:
  113.  push bc            ; save coords
  114.  ld hl,background    ; we need to redraw the background
  115.  call PutSprite        ; erase by redrawing the background
  116.  pop bc                ; restore coords
  117.  ret                ; return to what called us
  118.  
  119. ;===========================================================
  120. ; RLE picture displayer
  121. ; Decodes a RLE picture made by RLE2PIC
  122. ;
  123. ; written by David Phillips <electrum@tfs.net>
  124. ; started: 8/19/98
  125. ; last update: 8/31/98
  126. ;
  127. ; input: HL = RLE encoded picture, DE = where to display
  128. ; output: 1024 byte decoded picture
  129. ; destroys: AF, BC, DE, HL
  130. ; current size: 32 bytes
  131. ;===========================================================
  132. DispRLE:
  133.  ld bc,1024            ; we need to copy 
  134. DispRLEL:
  135.  ld a,(hl)            ; get the next byte
  136.  cp $80                ; is it a run?
  137.  jr z,DispRLERun    ; then we need to decode the run
  138.  ldi                ; copy the byte, and update counters
  139. DispRLEC:
  140.  ld a,b                ; load in the high byte
  141.  or c                ; check both both bytes for zero
  142.  jr nz,DispRLEL        ; if not, then we're not done either
  143.  ret                ; if it's zero, we're done
  144. DispRLERun:
  145.  inc hl                ; move to the run value
  146.  ld a,(hl)            ; get the run value
  147.  inc hl                ; move to the run count
  148.  push hl            ; save source pointer
  149.  ld h,(hl)            ; get the run count
  150.  ex de,hl            ; swap source and destination pointers
  151. DispRLERunL:
  152.  ld (hl),a            ; copy the byte
  153.  inc hl                ; increase destination pointer
  154.  dec bc                ; decrease byte count
  155.  dec d                ; decrease run count
  156.  jr nz,DispRLERunL    ; if we're not done, then loop
  157.  ex de,hl            ; swap pointers back
  158.  pop hl                ; recover source pointer
  159.  inc hl                ; advance the source pointer
  160.  jr DispRLEC        ; check to see if we should loop
  161.  
  162. ; ASCR    - Advanced Sprite Clipping Routines
  163. ;
  164. ;     by Jimmy MÃ¥rdell    970304     Last update 970803
  165. ;
  166. ; Temporary variables needed:
  167. ;  clipmask  : Byte
  168. ;  rows2put  : Byte
  169. ;  bitmask     : Byte
  170. ;
  171. ;
  172. ; PutSprite_MSB:
  173. ;  Puts an 8x8 sprite at coordinates B,C. HL should point to a bitmapped
  174. ;  sprite followed by a bitmapped mask, total 16 bytes. DE should point
  175. ;  to an a memory location where the background will be stored as a
  176. ;  bitmap (8 bytes). The sprite will be clipped.
  177. ;
  178. ; PutSprite:
  179. ;  Puts an 8x8 sprite at B,C. HL = pointer to sprite. No mask and no
  180. ;  background storage, just clipping.
  181.  
  182.  
  183. PutSprite_MSB:        ; BC = x,y    DE = background storage  HL = sprite + mask
  184.  ld a,c
  185.  cp 150
  186.  jr nc,PMSB_NoBotClip
  187.  cp 64
  188.  ret nc
  189. PMSB_NoBotClip:
  190.  push bc
  191.  push de
  192.  push hl
  193.  push ix
  194.  ld a,$FF
  195.  ld (clipmask),a
  196.  bit 7,b
  197.  jr z,PMSB_CheckRightClip
  198.  ld a,b
  199.  cp 249
  200.  jr c,EndPMSB
  201.  neg
  202.  push bc
  203.  ld b,a
  204.  ld a,$FF
  205. PMSB_LeftClip:
  206.  srl a
  207.  djnz PMSB_LeftClip
  208.  pop bc
  209.  res 7,b
  210.  dec c
  211.  ld (clipmask),a
  212.  jr PMSB_CheckBotClip
  213. PMSB_CheckRightClip
  214.  ld a,b
  215.  sub 121
  216.  jr c,PMSB_CheckBotClip
  217.  push bc
  218.  ld b,a
  219.  inc b
  220.  ld a,$FF
  221. PMSB_RightClip:
  222.  add a,a
  223.  djnz PMSB_RightClip
  224.  ld (clipmask),a
  225.  pop bc
  226. PMSB_CheckBotClip:
  227.  ld a,8
  228.  ld (rows2put),a
  229.  bit 7,c
  230.  jr nz,PMSB_CheckTopClip
  231.  bit 6,c
  232.  jp nz,EndMSB
  233.  ld a,64
  234.  sub c
  235.  cp 8
  236.  jr nc,PMSB_ClippingDone
  237.  ld (rows2put),a
  238.  jr PMSB_ClippingDone
  239. PMSB_CheckTopClip:
  240.  ld a,c
  241.  cp 249
  242. EndPMSB:
  243.  jr c,EndMSB
  244.  ret c
  245.  push bc
  246.  neg
  247.  ld b,a
  248.  sub 8
  249.  neg
  250.  ld (rows2put),a
  251. PMSB_TopClip:
  252.  inc hl
  253.  inc de
  254.  djnz PMSB_TopClip
  255.  pop bc
  256.  ld c,0
  257. PMSB_ClippingDone:
  258.  di
  259.  push iy
  260.  push hl
  261.  pop iy
  262.  push de
  263.  pop ix
  264.  ld a,(rows2put)
  265.  push af
  266.  call FIND_PIXEL
  267. Modify_5:
  268.  ld de,$FC00
  269.  add hl,de
  270.  ld (bitmask),a
  271.  pop bc
  272. PMSB_PutRow:
  273.  push bc
  274.  push hl
  275.  ld a,(clipmask)
  276.  ld e,a
  277.  ld a,(iy+8)
  278.  and e
  279.  ld e,a      ; e = mask for this row
  280.  ld d,0      ; d = background for this row
  281.  ld b,8      ; b = pixels left to put
  282.  ld c,(iy) ; c = sprite row
  283.  inc iy
  284. PMSB_PutCol:
  285.  push bc
  286.  sla d
  287.  ld a,(bitmask)
  288.  and (hl)
  289.  jr z,PMSB_NPH
  290.  inc d
  291. PMSB_NPH:
  292.  rlc e
  293.  jr nc,PMSB_NextBit
  294.  ld a,(bitmask)
  295.  rlc c
  296.  jr c,PMSB_BitOn
  297.  cpl
  298.  and (hl)
  299.  ld (hl),a
  300.  jr PMSB_NextBit
  301. PMSB_BitOn:
  302.  or (hl)
  303.  ld (hl),a
  304. PMSB_NextBit:
  305.  ld a,(bitmask)
  306.  rrca
  307.  ld (bitmask),a
  308.  jr nc,PMSB_SSB
  309.  inc hl
  310. PMSB_SSB:
  311.  pop bc
  312.  rlc c
  313.  djnz PMSB_PutCol
  314.  ld (ix),d
  315.  inc ix
  316.  pop hl
  317.  ld de,16
  318.  add hl,de
  319.  pop bc
  320.  djnz PMSB_PutRow
  321.  pop iy
  322.  ei
  323. EndMSB:
  324.  pop ix
  325.  pop hl
  326.  pop de
  327.  pop bc
  328.  ret
  329.  
  330. PutSprite:         ; BC = x,y  HL = sprite
  331.  push bc
  332.  push de
  333.  push hl
  334.  push ix
  335.  ld a,$FF
  336.  ld (clipmask),a
  337.  bit 7,b
  338.  jr z,PSC_CheckRightClip
  339.  ld a,b
  340.  cp 249
  341.  jr c,EndPPS
  342.  neg
  343.  push bc
  344.  ld b,a
  345.  ld a,$FF
  346. PSC_LeftClip:
  347.  srl a
  348.  djnz PSC_LeftClip
  349.  pop bc
  350.  res 7,b
  351.  dec c
  352.  ld (clipmask),a
  353.  jr PSC_CheckBotClip
  354. PSC_CheckRightClip
  355.  ld a,b
  356.  sub 121
  357.  jr c,PSC_CheckBotClip
  358.  push bc
  359.  ld b,a
  360.  inc b
  361.  ld a,$FF
  362. PSC_RightClip:
  363.  add a,a
  364.  djnz PSC_RightClip
  365.  ld (clipmask),a
  366.  pop bc
  367. PSC_CheckBotClip:
  368.  ld a,8
  369.  ld (rows2put),a
  370.  bit 7,c
  371.  jr nz,PSC_CheckTopClip
  372.  bit 6,c
  373.  jr nz,EndPS
  374.  ld a,64
  375.  sub c
  376.  cp 8
  377.  jr nc,PSC_ClippingDone
  378.  ld (rows2put),a
  379.  jr PSC_ClippingDone
  380. PSC_CheckTopClip:
  381.  ld a,c
  382.  cp 249
  383. EndPPS:
  384.  jr c,EndPS
  385.  push bc
  386.  neg
  387.  ld b,a
  388.  sub 8
  389.  neg
  390.  ld (rows2put),a
  391. PSC_TopClip:
  392.  inc hl
  393.  inc de
  394.  djnz PSC_TopClip
  395.  pop bc
  396.  ld c,0
  397. PSC_ClippingDone:
  398.  push hl
  399.  pop ix
  400.  ld a,(rows2put)
  401.  push af
  402.  call FIND_PIXEL
  403. Modify_6:
  404.  ld de,$FC00
  405.  add hl,de
  406.  ld d,a
  407.  pop bc
  408. PSC_PutRow:
  409.  push bc
  410.  push hl
  411.  ld a,(clipmask)
  412.  ld e,a
  413.  ld b,8
  414.  ld c,(ix)
  415.  inc ix
  416. PSC_PutCol:
  417.  push bc
  418.  rlc e
  419.  jr nc,PSC_NextBit
  420.  ld a,d
  421.  rlc c
  422.  jr c,PSC_BitOn
  423.  cpl
  424.  and (hl)
  425.  ld (hl),a
  426.  jr PSC_NextBit
  427. PSC_BitOn:
  428.  or (hl)
  429.  ld (hl),a
  430. PSC_NextBit:
  431.  rrc d
  432.  jr nc,PSC_SSB
  433.  inc hl
  434. PSC_SSB:
  435.  pop bc
  436.  rlc c
  437.  djnz PSC_PutCol
  438.  pop hl
  439.  ld bc,16
  440.  add hl,bc
  441.  pop bc
  442.  djnz PSC_PutRow
  443. EndPS:
  444.  pop ix
  445.  pop hl
  446.  pop de
  447.  pop bc
  448.  ret
  449.  
  450. FIND_PIXEL:
  451.  push bc
  452.  push de
  453.  ld hl,ExpTable+1
  454.  ld d,0
  455.  ld a,b
  456.  and $07
  457.  ld e,a
  458.  add hl,de
  459.  ld e,(hl)
  460.  ld h,d
  461.  srl b
  462.  srl b
  463.  srl b
  464.  ld a,c
  465.  add a,a
  466.  add a,a
  467.  ld l,a
  468.  add hl,hl
  469.  add hl,hl
  470.  ld a,e
  471.  ld e,b
  472.  add hl,de
  473.  pop de
  474.  pop bc
  475.  ret
  476.  
  477. ExpTable:
  478.  .db $01,$80,$40,$20,$10,$08,$04,$02,$01
  479.  
  480. SprLeft:
  481.     .db %00111000
  482.     .db %01111100
  483.     .db %11100110
  484.     .db %01111111
  485.     .db %00000111
  486.     .db %00001111
  487.     .db %01111110
  488.     .db %00111100
  489.     .db %00111000
  490.     .db %01111100
  491.     .db %11111110
  492.     .db %01111111
  493.     .db %00000111
  494.     .db %00001111
  495.     .db %01111110
  496.     .db %00111100
  497.  
  498. SprRight:
  499.     .db %00011100
  500.     .db %00111110
  501.     .db %01110011
  502.     .db %11111110
  503.     .db %11100000
  504.     .db %11110000
  505.     .db %01111110
  506.     .db %00111100
  507.     .db %00011100
  508.     .db %00111110
  509.     .db %01111111
  510.     .db %11111110
  511.     .db %11100000
  512.     .db %11110000
  513.     .db %01111110
  514.     .db %00111100
  515.  
  516. Pic:
  517. ; compressed picture made with RLE2PIC
  518. ; RLE2PIC by David Phillips <electrum@tfs.net>
  519.  
  520.  .db $80,$00,$b2,$03,$80,$80,$01,$80,$00,$0e,$1f,$e0
  521.  .db $80,$00,$03,$ff,$3f,$c0,$80,$00,$08,$7f,$f0,$80
  522.  .db $00,$03,$fe,$3f,$80,$80,$01,$80,$00,$08,$ff,$f0
  523.  .db $80,$00,$02,$01,$fe,$7f,$80,$80,$01,$80,$00,$08
  524.  .db $f9,$f0,$80,$00,$02,$01,$fe,$7f,$80,$80,$01,$80
  525.  .db $00,$07,$01,$f9,$f3,$ef,$87,$d9,$fe,$ff,$87,$f8
  526.  .db $80,$f1,$02,$fe,$80,$00,$03,$01,$f3,$f7,$ff,$cf
  527.  .db $fb,$fe,$ff,$1f,$fd,$f1,$e7,$fe,$80,$00,$03,$01
  528.  .db $f3,$e7,$ff,$cf,$fb,$fe,$ff,$3f,$fd,$f3,$ef,$ff
  529.  .db $80,$00,$03,$03,$f8,$07,$cf,$8f,$f3,$fd,$df,$3e
  530.  .db $7d,$f3,$ef,$9f,$80,$00,$03,$03,$fc,$0f,$cf,$9f
  531.  .db $f7,$fd,$fe,$7e,$fd,$f7,$df,$bf,$80,$00,$03,$01
  532.  .db $ff,$0f,$df,$9f,$f7,$df,$fe,$7c,$f9,$e7,$df,$3e
  533.  .db $80,$00,$03,$01,$ff,$0f,$9f,$1f,$87,$ff,$be,$7c
  534.  .db $f9,$e7,$9f,$fe,$80,$00,$04,$ff,$80,$9f,$02,$3f
  535.  .db $07,$ff,$fc,$fd,$fb,$ef,$bf,$fe,$80,$00,$04,$7f
  536.  .db $9f,$bf,$3f,$0f,$bf,$7c,$f9,$fb,$ef,$3f,$fe,$80
  537.  .db $00,$04,$1f,$9f,$80,$3e,$02,$0f,$bf,$7c,$f9,$f3
  538.  .db $ff,$3e,$80,$00,$04,$0f,$df,$bf,$3e,$7e,$0f,$be
  539.  .db $f9,$fb,$f3,$de,$7e,$7c,$80,$00,$03,$0f,$9f,$bf
  540.  .db $80,$7e,$02,$1f,$7e,$f9,$80,$f3,$02,$de,$7c,$fc
  541.  .db $80,$00,$03,$0f,$9f,$be,$80,$7c,$02,$1f,$7c,$f9
  542.  .db $f3,$e3,$fc,$7c,$f8,$80,$00,$03,$1f,$9f,$7e,$7c
  543.  .db $fc,$1f,$7d,$f3,$f7,$e7,$80,$fc,$02,$f8,$80,$00
  544.  .db $03,$1f,$bf,$7e,$80,$fc,$02,$3e,$79,$f3,$f7,$c7
  545.  .db $f8,$fd,$f0,$80,$00,$03,$1f,$fe,$7f,$80,$f8,$02
  546.  .db $3e,$79,$f3,$ff,$c7,$f8,$ff,$f0,$80,$00,$03,$0f
  547.  .db $fc,$ff,$f9,$f8,$3e,$7b,$e3,$ff,$87,$f8,$7f,$e0
  548.  .db $80,$00,$03,$0f,$f8,$fd,$f1,$f8,$7c,$f3,$e1,$fe
  549.  .db $07,$f0,$3f,$80,$80,$01,$80,$00,$05,$f8,$80,$00
  550.  .db $0f,$f8,$80,$00,$0e,$01,$f8,$80,$00,$3d,$20,$00
  551.  .db $0f,$80,$00,$02,$80,$80,$02,$f2,$02,$18,$60,$80
  552.  .db $80,$01,$80,$00,$04,$20,$00,$08,$80,$80,$01,$80
  553.  .db $00,$02,$80,$80,$01,$8a,$00,$08,$20,$80,$00,$05
  554.  .db $2c,$88,$08,$9c,$89,$86,$80,$80,$01,$8a,$c6,$08
  555.  .db $21,$8f,$1c,$80,$00,$03,$32,$88,$08,$82,$88,$89
  556.  .db $80,$80,$01,$f3,$22,$08,$20,$88,$a0,$80,$00,$03
  557.  .db $22,$78,$08,$9e,$80,$88,$02,$80,$80,$01,$82,$22
  558.  .db $08,$20,$8f,$1c,$80,$00,$03,$22,$80,$08,$02,$a2
  559.  .db $50,$88,$80,$80,$01,$82,$22,$08,$20,$88,$02,$80
  560.  .db $00,$03,$3c,$70,$0f,$1e,$21,$c7,$80,$80,$01,$82
  561.  .db $27,$1c,$71,$c8,$3c,$80,$00,$ff,$80,$00,$13
  562.  
  563. ; 1024 bytes compressed to 503 -- 49% of original.
  564.  
  565. .end
  566.