daily pastebin goal
5%
SHARE
TWEET

spritescale_graymask

Zeda Feb 11th, 2015 211 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. spritescale_graymask:
  2. ;;Input:
  3. ;;   IX -> sprite
  4. ;;    D = x-coordinate
  5. ;;    E = y-coordinate
  6. ;;    A = scale factor ($01 to $00 <=> 0.4% to 100%)
  7. ;;        eg: $80 is 50%
  8. ;;Requirements:
  9. ;;    This routine must be called in RAM, since it uses Self-Modifying Code (SMC)
  10. ;;    Sprite format is one byte for byte-width, one byte for height, followed by the data.
  11. ;;    The sprite data is formed in 3-byte chunks:
  12. ;;       The first byte comes from the mask.
  13. ;;       The second byte from one layer.
  14. ;;       The third byte from the next layer.
  15. ;;    Shuffle the bytes of all three layers in this way
  16. ;Load everything to RAM (SMC)
  17.     ld (scalefactor),a
  18. ;Check window bounds while we are at it. Start with y.
  19.     inc ix
  20.     inc ix
  21.     ld a,(ix-1)
  22.     or a
  23.     ret z
  24.     ld (unscaled_height),a
  25.     ld c,a
  26.     ld a,e
  27.     ld (coord_y),a
  28.     cp 64
  29.     jr c,$+4
  30.     add a,c
  31.     ret nc
  32. ;Check x bounds
  33.     ld a,(ix-2)
  34.     ld (byte_width),a
  35.     add a,a
  36.     ret z
  37.     add a,a
  38.     add a,a
  39.     ld c,a
  40.     ld a,d
  41.     cp 96
  42.     jr c,$+4
  43.     add a,c
  44.     ret nc
  45.     ld a,d
  46.     dec a
  47.     ld (coord_x_save),a
  48. ;find buffer offset, allowing signed coordinates
  49.     ld a,e
  50.     add a,a
  51.     sbc a,a
  52.     ld h,a
  53.     ld a,d
  54.     ld d,h
  55.     ld l,e
  56.     add hl,hl
  57.     add hl,de
  58.     add hl,hl
  59.     add hl,hl
  60.     ld e,a
  61.     add a,a
  62.     sbc a,a
  63.     ld d,a
  64.     sra e
  65.     sra e
  66.     sra e
  67.     add hl,de
  68.     ld (buf_offset_save),hl
  69.     ld (buf_offset),hl
  70. ;calculate the pixel mask for the buffer
  71.     ld a,(coord_x_save)
  72.     inc a
  73.     and 7
  74.     ld b,a
  75.     ld a,%10000000
  76.     jr z,$+5
  77.     rrca \ djnz $-1
  78.     ld (pmask_buf_save),a
  79.  
  80.  
  81. ;;Ideally here, we would increment the Y coordinate until it is on screen
  82. ;;save that for later and hopefully I don't forget.
  83. ;;If you still see this note, remind Zeda on FB or xedaelnara@gmail.com or Omnimaga or Cemetech
  84. ;;Also, remember to remove the y coordinate check in the pixel plotting part
  85.  
  86.  
  87. unscaled_height = $+1
  88.     ld c,0
  89. colLoop:
  90. ;Here is where we start looping through the sprite, pixel-by-pixel
  91. coord_x_save = $+1
  92.     ld a,0
  93.     ld (coord_x),a
  94. pmask_buf_save = $+1
  95.     ld a,0
  96.     ld (pmask_buf),a
  97. byte_width = $+1
  98.     ld b,0
  99. rowLoop:
  100.     push bc
  101. buf_offset = $+1
  102.     ld de,0
  103.     xor a
  104.     ld (spriteratio),a
  105. drawLoop:
  106. spriteratio = $+1
  107. pmask_sprite = $+2
  108.     ld bc,$8000
  109. scalefactor = $+1
  110.     ld a,0
  111.     add a,c
  112.     ld (spriteratio),a
  113.     jr c,rotspritemask
  114. pmask_buf = $+1
  115.     ld c,0
  116. ;;Check coord_x bounds and coord_y
  117. coord_x = $+1
  118.     ld a,0
  119.     inc a
  120.     ld (coord_x),a
  121.     cp 96
  122.     jr nc,rotbufmask
  123.     ld a,(coord_y)
  124.     cp 64
  125.     jr nc,y_oob
  126.     ld a,b
  127.     and (ix)
  128.     jr nz,layer1
  129.     ld hl,buf0 \ add hl,de \ ld a,c \ cpl \ and (hl) \ ld (hl),a
  130.     ld hl,buf1 \ add hl,de \ ld a,c \ cpl \ and (hl) \ ld (hl),a
  131. layer1:
  132.     ld a,b
  133.     and (ix+1)
  134.     jr z,layer2
  135.     ld hl,buf0 \ add hl,de \ ld a,(hl) \ or c \ ld (hl),a
  136. layer2:
  137.     ld a,b
  138.     and (ix+2)
  139.     jr z,rotbufmask
  140.     ld hl,buf1 \ add hl,de \ ld a,(hl) \ or c \ ld (hl),a
  141.  
  142. rotbufmask:
  143.     ld a,c
  144.     rrca \ jr nc,$+7 \ inc de \ ld (buf_offset),de
  145.     ld (pmask_buf),a
  146. rotspritemask:
  147.     ld a,b
  148.     rrca
  149.     ld (pmask_sprite),a
  150.     jr nc,drawloop
  151. y_oob:
  152.     pop bc
  153.     ld de,3
  154.     add ix,de
  155.     djnz rowLoop
  156. ;increment y pixel for buf, if it hits 64, exit early
  157. coord_y = $+1
  158.     ld a,0
  159.     inc a
  160.     cp 64
  161.     ret z
  162.     ld (coord_y),a
  163. buf_offset_save = $+1
  164.     ld hl,0
  165.     ld de,12
  166.     add hl,de
  167.     ld (buf_offset),hl
  168.     ld (buf_offset_save),hl
  169. ;increment y pixel for sprite using scaling
  170.     ld de,(scalefactor) \ ld a,(spriteratio)
  171.     dec c \ ret z \ add a,e \ jr c,$-3
  172.     ld (spriteratio),a
  173.     jp colLoop
RAW Paste Data
Top