Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/gfx/sprite_oam.asm
1271 views
1
PrepareOAMData::
2
; Determine OAM data for currently visible
3
; sprites and write it to wShadowOAM.
4
5
ld a, [wUpdateSpritesEnabled]
6
dec a
7
jr z, .updateEnabled
8
9
cp -1
10
ret nz
11
ld [wUpdateSpritesEnabled], a
12
jp HideSprites
13
14
.updateEnabled
15
xor a
16
ldh [hOAMBufferOffset], a
17
18
.spriteLoop
19
ldh [hSpriteOffset2], a
20
21
ld d, HIGH(wSpriteStateData1)
22
ldh a, [hSpriteOffset2]
23
ld e, a
24
ld a, [de] ; [x#SPRITESTATEDATA1_PICTUREID]
25
and a
26
jp z, .nextSprite
27
28
inc e
29
inc e
30
ld a, [de] ; [x#SPRITESTATEDATA1_IMAGEINDEX]
31
ld [wSavedSpriteImageIndex], a
32
cp $ff ; off-screen (don't draw)
33
jr nz, .visible
34
35
call GetSpriteScreenXY
36
jr .nextSprite
37
38
.visible
39
cp $a0 ; is the sprite unchanging like an item ball or boulder?
40
jr c, .usefacing
41
42
; unchanging
43
and $f
44
add $10 ; skip to the second half of the table which doesn't account for facing direction
45
jr .next
46
47
.usefacing
48
and $f
49
50
.next
51
ld l, a
52
53
; get sprite priority
54
push de
55
inc d
56
ld a, e
57
add $5
58
ld e, a
59
ld a, [de] ; [x#SPRITESTATEDATA2_GRASSPRIORITY]
60
and $80
61
ldh [hSpritePriority], a ; temp store sprite priority
62
pop de
63
64
; read the entry from the table
65
ld h, 0
66
ld bc, SpriteFacingAndAnimationTable
67
add hl, hl
68
add hl, hl
69
add hl, bc
70
ld a, [hli]
71
ld c, a
72
ld a, [hli]
73
ld b, a
74
ld a, [hli]
75
ld h, [hl]
76
ld l, a
77
78
call GetSpriteScreenXY
79
80
ldh a, [hOAMBufferOffset]
81
ld e, a
82
ld d, HIGH(wShadowOAM)
83
84
.tileLoop
85
ldh a, [hSpriteScreenY] ; temp for sprite Y position
86
add $10 ; Y=16 is top of screen (Y=0 is invisible)
87
add [hl] ; add Y offset from table
88
ld [de], a ; write new sprite OAM Y position
89
inc hl
90
ldh a, [hSpriteScreenX] ; temp for sprite X position
91
add $8 ; X=8 is left of screen (X=0 is invisible)
92
add [hl] ; add X offset from table
93
inc e
94
ld [de], a ; write new sprite OAM X position
95
inc e
96
ld a, [bc] ; read pattern number offset (accommodates orientation (offset 0,4 or 8) and animation (offset 0 or $80))
97
inc bc
98
push bc
99
ld b, a
100
101
ld a, [wSavedSpriteImageIndex]
102
swap a ; high nybble determines sprite used (0 is always player sprite, next are some npcs)
103
and $f
104
105
; Sprites $a and $b have one face (and therefore 4 tiles instead of 12).
106
; As a result, sprite $b's tile offset is less than normal.
107
cp $b
108
jr nz, .notFourTileSprite
109
ld a, $a * 12 + 4
110
jr .next2
111
112
.notFourTileSprite
113
; a *= 12
114
sla a
115
sla a
116
ld c, a
117
sla a
118
add c
119
120
.next2
121
add b ; add the tile offset from the table (based on frame and facing direction)
122
pop bc
123
ld [de], a ; tile id
124
inc hl
125
inc e
126
ld a, [hl]
127
bit BIT_SPRITE_UNDER_GRASS, a
128
jr z, .skipPriority
129
ldh a, [hSpritePriority]
130
or [hl]
131
.skipPriority
132
inc hl
133
ld [de], a
134
inc e
135
bit BIT_END_OF_OAM_DATA, a
136
jr z, .tileLoop
137
138
ld a, e
139
ldh [hOAMBufferOffset], a
140
141
.nextSprite
142
ldh a, [hSpriteOffset2]
143
add $10
144
cp LOW($100)
145
jp nz, .spriteLoop
146
147
; Clear unused OAM.
148
ldh a, [hOAMBufferOffset]
149
ld l, a
150
ld h, HIGH(wShadowOAM)
151
ld de, $4
152
ld b, $a0
153
ld a, [wMovementFlags]
154
bit BIT_LEDGE_OR_FISHING, a
155
ld a, $a0
156
jr z, .clear
157
158
; Don't clear the last 4 entries because they are used for the shadow in the
159
; jumping down ledge animation and the rod in the fishing animation.
160
ld a, $90
161
162
.clear
163
cp l
164
ret z
165
ld [hl], b
166
add hl, de
167
jr .clear
168
169
GetSpriteScreenXY:
170
inc e
171
inc e
172
ld a, [de] ; [x#SPRITESTATEDATA1_YPIXELS]
173
ldh [hSpriteScreenY], a
174
inc e
175
inc e
176
ld a, [de] ; [x#SPRITESTATEDATA1_XPIXELS]
177
ldh [hSpriteScreenX], a
178
ld a, 4
179
add e
180
ld e, a
181
ldh a, [hSpriteScreenY]
182
add 4
183
and $f0
184
ld [de], a ; [x#SPRITESTATEDATA1_YADJUSTED]
185
inc e
186
ldh a, [hSpriteScreenX]
187
and $f0
188
ld [de], a ; [x#SPRITESTATEDATA1_XADJUSTED]
189
ret
190
191