Code: Select all
horizClip:
sub 8
jr nc, horizClip2 ;if part of the ship is on the right of the screen
ld d, c ;don't jump, use the mask to disable E
Eloop:
sla e ;push a 0 into the rightmost part of the mask
dec d ;eliminating that part of the sprite cuz its off-screen
jr nz, Eloop
jr retmask
horizClip2:
sub 151
jr nc, horizClip3 ;if its not in any part of the screen, dont
ret ;bother showing anything, return!
horizClip3:
ld a, 8 ;ship must be on the left, use the mask to disable D
sub c
Dloop:
srl e ;push a 0 into the leftmost part of the mask to clip the sprite
dec a
jr nz, Dloop
ld a, l
sub 32 ;subtract 32 to move sprite from the right to the left
ld l, a
jr nc, retmask
dec h ;dont forget to CARRY the one!
jr retmask
putSpritePlus:
ld h, a
vertClip:
ld a, l ;load y co. into a
cp 100
jr c, vertClip2 ;while the spite is too high
inc ix ;go to the next part of the pic since
inc l ;this one wont be used and go down one
djnz vertClip ;also subtract one from the height
ret ;if new height is 0, nothing to print, return
vertClip2:
add a, b ;y co. + height is the bottom of the sprite
sub 64
jr c, vertExit ;while the sprite is too low
ld a, l
djnz vertClip2 ;subtract 1 from the height
ret ;if new height is 0, nothing to print, return
vertExit:
ld a, h
push hl ;save the x, y co.'s
ld e,l
ld h,$00 ;now we must go to the correct spot in memory
ld d,h ;aka, gbuf to output the sprite
add hl,de ;gbuf + (int)(x/8) + y * 12 = answer
add hl,de
add hl,hl
add hl,hl ;put Y co. * 12 into hl
ld e,a
and $07 ;mod 8 to find the bit offset
ld c,a
srl e
srl e
srl e ; equals (int)( x co./ 8 )
add hl,de ;adds the x displacement to the y displacement
ld de,gbuf
add hl,de ;finally add the gbuf to get the correct index!
pop de ;restore x co. to d, now to make the mask for horiz clipping
ld e, %11111111 ;start with a blank mask (we will and it later on)
ld a, d
sub 89
jr nc, horizClip ;if the sprite is not on the screen, clip it horizontally (using mask)
retmask: ld a, e
ld (mask), a ;store the mask
putSpriteLoop1:
ld a, (mask)
ld d, a
ld a,(ix) ;load part of image into a
and d ;mask image!
ld d, a
ld e,$00 ;de together are the 8-bit image
ld a,c
or a
jr z,putSpriteSkip1
putSpriteLoop2:
srl d ;move a bit from d to e
rr e
dec a ;based on the bit offset (c) times
jr nz,putSpriteLoop2
putSpriteSkip1:
ld a,(hl)
xor d ;xor d part of image & save
ld (hl),a
inc hl
ld a,(hl)
xor e ;xor e part of image & save
ld (hl),a
ld de,$0B
add hl,de ;move pointer to the next line
inc ix
djnz putSpriteLoop1
ret