1. Tilemaps
2. Sprites
3. Scrolling
4. Control
Notes

2. SPRITES

Ah, the sprite. The source of endless arguments between comp.sys.sinclair and comp.sys.cbm, the sprite is essential for our purposes here. You see Miner Willy there? He's a sprite. So are the monsters. Robocop there? He's a sprite too, and so are his bullets, and the people he's shooting. So, we'll be needing some sprites too, won't we?

Sprites are a little bit more complicated than tiles. You see, whereas we can simply blast tiles onto the screen without a care in the world, sprites have to worry about what they're being displayed over. Additionally, sprites aren't always neatly aligned on the screen at multiples of eight.

The first thing we need to consider is that there are two types of sprite. There's the overlapping sprite, and then there's the masked sprite. An overlapping (unmasked) sprite is one that overdraws whatever is under it, but doesn't draw any nasty blank areas around it. (Like Miner Willy.) A masked sprite (like Robo) has blank pixels inside the sprite, and as such needs to delete some of what's under it. In case that rather poor explanation wasn't enough, here's what Robocop would look like if he was an overlapping sprite.

Now, each of these types has their place. Continuing to use Robocop as an example, while Robo and the crims are masked sprites, their bullets are unmasked, and as such are faster to draw. It all depends on whether or not you have a background you need to keep intact. Since both are useful, I suppose I'm going to have to show you how to draw both. We'll start with the unmasked sprite, since it's simpler.






umask_sprite:
; Unmasked Sprite Routine
;
; Variables needed:
; B,C - screen Y,X coordinates
; DE - address of graphic data to display
;

call get_screen_offset ; Position HL at the correct row
call get_column_offset ; Position HL at the correct column -
; C is now set with the column offset

ld b, 8 ; Load B with number of rows

umask_sprite_loop:
ld a,(de) ; Load A with a byte

push bc

ld a, c ; Load A with offset
cp 0 ; Compare offset with zero
jp z, umask_sprite_skip ; If offset is zero jump past the offsetting loop

ld a,(de) ; Load A with a byte
ld b, c ; Load B with C to use as a counter
umask_sprite_a_loop:
srl a ; Divide A by two
djnz umask_sprite_a_loop ; Decrement B. If it's not zero, loop back for more shifting

umask_sprite_skip:
or (hl) ; OR sprite data with whatever's on the screen
ld (hl), a

ld a, c ; Load A with offset
cp 0 ; Compare offset with zero
jp z, umask_sprite_next ; If offset is zero jump to do the next row

inc hl ; and move HL to the next screen byte

xor %00000111 ; XOR offset to get left shift offset
ld b, a ; Load B with A to use as a counter
inc b

ld a,(de) ; Load A with a byte of sprite data

umask_sprite_b_loop:
sla a ; Shift A left
djnz umask_sprite_b_loop ; Decrement B. If it's not zero, loop back for more shifting

or (hl) ; OR sprite data with whatever's on the screen
ld (hl), a

dec hl ; Point HL back at the previous byte

umask_sprite_next
pop bc ; Get the original B and C back (we need the B counter)

inc de ; Shift DE to point at the next byte of graphic data

call _get_next_row ; Shift HL to point exactly one row down
djnz umask_sprite_loop ; If B is not zero, decrement B and loop

ret

There we go. It's a bit more complicated to draw a sprite than a tile, but that's because we need to be able to draw it at any pixel location, rather than just neat little multiples of eight. You'll notice also that we've added another helper function to find the address and offset of the right column. So, now that we know the basics, let's move on to the wonders of the masked sprite...

mask_sprite:
; Unmasked Sprite Routine
;
; Variables needed:
; B,C - screen Y,X coordinates
; IX - address of graphic data to display
;

call get_screen_offset ; Position HL at the correct row
call get_column_offset ; Position HL at the correct column -
; C is now set with the column offset

ld b, 8 ; Load B with number of rows

mask_sprite_loop:
push bc

ld d,(ix+8)
ld e,(ix)

ld a, c ; Load A with offset
cp 0 ; Compare offset with zero
jp z, mask_sprite_skip_1 ; If offset is zero jump to do the next row

ld b, c ; Load B with C to use as a counter
mask_sprite_a_loop:
srl d
srl e
set 7, d
res 7, e
djnz mask_sprite_a_loop ; Decrement B. If it's not zero, loop back for more shifting

mask_sprite_skip_1:
ld a, (hl)
and d
or e
ld (hl), a

ld a, c ; Load A with offset
cp 0 ; Compare offset with zero
jp z, mask_sprite_next ; If offset is zero jump to do the next row

inc hl ; and move HL to the next screen byte

xor %00000111 ; XOR offset to get left shift offset
ld b, a ; Load B with A to use as a counter
inc b

ld d,(ix+8)
ld e,(ix)

mask_sprite_b_loop:
sll d
sll e

set 0, d
res 0, e
djnz mask_sprite_b_loop ; Decrement B. If it's not zero, loop back for more shifting

ld a, (hl)
and d
or e
ld (hl), a

dec hl ; Point HL back at the previous byte

mask_sprite_next
pop bc ; Get the original B and C back (we need the B counter)

inc ix ; Shift DE to point at the next byte of graphic data

call _get_next_row ; Shift HL to point exactly one row down
djnz mask_sprite_loop ; If B is not zero, decrement B and loop

ret

This code assumes that the sprite is an 8x8 block again. It also assumes that the sprite is immediately followed by an 8x8 mask. This means we can use the IX register, for a bit of variety. As you can see, it's another level of complication over the unmasked sprite, but I'm sure you won't have many problems getting your head around it. Again, you can cut and paste these examples and change the sprite data to use sprites in your own code, and since the sprite routines are very self-contained, they should be extremely easy for you to use.

<<Back

Forward>>

Back to Contents