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 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,0ドルB
 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,ドル7ドルE,$B9,$CF,$CF,$B9,7ドルE,00ドル
Layer2:
.db $FC,72,ドル9ドルE,9ドルE,72,ドル$FC,00,ドル00ドル
XORLayer: ;XOR of Layer1 and Layer2
.db $FC,0ドルC,27,ドル51,ドル$BD,45,ドル7ドルE,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!


page revision: 10, last edited: 11 Aug 2008 05:51
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /