Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
sprite.c
Go to the documentation of this file.
1/* The training's sprites (TrainSprite), which begin STGTRAIN.PRO's first
2 object. STGTRAIN.PRO was at least three objects: each one's jump tables are
3 aligned to 8 from the start of its own rodata, and the tables at 0x8008251C
4 and 0x800825E8 (USA) each start right where the one before ends, 4 bytes past
5 a multiple of 8, so each one starts a new object. Where each object's code
6 starts is only known to be between the function with the last jump table of
7 the object before and the one with its first: here at the training result
8 (result.c) and the session (session.c). Each object's data is in its last
9 module */
10
11#include "menus/gym.h"
12
13/* Sets the sprite bank and where the sprites start in it */
15 sprite->bank = bank;
16 sprite->bankOffset = offset;
17 sprite->unk84 = bank->unk2;
18 sprite->spriteCount = bank->spriteCount;
19}
20
21/* Starts an animation from its first frame */
23 if (anim != NULL) {
24 sprite->anim = anim;
25 sprite->frame = 0;
26 sprite->frameTime = 0;
27 sprite->frameCount = anim->frameCount;
28 sprite->flags = 0;
29 }
30}
31
32/* Sets the layer and depth the sprite is drawn at */
33void STGTRAIN_setSpriteLayer(TrainSprite *sprite, s32 layerId, s32 depth) {
34 sprite->layerId = layerId;
35 sprite->depth = depth;
36}
37
38/* Sets where the sprite is drawn */
40 sprite->x = x;
41 sprite->y = y;
42}
43
44/* Sets where the sprite's image is in VRAM */
46 sprite->imageX = x;
47 sprite->imageY = y;
48}
49
50/* Sets where the sprite's CLUT is in VRAM */
52 sprite->clutX = x;
53 sprite->clutY = y;
54}
55
56/* Scales the sprite (0x1000 is its size), drawing it as quads */
58 sprite->scale.vx = x;
59 sprite->scale.vy = y;
60 sprite->scale.vz = z;
61 sprite->transformed = 1;
62}
63
64/* Sets the point the sprite scales and turns about */
66 sprite->pivotX = x;
67 sprite->pivotY = y;
68}
69
70/* Turns the sprite, drawing it as quads */
72 sprite->rotation.vx = x;
73 sprite->rotation.vy = y;
74 sprite->rotation.vz = z;
75 sprite->transformed = 1;
76}
77
78/* Whether the animation has ended (the flags but for the pause) */
80 return sprite->flags & 0x7FFFFFFF;
81}
82
83/* Pauses the animation (paused) or plays it on */
85 if (paused) {
86 sprite->flags |= 0x80000000;
87 } else {
88 sprite->flags &= 0x7FFFFFFF;
89 }
90}
91
92/* Fills in a part of the sprite as a sprite, and adds it to the sprite's
93 ordering table */
94static inline void STGTRAIN_addSpriteSprt(TrainSprite *sprite, SPRT *sprt, TrainSpritePart *part,
95 TrainAnimFrame *frame, s32 x, s32 y, u8 u, u8 v, u16 w, u16 h,
96 u16 clut) {
97 setSprt(sprt);
98 if ((s16)part->clut & 0x8000) {
99 setSemiTrans(sprt, 1);
100 }
101 setRGB0(sprt, 0x80, 0x80, 0x80);
102 sprt->x0 = x + (frame->x + sprite->x);
103 sprt->y0 = y + (frame->y + sprite->y);
104 sprt->u0 = u;
105 sprt->v0 = v;
106 sprt->w = w;
107 sprt->h = h;
108 sprt->clut = clut;
109 addPrim(sprite->ot, sprt);
110}
111
112/* Fills in a part of the sprite as a quad, scaled and rotated about the
113 pivot, and adds it to the sprite's ordering table */
114static inline void STGTRAIN_addSpriteQuad(TrainSprite *sprite, POLY_FT4 *ft4, TrainSpritePart *part,
115 TrainAnimFrame *frame, s32 x, s32 y, u8 u, u8 v, u16 w, u16 h,
116 u16 tpage, u16 clut) {
117 SVECTOR out;
118 SVECTOR in[4];
119 s32 k;
120
121 setPolyFT4(ft4);
122 if ((s16)part->clut & 0x8000) {
123 setSemiTrans(ft4, 1);
124 }
125 setRGB0(ft4, 0x80, 0x80, 0x80);
126 in[0].vx = in[2].vx = x + (frame->x + sprite->x) - sprite->pivotX;
127 in[1].vx = in[3].vx = in[0].vx + w;
128 in[0].vy = in[1].vy = y + (frame->y + sprite->y) - sprite->pivotY;
129 in[2].vy = in[3].vy = in[0].vy + h;
130 in[0].vz = in[1].vz = in[2].vz = in[3].vz = 0;
131 for (k = 0; k < 4; k++) {
132 ApplyMatrixSV(&sprite->matrix, &in[k], &out);
133 (&ft4->x0)[k * 4] = out.vx + sprite->pivotX;
134 (&ft4->y0)[k * 4] = out.vy + sprite->pivotY;
135 }
136 ft4->u0 = ft4->u2 = u;
137 ft4->u1 = ft4->u3 = w + u - 1;
138 ft4->v0 = ft4->v1 = v;
139 ft4->v2 = ft4->v3 = h + v - 1;
140 ft4->tpage = tpage;
141 ft4->clut = clut;
142 addPrim(sprite->ot, ft4);
143}
144
145/*
146 * The animated sprite's update: state 1 advances the animation, unless it
147 * is paused, and draws the frame's sprite, its parts from the last to the
148 * first, as sprites or, scaled and rotated about the pivot, as quads.
149 * The match depends on cur holding the animation before its frames.
150 */
152 union {
153 TrainAnim *anim;
154 TrainAnimFrame *frame;
155 } cur; /* the animation, then its frame */
156 union {
157 TrainSpriteBank *bank;
158 u8 *bytes;
159 s32 *count;
160 TrainSpritePart *part;
161 } p; /* through the bank: each sprite is a count and its parts */
162 TrainSpritePart *part;
163 s32 n;
164 s32 count;
165 s32 i;
166 s32 j;
167 s32 state;
168 u16 info;
169 u16 w;
170 u16 h;
171 u16 tpage;
172 u16 prevTpage;
173 u16 clut;
174 s32 transform;
175 u8 u;
176 u8 v;
177 s32 x;
178 s32 y;
179 union {
180 void *ptr;
181 SPRT *sprt;
182 POLY_FT4 *ft4;
183 DR_TPAGE *tpage;
184 } prim;
185
186 state = sprite->state;
187 switch (state) {
188 case 0:
189 default:
190 sprite->nextState(sprite);
191 sprite->frameTime = GFX.funcs.getTime();
192 break;
193 case 1:
194 if (sprite->bank == NULL) {
195 break;
196 }
197 if (sprite->anim == NULL) {
198 break;
199 }
200 cur.anim = sprite->anim;
201 cur.frame = cur.anim->frames;
202 cur.frame += sprite->frame;
203 if (sprite->flags >= 0 && GFX.funcs.getTime() - sprite->frameTime > cur.frame->duration) {
204 sprite->frameTime = GFX.funcs.getTime();
205 if (++sprite->frame >= sprite->frameCount - 1) {
206 sprite->frame = sprite->frameCount - 1;
207 sprite->flags = 1;
208 }
209 cur.anim = sprite->anim;
210 cur.frame = cur.anim->frames;
211 cur.frame += sprite->frame;
212 }
213 p.bank = sprite->bank;
214 p.bytes += sprite->bankOffset;
215 count = cur.frame->sprite;
216 for (i = 0; i < count; i++) {
217 n = *p.count++;
218 for (j = 0; j < n; j++) {
219 p.part++;
220 }
221 }
222 tpage = 0;
223 prevTpage = 0;
224 transform = 0;
225 if (sprite->transformed) {
226 if (sprite->scale.vx == 0 && sprite->scale.vy == 0) {
227 break;
228 }
229 if (sprite->scale.vx == ONE && sprite->scale.vy == ONE) {
230 sprite->transformed = 0;
231 } else {
232 transform = 1;
233 RotMatrixYXZ_gte(&sprite->rotation, &sprite->matrix);
234 ScaleMatrix(&sprite->matrix, &sprite->scale);
235 }
236 }
237 sprite->layer = GFX.funcs.getLayer(sprite->layerId);
238 sprite->ot = sprite->layer->getOtEntry(sprite->layer, sprite->depth);
239 prim.ptr = GFX.funcs.getPrim();
240 n = *p.count++;
241 for (i = 0; i < n; i++) {
242 p.part++;
243 }
244 part = p.part;
245 for (i = 0; i < n; i++) {
246 part--;
247 info = part->tpage;
248 clut = getClut(sprite->clutX + (info & 0x1F) * 16, sprite->clutY + ((part->clut & 0x7FC0) >> 6));
249 tpage = getTPage(info >> 7, info >> 5, sprite->imageX + (info & 0x1F) * 64, sprite->imageY);
250 u = part->u;
251 x = part->x;
252 y = part->y;
253 v = part->v;
254 w = part->w;
255 h = part->h;
256 if (!transform) {
257 if (i == 0) {
258 prevTpage = tpage;
259 }
260 if (prevTpage != tpage) {
261 SetDrawTPage(prim.tpage, 0, 1, prevTpage);
262 addPrim(sprite->ot, prim.ptr);
263 prim.tpage++;
264 prevTpage = tpage;
265 }
266 STGTRAIN_addSpriteSprt(sprite, prim.sprt, part, cur.frame, x, y, u, v, w, h, clut);
267 prim.sprt++;
268 } else {
269 STGTRAIN_addSpriteQuad(sprite, prim.ft4, part, cur.frame, x, y, u, v, w, h, tpage, clut);
270 prim.ft4++;
271 }
272 }
273 if (!transform) {
274 SetDrawTPage(prim.tpage, 0, 1, tpage);
275 addPrim(sprite->ot, prim.ptr);
276 prim.tpage++;
277 }
278 GFX.funcs.setPrim(prim.ptr);
279 break;
280 case 2:
281 case 3:
282 break;
283 }
284}
285
286/* Creates an animated sprite */
#define NULL
Definition common.h:17
signed int s32
Definition common.h:11
signed short s16
Definition common.h:9
unsigned short u16
Definition common.h:10
unsigned char u8
Definition common.h:8
GfxState GFX
GFX: the graphics state and its methods.
Definition display.c:209
STGTRAIN.PRO: the gyms, where the partners train their stats.
void STGTRAIN_setSpriteScale(TrainSprite *sprite, s32 x, s32 y, s32 z)
Scales the sprite (0x1000 is its size), drawing it as quads.
Definition sprite.c:57
void STGTRAIN_setSpritePaused(TrainSprite *sprite, s32 paused)
Pauses the animation (paused) or plays it on.
Definition sprite.c:84
void STGTRAIN_setSpriteBank(TrainSprite *sprite, TrainSpriteBank *bank, s32 offset)
Sets the sprite bank and where the sprites start in it.
Definition sprite.c:14
void STGTRAIN_setSpriteClutPos(TrainSprite *sprite, s32 x, s32 y)
Sets where the sprite's CLUT is in VRAM.
Definition sprite.c:51
static void STGTRAIN_addSpriteQuad(TrainSprite *sprite, POLY_FT4 *ft4, TrainSpritePart *part, TrainAnimFrame *frame, s32 x, s32 y, u8 u, u8 v, u16 w, u16 h, u16 tpage, u16 clut)
Fills in a part of the sprite as a quad, scaled and rotated about the pivot, and adds it to the sprit...
Definition sprite.c:114
void STGTRAIN_setSpritePivot(TrainSprite *sprite, s32 x, s32 y)
Sets the point the sprite scales and turns about.
Definition sprite.c:65
s32 STGTRAIN_getSpriteFlags(TrainSprite *sprite)
Whether the animation has ended (the flags but for the pause)
Definition sprite.c:79
TrainSprite * STGTRAIN_createSprite(void)
Creates an animated sprite.
Definition sprite.c:287
void STGTRAIN_setSpriteImagePos(TrainSprite *sprite, s32 x, s32 y)
Sets where the sprite's image is in VRAM.
Definition sprite.c:45
void STGTRAIN_setSpriteAnim(TrainSprite *sprite, TrainAnim *anim)
Starts an animation from its first frame.
Definition sprite.c:22
void STGTRAIN_setSpriteLayer(TrainSprite *sprite, s32 layerId, s32 depth)
Sets the layer and depth the sprite is drawn at.
Definition sprite.c:33
void STGTRAIN_setSpriteRotation(TrainSprite *sprite, s16 x, s16 y, s16 z)
Turns the sprite, drawing it as quads.
Definition sprite.c:71
void STGTRAIN_setSpritePos(TrainSprite *sprite, s32 x, s32 y)
Sets where the sprite is drawn.
Definition sprite.c:39
static void STGTRAIN_addSpriteSprt(TrainSprite *sprite, SPRT *sprt, TrainSpritePart *part, TrainAnimFrame *frame, s32 x, s32 y, u8 u, u8 v, u16 w, u16 h, u16 clut)
Fills in a part of the sprite as a sprite, and adds it to the sprite's ordering table.
Definition sprite.c:94
void STGTRAIN_updateSprite(TrainSprite *sprite)
The animated sprite's update: state 1 advances the animation, unless it is paused,...
Definition sprite.c:151
void *(* getPrim)(void)
Definition gfx.h:19
struct Layer *(* getLayer)(s32 id)
Definition gfx.h:28
void(* setPrim)(void *next)
Definition gfx.h:20
s32(* getTime)(void)
Definition gfx.h:31
GfxFuncs funcs
Definition gfx.h:77
u_long *(* getOtEntry)(struct Layer *, s32 depth)
Definition gfx.h:155
A frame of a sprite animation.
Definition gym.h:26
A sprite animation: its frames follow the header.
Definition gym.h:34
s16 frameCount
Definition gym.h:36
TrainAnimFrame frames[1]
Definition gym.h:37
A sprite bank's header, as the animated sprite reads it: its sprites start bankOffset bytes from it (...
Definition gym.h:57
u16 spriteCount
Definition gym.h:60
u16 unk2
0 in every bank
Definition gym.h:59
A part of a sprite: a rectangle of a texture page.
Definition gym.h:42
u16 tpage
bits 0-4: the page's column, 64 pixels each; 5-8: getTPage's abr and tp
Definition gym.h:48
u16 clut
bits 6-14: the CLUT's row; bit 15: semi-transparent
Definition gym.h:47
Draws an animated sprite, maybe rotated and scaled (STGTRAIN_createSprite)
Definition gym.h:64
void(* nextState)(struct TrainSprite *task)
Definition gym.h:65
s32 y
Definition gym.h:71
s32 pivotY
Definition gym.h:88
s32 imageY
Definition gym.h:73
void(* setBank)(struct TrainSprite *sprite, TrainSpriteBank *bank, s32 offset)
Definition gym.h:92
s32 flags
bit 31: paused; the rest, ended
Definition gym.h:76
void(* setScale)(struct TrainSprite *sprite, s32 x, s32 y, s32 z)
Definition gym.h:98
s32 bankOffset
Definition gym.h:81
MATRIX matrix
Definition gym.h:91
s32 transformed
Definition gym.h:86
void(* setRotation)(struct TrainSprite *sprite, s16 x, s16 y, s16 z)
Definition gym.h:100
u16 unk84
the bank's unk2 and spriteCount: nothing reads them
Definition gym.h:79
s32 x
Definition gym.h:70
void(* setAnim)(struct TrainSprite *sprite, TrainAnim *anim)
Definition gym.h:93
s32 clutX
Definition gym.h:74
void(* setClutPos)(struct TrainSprite *sprite, s32 x, s32 y)
Definition gym.h:97
s32 depth
Definition gym.h:69
s32(* getFlags)(struct TrainSprite *sprite)
Definition gym.h:101
s32 state
Definition gym.h:65
u_long * ot
Definition gym.h:67
TrainSpriteBank * bank
Definition gym.h:78
SVECTOR rotation
Definition gym.h:90
s32 clutY
Definition gym.h:75
s32 frameCount
Definition gym.h:84
VECTOR scale
Definition gym.h:89
void(* setImagePos)(struct TrainSprite *sprite, s32 x, s32 y)
Definition gym.h:95
void(* setPivot)(struct TrainSprite *sprite, s32 x, s32 y)
Definition gym.h:99
u16 spriteCount
Definition gym.h:80
s32 frameTime
Definition gym.h:85
void(* setPos)(struct TrainSprite *sprite, s32 x, s32 y)
Definition gym.h:94
Layer * layer
Definition gym.h:66
s32 imageX
Definition gym.h:72
void(* setLayer)(struct TrainSprite *sprite, s32 layerId, s32 depth)
Definition gym.h:96
void(* setPaused)(struct TrainSprite *sprite, s32 paused)
Definition gym.h:102
s32 pivotX
Definition gym.h:87
s32 layerId
Definition gym.h:68
s32 frame
Definition gym.h:83
TrainAnim * anim
Definition gym.h:82
void * createTask(void(*update)(), s32 size, s32 childrenSize)
createTaskWithId for a task nothing needs to find
Definition task.c:105