z80:Sprite Routines
Sprite Routines
The routine below is a routine that takes your 8 wide * X high sprite and then puts it in the screen buffer. It is commonly known as Ion PutSprite, by Joe Wingbermuehle. More routines can be found on [*http://ticalc.org TiCalc] (see links to your left), United Ti and Cemetech.
putSprite: ld e,l ld h,$00 ld d,h add hl,de add hl,de add hl,hl add hl,hl ;Find the Y displacement offset ld e,a and $07 ;Find the bit number ld c,a srl e srl e srl e add hl,de ;Find the X displacement offset ld de,gbuf add hl,de putSpriteLoop1: sl1: ld d,(ix) ;loads image byte into D ld e,$00 ld a,c or a jr z,putSpriteSkip1 putSpriteLoop2: srl d ;rotate to give out smooth moving rr e dec a jr nz,putSpriteLoop2 putSpriteSkip1: ld a,(hl) xor d ld (hl),a inc hl ld a,(hl) xor e ld (hl),a ;copy to buffer using XOR logic ld de,$0B add hl,de inc ix ;Set for next byte of image djnz putSpriteLoop1 ret
As is probably apparent, this routine can be modified to not only XOR sprites, but also AND and OR. All you have to do is change the two XOR's to the appropriate function.
Animating Sprites
The routine below will animate two sprites by simply displaying the first sprite, erasing it, then displaying the second sprite, erasing it and finally repeat the loop. Remember that this code is for only 2 sprites. More can be added though. Note that this routine uses ion calls and will not function unless you are using the ion header. See Shells for more information.
loop: ld ix,Sprite_address ld a,X_coordinate ld l,Y_coordinate ld b,Sprite_height call iputsprite8 ;Assuming your sprite is 8x8 call ifastcopy ld ix,Sprite_address ld a,X_coordinate ld l,Y_coordinate ld b,Sprite_height call iputsprite8 ;erase ld ix,Sprite_address ld a,X_coordinate+1 ld l,Y_coordinate ld b,Sprite_height call iputsprite8 ;draw and display your second sprite call ifastcopy ld ix,Sprite_address ld a,X_coordinate+1 ld l,Y_coordinate ld b,Sprite_height call iputsprite8 ;erase jp loop
Thank you Fallen Ghost for your help with this code!
Another look at Animated Sprites
If you want to cut down on size or increase speed, there is another way to create animated sprites. Instead of having to erase each time, what you could do is add an extra sprite that's an XOR of two sprites: the layer previously displayed, and the next layer. Note that the first and last layers must be intact (what they would look like without XOR-ing to another layer).
Example:
loop: ld ix,Layer1 ;draw first layer as is everytime ld a,0 ld l,0 ld b,8 call ionputsprite call ionfastcopy ld ix,XORLayer ;move onto next layer. Since it has the XORed part, no need to erase ("already done") ld a,0 ld l,0 ld b,8 call ionputsprite call ionfastcopy ;... any other layers would look similar to the few lines of code above ld ix,Layer2 ;Just need to erase last layer ld a,0 ld l,0 ld b,8 call ionputsprite jr loop Layer1: .db $00,$7E,$B9,$CF,$CF,$B9,$7E,$00 Layer2: .db $FC,$72,$9E,$9E,$72,$FC,$00,$00 XORLayer: ;XOR of Layer1 and Layer2 .db $FC,$0C,$27,$51,$BD,$45,$7E,$00 ;... extra XORed layers, if necessary
Calculating from above, having to erase each time took 13 bytes/extra layer, and some time (usually insignificant, but sometimes crucial). By having the XOR layer, it only adds 8 more bytes (assuming you have 8*8 sprites), and takes less time (if time is crucial). That's a saving of 5 bytes/extra layer!