Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- spritescale_graymask:
- ;;Input:
- ;; IX -> sprite
- ;; D = x-coordinate
- ;; E = y-coordinate
- ;; A = scale factor ($01 to $00 <=> 0.4% to 100%)
- ;; eg: $80 is 50%
- ;;Requirements:
- ;; This routine must be called in RAM, since it uses Self-Modifying Code (SMC)
- ;; Sprite format is one byte for byte-width, one byte for height, followed by the data.
- ;; The sprite data is formed in 3-byte chunks:
- ;; The first byte comes from the mask.
- ;; The second byte from one layer.
- ;; The third byte from the next layer.
- ;; Shuffle the bytes of all three layers in this way
- ;Load everything to RAM (SMC)
- ld (scalefactor),a
- ;Check window bounds while we are at it. Start with y.
- inc ix
- inc ix
- ld a,(ix-1)
- or a
- ret z
- ld (unscaled_height),a
- ld c,a
- ld a,e
- ld (coord_y),a
- cp 64
- jr c,$+4
- add a,c
- ret nc
- ;Check x bounds
- ld a,(ix-2)
- ld (byte_width),a
- add a,a
- ret z
- add a,a
- add a,a
- ld c,a
- ld a,d
- cp 96
- jr c,$+4
- add a,c
- ret nc
- ld a,d
- dec a
- ld (coord_x_save),a
- ;find buffer offset, allowing signed coordinates
- ld a,e
- add a,a
- sbc a,a
- ld h,a
- ld a,d
- ld d,h
- ld l,e
- add hl,hl
- add hl,de
- add hl,hl
- add hl,hl
- ld e,a
- add a,a
- sbc a,a
- ld d,a
- sra e
- sra e
- sra e
- add hl,de
- ld (buf_offset_save),hl
- ld (buf_offset),hl
- ;calculate the pixel mask for the buffer
- ld a,(coord_x_save)
- inc a
- and 7
- ld b,a
- ld a,%10000000
- jr z,$+5
- rrca \ djnz $-1
- ld (pmask_buf_save),a
- ;;Ideally here, we would increment the Y coordinate until it is on screen
- ;;save that for later and hopefully I don't forget.
- ;;If you still see this note, remind Zeda on FB or xedaelnara@gmail.com or Omnimaga or Cemetech
- ;;Also, remember to remove the y coordinate check in the pixel plotting part
- unscaled_height = $+1
- ld c,0
- colLoop:
- ;Here is where we start looping through the sprite, pixel-by-pixel
- coord_x_save = $+1
- ld a,0
- ld (coord_x),a
- pmask_buf_save = $+1
- ld a,0
- ld (pmask_buf),a
- byte_width = $+1
- ld b,0
- rowLoop:
- push bc
- buf_offset = $+1
- ld de,0
- xor a
- ld (spriteratio),a
- drawLoop:
- spriteratio = $+1
- pmask_sprite = $+2
- ld bc,$8000
- scalefactor = $+1
- ld a,0
- add a,c
- ld (spriteratio),a
- jr c,rotspritemask
- pmask_buf = $+1
- ld c,0
- ;;Check coord_x bounds and coord_y
- coord_x = $+1
- ld a,0
- inc a
- ld (coord_x),a
- cp 96
- jr nc,rotbufmask
- ld a,(coord_y)
- cp 64
- jr nc,y_oob
- ld a,b
- and (ix)
- jr nz,layer1
- ld hl,buf0 \ add hl,de \ ld a,c \ cpl \ and (hl) \ ld (hl),a
- ld hl,buf1 \ add hl,de \ ld a,c \ cpl \ and (hl) \ ld (hl),a
- layer1:
- ld a,b
- and (ix+1)
- jr z,layer2
- ld hl,buf0 \ add hl,de \ ld a,(hl) \ or c \ ld (hl),a
- layer2:
- ld a,b
- and (ix+2)
- jr z,rotbufmask
- ld hl,buf1 \ add hl,de \ ld a,(hl) \ or c \ ld (hl),a
- rotbufmask:
- ld a,c
- rrca \ jr nc,$+7 \ inc de \ ld (buf_offset),de
- ld (pmask_buf),a
- rotspritemask:
- ld a,b
- rrca
- ld (pmask_sprite),a
- jr nc,drawloop
- y_oob:
- pop bc
- ld de,3
- add ix,de
- djnz rowLoop
- ;increment y pixel for buf, if it hits 64, exit early
- coord_y = $+1
- ld a,0
- inc a
- cp 64
- ret z
- ld (coord_y),a
- buf_offset_save = $+1
- ld hl,0
- ld de,12
- add hl,de
- ld (buf_offset),hl
- ld (buf_offset_save),hl
- ;increment y pixel for sprite using scaling
- ld de,(scalefactor) \ ld a,(spriteratio)
- dec c \ ret z \ add a,e \ jr c,$-3
- ld (spriteratio),a
- jp colLoop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement