Advertisement
luckytyphlosion

better oam code

Mar 30th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PrepareOAMData: ; 499b (1:499b)
  2. ; Determine OAM data for currently visible
  3. ; sprites and write it to wOAMBuffer.
  4. ; Yellow code has been changed to use registers more efficiently
  5. ; as well as tweaking the code to show gbc palettes
  6.  
  7.     ld a, [wUpdateSpritesEnabled]
  8.     dec a
  9.     jr z, .updateEnabled
  10.  
  11.     cp $ff
  12.     ret nz
  13.     ld [wUpdateSpritesEnabled], a
  14.     jp HideSprites
  15.  
  16. .updateEnabled
  17.     xor a
  18.     ld [hOAMBufferOffset], a
  19.  
  20. .spriteLoop
  21.     ld [hSpriteOffset2], a
  22.    
  23.     ld e, a
  24.     ld d, wSpriteStateData1 / $100
  25.    
  26.     ld a, [de] ; c1x0
  27.     and a
  28.     jp z, .nextSprite
  29.  
  30.     inc e
  31.     inc e
  32.     ld a, [de] ; c1x2 (facing/anim)
  33.     ld [wd5cd], a
  34.     cp $ff ; off-screen (don't draw)
  35.     jr nz, .visible
  36.  
  37.     call GetSpriteScreenXY
  38.     jr .nextSprite
  39.  
  40. .visible
  41.     cp $a0 ; is the sprite unchanging like an item ball or boulder?
  42.     jr c, .usefacing
  43.  
  44. ; unchanging
  45.     xor a
  46.     jr .next
  47.  
  48. .usefacing
  49.     and $f
  50.  
  51. .next
  52. ; read the entry from the table
  53.     add a
  54.     ld c, a
  55.     ld b, 0
  56.     ld hl, SpriteFacingAndAnimationTable
  57.     add hl, bc
  58.     ld a, [hli]
  59.     ld h, [hl]
  60.     ld l, a
  61. ; get sprite priority
  62.     ld b, d
  63.     ld c, e
  64.    
  65.     inc b
  66.     ld a, c
  67.     add $5
  68.     ld c, a
  69.     ld a, [bc] ; c2x7
  70.     and $80
  71.     ld [hSpritePriority], a ; temp store sprite priority
  72.    
  73.     call GetSpriteScreenXY
  74.    
  75.     ld a, [wd5cd]            ; temp copy of c1x2
  76.     swap a                   ; high nybble determines sprite used (0 is always player sprite, next are some npcs)
  77.     and $f
  78.  
  79.     ; Sprites $a and $b have one face (and therefore 4 tiles instead of 12).
  80.     ; As a result, sprite $b's tile offset is less than normal.
  81.     cp $b
  82.     jr nz, .notFourTileSprite
  83.     ld a, $a * 12 + 4 ; $7c
  84.     jr .startTileLoop
  85.  
  86. .notFourTileSprite
  87.     ; a *= 12
  88.     add a
  89.     add a
  90.     ld c, a
  91.     add a
  92.     add c
  93. .startTileLoop
  94.     ld b, a
  95.    
  96.     ld a, [hOAMBufferOffset]
  97.     ld e, a
  98.     ld d, wOAMBuffer / $100
  99.    
  100.     ld c, $4
  101. .loop
  102.     ld a, [hSpriteScreenY]   ; temp for sprite Y position
  103.     add $10                  ; Y=16 is top of screen (Y=0 is invisible)
  104.     add [hl]                 ; add Y offset from table
  105.     ld [de], a               ; write new sprite OAM Y position
  106.     inc hl
  107.     inc e
  108.     ld a, [hSpriteScreenX]   ; temp for sprite X position
  109.     add $8                   ; X=8 is left of screen (X=0 is invisible)
  110.     add [hl]                 ; add X offset from table
  111.     ld [de], a
  112.     inc hl
  113.     inc e
  114.     ld a, [hli]
  115.     add b
  116.     ld [de], a ; tile id
  117.     inc e
  118.     ld a, [hl]
  119.     bit 1, a ; is the tile allowed to set the sprite priority bit?
  120.     jr z, .skipPriority
  121.     ld a, [hSpritePriority]
  122.     or [hl]
  123. .skipPriority
  124. ; uncomment the following range for pal conversion
  125. ;   and $f0
  126. ;   bit 4, a ; OBP0 or OBP1
  127. ;   jr z, .spriteusesOBP0
  128. ;   or %100 ; palettes 4-7 are OBP1
  129. ;.spriteusesOBP0
  130.     ld [de], a
  131.     inc hl
  132.     inc e
  133.     dec c
  134.     jr nz, .loop
  135.  
  136.     ld a, e
  137.     ld [hOAMBufferOffset], a
  138. .nextSprite
  139.     ld a, [hSpriteOffset2]
  140.     add $10
  141.     jp nz, .spriteLoop
  142.  
  143.     ; Clear unused OAM.
  144.     ld a, [wd736]
  145.     bit 6, a ; jumping down ledge or fishing animation?
  146.     ld c, $a0
  147.     jr z, .clear
  148.  
  149. ; Don't clear the last 4 entries because they are used for the shadow in the
  150. ; jumping down ledge animation and the rod in the fishing animation.
  151.     ld c, $90
  152.  
  153. .clear
  154.     ld a, [hOAMBufferOffset]
  155.     cp c
  156.     ret nc
  157.     ld l, a
  158.     ld h, wOAMBuffer / $100
  159.  
  160.     ld de, $4 ; entry size
  161.     ld b, $a0
  162. .clearLoop
  163.     ld [hl], b
  164.     add hl, de
  165.     cp l
  166.     jr nz, .clearLoop
  167.     ret
  168.  
  169. GetSpriteScreenXY: ; 4a5f (1:4a5f)
  170.     inc e
  171.     inc e
  172.     ld a, [de] ; c1x4
  173.     ld [hSpriteScreenY], a
  174.     inc e
  175.     inc e
  176.     ld a, [de] ; c1x6
  177.     ld [hSpriteScreenX], a
  178.     ld a, 4
  179.     add e
  180.     ld e, a
  181.     ld a, [hSpriteScreenY]
  182.     add 4
  183.     and $f0
  184.     ld [de], a ; c1xa (y)
  185.     inc e
  186.     ld a, [hSpriteScreenX]
  187.     and $f0
  188.     ld [de], a  ; c1xb (x)
  189.     ret
  190.  
  191. ======================================================================
  192.  
  193. SpriteFacingAndAnimationTable: ; 4000 (1:4000)
  194.     dw SpriteFacingDownAndStanding        ; facing down, walk animation frame 0
  195.     dw SpriteFacingDownAndWalking         ; facing down, walk animation frame 1
  196.     dw SpriteFacingDownAndStanding        ; facing down, walk animation frame 2
  197.     dw SpriteFacingDownAndWalking2        ; facing down, walk animation frame 3
  198.  
  199.     dw SpriteFacingUpAndStanding          ; facing up, walk animation frame 0
  200.     dw SpriteFacingUpAndWalking           ; facing up, walk animation frame 1
  201.     dw SpriteFacingUpAndStanding          ; facing up, walk animation frame 2
  202.     dw SpriteFacingUpAndWalking2          ; facing up, walk animation frame 3
  203.  
  204.     dw SpriteFacingLeftAndStanding        ; facing left, walk animation frame 0
  205.     dw SpriteFacingLeftAndWalking         ; facing left, walk animation frame 1
  206.     dw SpriteFacingLeftAndStanding        ; facing left, walk animation frame 2
  207.     dw SpriteFacingLeftAndWalking         ; facing left, walk animation frame 3
  208.    
  209.     dw SpriteFacingRightAndStanding       ; facing right, walk animation frame 0
  210.     dw SpriteFacingRightAndWalking        ; facing right, walk animation frame 1
  211.     dw SpriteFacingRightAndStanding       ; facing right, walk animation frame 2
  212.     dw SpriteFacingRightAndWalking        ; facing right, walk animation frame 3
  213.    
  214.     dw SpriteFacingDownAndStanding        ; ---
  215.     dw SpriteFacingDownAndStanding        ; This table is used for sprites $a and $b.
  216.     dw SpriteFacingDownAndStanding        ; All orientation and animation parameters
  217.     dw SpriteFacingDownAndStanding        ; lead to the same result. Used for immobile
  218.     dw SpriteFacingDownAndStanding        ; sprites like items on the ground
  219.     dw SpriteFacingDownAndStanding        ; ---
  220.     dw SpriteFacingDownAndStanding
  221.     dw SpriteFacingDownAndStanding
  222.     dw SpriteFacingDownAndStanding
  223.     dw SpriteFacingDownAndStanding
  224.     dw SpriteFacingDownAndStanding
  225.     dw SpriteFacingDownAndStanding
  226.     dw SpriteFacingDownAndStanding
  227.     dw SpriteFacingDownAndStanding
  228.     dw SpriteFacingDownAndStanding
  229.     dw SpriteFacingDownAndStanding
  230.  
  231. SpriteFacingDownAndStanding: ; 4042 (1:4042)
  232. ; Sprite OAM Parameters
  233.     db $00,$00,$00,$00                                      ; top left
  234.     db $00,$08,$01,$00                                      ; top right
  235.     db $08,$00,$02,OAMFLAG_CANBEMASKED                      ; bottom left
  236.     db $08,$08,$03,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  237.  
  238. SpriteFacingDownAndWalking: ; 4053 (1:4053)
  239. ; Sprite OAM Parameters
  240.     db $00,$00,$80,$00                                      ; top left
  241.     db $00,$08,$81,$00                                      ; top right
  242.     db $08,$00,$82,OAMFLAG_CANBEMASKED                      ; bottom left
  243.     db $08,$08,$83,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  244.  
  245. SpriteFacingDownAndWalking2: ; 4064 (1:4064)
  246. ; Sprite OAM Parameters
  247.     db $00,$08,$80,OAMFLAG_VFLIPPED                                           ; top left
  248.     db $00,$00,$81,OAMFLAG_VFLIPPED                                           ; top right
  249.     db $08,$08,$82,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED                     ; bottom left
  250.     db $08,$00,$83,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA ; bottom right
  251.  
  252. SpriteFacingUpAndStanding: ; 4075 (1:4075)
  253. ; Sprite OAM Parameters
  254.     db $00,$00,$04,$00                                      ; top left
  255.     db $00,$08,$05,$00                                      ; top right
  256.     db $08,$00,$06,OAMFLAG_CANBEMASKED                      ; bottom left
  257.     db $08,$08,$07,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  258.    
  259. SpriteFacingUpAndWalking: ; 4086 (1:4086)
  260. ; Sprite OAM Parameters
  261.     db $00,$00,$84,$00                                      ; top left
  262.     db $00,$08,$85,$00                                      ; top right
  263.     db $08,$00,$86,OAMFLAG_CANBEMASKED                      ; bottom left
  264.     db $08,$08,$87,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  265.  
  266. SpriteFacingUpAndWalking2: ; 4097 (1:4097)
  267. ; Sprite OAM Parameters
  268.     db $00,$08,$84,OAMFLAG_VFLIPPED                                           ; top left
  269.     db $00,$00,$85,OAMFLAG_VFLIPPED                                           ; top right
  270.     db $08,$08,$86,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED                     ; bottom left
  271.     db $08,$00,$87,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA ; bottom right
  272.    
  273. SpriteFacingLeftAndStanding: ; 40a8 (1:40a8)
  274. ; Sprite OAM Parameters
  275.     db $00,$00,$08,$00                                      ; top left
  276.     db $00,$08,$09,$00                                      ; top right
  277.     db $08,$00,$0a,OAMFLAG_CANBEMASKED                      ; bottom left
  278.     db $08,$08,$0b,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  279.  
  280. SpriteFacingLeftAndWalking: ; 40b9 (1:40b9)
  281. ; Sprite OAM Parameters
  282.     db $00,$00,$88,$00                                      ; top left
  283.     db $00,$08,$89,$00                                      ; top right
  284.     db $08,$00,$8a,OAMFLAG_CANBEMASKED                      ; bottom left
  285.     db $08,$08,$8b,OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA  ; bottom right
  286.  
  287. SpriteFacingRightAndStanding: ; 40ca (1:40ca)
  288. ; Sprite OAM Parameters
  289.     db $00,$08,$08,OAMFLAG_VFLIPPED                                           ; top left
  290.     db $00,$00,$09,OAMFLAG_VFLIPPED                                           ; top right
  291.     db $08,$08,$0a,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED                     ; bottom left
  292.     db $08,$00,$0b,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA ; bottom right
  293.    
  294. SpriteFacingRightAndWalking: ; 40db (1:40db)
  295. ; Sprite OAM Parameters
  296.     db $00,$08,$88,OAMFLAG_VFLIPPED                                           ; top left
  297.     db $00,$00,$89,OAMFLAG_VFLIPPED                                           ; top right
  298.     db $08,$08,$8a,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED                     ; bottom left
  299.     db $08,$00,$8b,OAMFLAG_VFLIPPED | OAMFLAG_CANBEMASKED | OAMFLAG_ENDOFDATA ; bottom right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement