Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
sprite_drawer.c
Go to the documentation of this file.
1#include "engine/game.h"
2#include <libgs.h>
3#include <libetc.h>
4#include <libsnd.h>
5
6/* Small variables, addressed through $gp (see the Makefile) */
8
9/* Makes `obj` the sprite drawer the methods work on */
11 SPRITE_DRAWER = obj;
12}
13
14/* Sprite drawer method: the VRAM position of the sheet's texture and CLUTs */
21
22/* Sprite drawer method: the other CLUT area, for parts that ask for it */
25 SPRITE_DRAWER->altClutY = y - 0x100;
26}
27
28/* Sprite drawer method: the CLUT row added to every part's */
32
33/* Sprite drawer method: the layer and depth to draw to */
34void spriteDrawerSetLayer(Layer *layer, s32 depth) {
35 SPRITE_DRAWER->layer = layer;
36 SPRITE_DRAWER->ot = layer->getOtEntry(layer, depth);
37}
38
39/* Sprite drawer method: the layer (by id) and depth to draw to */
42}
43
44/*
45 * Draws frame `frame` of a sprite sheet (see SpritePart) at (x, y): as
46 * sprites, changing the texture page between parts when needed, or as
47 * textured quads when the drawer is scaled or rotated.
48 */
49void spriteDrawerDraw(s32 *sheet, s32 frame, s32 x, s32 y) {
50 Vec2 scroll;
51 SVECTOR out;
52 SVECTOR in[4];
53 SpritePart *parts;
54 SpritePart *part;
55 u8 *ids;
56 s16 *p;
57 PrimPtr prim;
58 s32 transform;
59 s32 count;
60 s32 frameRow;
61 s32 semi;
62 s32 abr;
63 s32 i;
64 s32 k;
65 u8 u;
66 u8 v;
67
68 transform = 0;
69 /* the sheet starts with byte offsets: to its parts, its frame ids, then
70 each frame's list of parts */
71 ids = (u8 *)sheet + sheet[1];
72 parts = (SpritePart *)((u8 *)sheet + sheet[0]);
73 for (i = 0; ids[i] != frame; i++) {
74 }
75 p = (s16 *)((u8 *)sheet + sheet[i + 2]);
76 /* rot.vx and rot.vy are tested as one word */
77 if (SPRITE_DRAWER->scale.vx != ONE || SPRITE_DRAWER->scale.vy != ONE || SPRITE_DRAWER->scale.vz != ONE ||
78 *(s32 *)&SPRITE_DRAWER->rot.vx != 0 || SPRITE_DRAWER->rot.vz != 0) {
79 transform = 1;
81 RotMatrixYXZ_gte(&SPRITE_DRAWER->rot, &SPRITE_DRAWER->matrix);
82 ScaleMatrix(&SPRITE_DRAWER->matrix, &SPRITE_DRAWER->scale);
83 }
84 }
85 count = *p++;
86 frameRow = *p++;
87 abr = *p++;
88 if (abr == -1) {
89 semi = 0;
90 abr = 0;
91 } else {
92 semi = 1;
93 }
96 } else {
97 scroll.x = 0;
98 scroll.y = 0;
99 }
100 p += (count - 1) * 3;
101 prim.any = GFX.funcs.getPrim();
102 if (!transform) {
103 u16 clut;
104 u16 tpage;
105 u16 prevTpage;
106
107 tpage = 0;
108 prevTpage = 0;
109 for (i = 0; i < count; i++) {
110 part = &parts[p[0]];
111 clut = getClut((part->mode ? SPRITE_DRAWER->altClutX : SPRITE_DRAWER->clutX) + part->clutX,
112 (part->mode ? SPRITE_DRAWER->altClutY : SPRITE_DRAWER->clutY) + part->clutY + frameRow +
114 tpage = getTPage(part->mode, abr, SPRITE_DRAWER->tpageX + (part->mode ? part->u / 2 : part->u / 4),
116 if (i == 0) {
117 prevTpage = tpage;
118 }
119 if (prevTpage != tpage) {
120 SetDrawTPage(prim.tpage, 0, 1, prevTpage);
121 addPrim(SPRITE_DRAWER->ot, prim.any);
122 prim.tpage++;
123 prevTpage = tpage;
124 }
125 /* the colour and the code byte as one word: setSprt sets the code after */
126 *(CVECTOR *)&prim.sprt->r0 = SPRITE_DRAWER->color;
127 setSprt(prim.sprt);
128 if (semi) {
129 setSemiTrans(prim.sprt, 1);
130 }
131 prim.sprt->x0 = p[1] + x - scroll.x;
132 prim.sprt->y0 = p[2] + y - scroll.y;
133 if (part->mode) {
134 prim.sprt->u0 = part->u & 0x7F;
135 } else {
136 prim.sprt->u0 = part->u;
137 }
138 prim.sprt->v0 = part->v;
139 prim.sprt->w = part->w;
140 prim.sprt->h = part->h;
141 prim.sprt->clut = clut;
142 addPrim(SPRITE_DRAWER->ot, prim.any);
143 prim.sprt++;
144 p -= 3;
145 }
146 SetDrawTPage(prim.tpage, 0, 1, tpage);
147 addPrim(SPRITE_DRAWER->ot, prim.any);
148 prim.tpage++;
149 } else {
150 u16 clut;
151 u16 tpage;
152
153 for (i = 0; i < count; i++) {
154 part = &parts[p[0]];
155 clut = getClut((part->mode ? SPRITE_DRAWER->altClutX : SPRITE_DRAWER->clutX) + part->clutX,
156 (part->mode ? SPRITE_DRAWER->altClutY : SPRITE_DRAWER->clutY) + part->clutY + frameRow +
158 tpage = getTPage(part->mode, abr, SPRITE_DRAWER->tpageX + (part->mode ? part->u / 2 : part->u / 4),
160 /* as one word, as above */
161 *(CVECTOR *)&prim.ft4->r0 = SPRITE_DRAWER->color;
162 setPolyFT4(prim.ft4);
163 if (semi) {
164 setSemiTrans(prim.ft4, 1);
165 }
166 in[0].vx = in[2].vx = p[1] + x - SPRITE_DRAWER->pivotX;
167 in[1].vx = in[3].vx = in[0].vx + part->w;
168 in[0].vy = in[1].vy = p[2] + y - SPRITE_DRAWER->pivotY;
169 in[2].vy = in[3].vy = in[0].vy + part->h;
170 in[0].vz = in[1].vz = in[2].vz = in[3].vz = 0;
171 for (k = 0; k < 4; k++) {
172 ApplyMatrixSV(&SPRITE_DRAWER->matrix, &in[k], &out);
173 (&prim.ft4->x0)[k * 4] = out.vx - scroll.x + SPRITE_DRAWER->pivotX;
174 (&prim.ft4->y0)[k * 4] = out.vy - scroll.y + SPRITE_DRAWER->pivotY;
175 }
176 if (part->mode) {
177 u = part->u & 0x7F;
178 } else {
179 u = part->u;
180 }
181 prim.ft4->u0 = prim.ft4->u2 = u;
182 prim.ft4->u1 = prim.ft4->u3 = u + part->w - 1;
183 v = part->v;
184 prim.ft4->v0 = prim.ft4->v1 = v;
185 prim.ft4->v2 = prim.ft4->v3 = v + part->h - 1;
186 prim.ft4->tpage = tpage;
187 prim.ft4->clut = clut;
188 addPrim(SPRITE_DRAWER->ot, prim.any);
189 prim.ft4++;
190 p -= 3;
191 }
192 }
193 GFX.funcs.setPrim(prim.any);
194}
195
196/* Sprite drawer method: scales the sprites (ONE: as they are) */
198 SPRITE_DRAWER->scale.vx = x;
199 SPRITE_DRAWER->scale.vy = y;
200 SPRITE_DRAWER->scale.vz = z;
202}
203
204/*
205 * Sprite drawer method: rotates the sprites. An old-style definition, as
206 * SpriteDrawer.setRotation's unprototyped callers suit: they pass ints,
207 * which it takes as shorts.
208 */
210s16 x;
211s16 y;
212s16 z;
213{
214 SPRITE_DRAWER->rot.vx = x;
215 SPRITE_DRAWER->rot.vy = y;
216 SPRITE_DRAWER->rot.vz = z;
218}
219
220/* Sprite drawer method: the point the sprites scale and rotate around */
225
226/* Sprite drawer method: whether the sprites move with the layer's scroll */
230
231/* Sprite drawer method: the color the sprites are tinted with */
232void spriteDrawerSetColor(CVECTOR *color) {
233 SPRITE_DRAWER->color = *color;
234}
235
236/*
237 * Clears a sprite drawer to neutral (no scale, gray tint, follows the scroll), gives it its
238 * methods and binds it
239 */
241 HEAP.zero(obj, sizeof(SpriteDrawer));
242 obj->scale.vx = ONE;
243 obj->scale.vy = ONE;
244 obj->scale.vz = ONE;
245 obj->followScroll = 1;
246 obj->color.b = 0x80;
247 obj->color.g = 0x80;
248 obj->color.r = 0x80;
250 obj->draw = spriteDrawerDraw;
254 obj->bind = bindSpriteDrawer;
261 bindSpriteDrawer(obj);
262}
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
Heap HEAP
The functions that return what a call left in v0 (allocMemHigh, findTask) and the ones that take othe...
Definition heap.c:184
void spriteDrawerSetFollowScroll(s32 on)
Sprite drawer method: whether the sprites move with the layer's scroll.
void spriteDrawerSetRotation(s16 x, s16 y, s16 z)
Sprite drawer method: rotates the sprites.
void spriteDrawerSetClutRow(s32 row)
Sprite drawer method: the CLUT row added to every part's.
void spriteDrawerSetColor(CVECTOR *color)
Sprite drawer method: the color the sprites are tinted with.
void spriteDrawerSetTexture(s32 x, s32 y)
Sprite drawer method: the VRAM position of the sheet's texture and CLUTs.
void spriteDrawerSetScale(s32 x, s32 y, s32 z)
Sprite drawer method: scales the sprites (ONE: as they are)
void spriteDrawerSetLayer(Layer *layer, s32 depth)
Sprite drawer method: the layer and depth to draw to.
void bindSpriteDrawer(SpriteDrawer *obj)
Makes obj the sprite drawer the methods work on.
void spriteDrawerSetLayerId(s32 id, s32 depth)
Sprite drawer method: the layer (by id) and depth to draw to.
void initSpriteDrawer(SpriteDrawer *obj)
Clears a sprite drawer to neutral (no scale, gray tint, follows the scroll), gives it its methods and...
static SpriteDrawer * SPRITE_DRAWER
Small variables, addressed through $gp (see the Makefile)
void spriteDrawerSetPivot(s32 x, s32 y)
Sprite drawer method: the point the sprites scale and rotate around.
void spriteDrawerSetAltClut(s32 x, s32 y)
Sprite drawer method: the other CLUT area, for parts that ask for it.
void spriteDrawerDraw(s32 *sheet, s32 frame, s32 x, s32 y)
Draws frame frame of a sprite sheet (see SpritePart) at (x, y): as sprites, changing the texture page...
void *(* getPrim)(void)
Definition gfx.h:19
struct Layer *(* getLayer)(s32 id)
Definition gfx.h:28
void(* setPrim)(void *next)
Definition gfx.h:20
GfxFuncs funcs
Definition gfx.h:77
void(* zero)(void *ptr, s32 size)
Definition heap.h:32
A drawing layer (GFX.funcs.createLayer): a DRAWENV with a pair of ordering tables,...
Definition gfx.h:128
void(* getScroll)()
Definition gfx.h:148
u_long *(* getOtEntry)(struct Layer *, s32 depth)
Definition gfx.h:155
Draws sprites from a sprite sheet (such as file 0x277, the menu graphics): a sheet has a table of fra...
Definition gfx.h:223
s32 clutRow
added to the CLUT y: palette animations
Definition gfx.h:232
s32 tpageY
Definition gfx.h:227
s32 transformDirty
Definition gfx.h:235
MATRIX matrix
Definition gfx.h:240
void(* setAltClut)()
(x, y)
Definition gfx.h:243
void(* setLayer)()
(layer, depth)
Definition gfx.h:245
s32 altClutX
for the parts that are flagged
Definition gfx.h:230
void(* setPivot)()
(x, y)
Definition gfx.h:250
s32 followScroll
offset by the layer's scroll
Definition gfx.h:233
s32 clutY
Definition gfx.h:229
void(* setScale)()
(x, y, z), ONE = 1.0
Definition gfx.h:248
s32 pivotX
Definition gfx.h:236
void(* setColor)()
(CVECTOR *)
Definition gfx.h:252
void(* bind)()
(drawer)
Definition gfx.h:241
s32 altClutY
Definition gfx.h:231
s32 pivotY
Definition gfx.h:237
SVECTOR rot
Definition gfx.h:239
void(* setLayerId)()
(id, depth)
Definition gfx.h:244
Layer * layer
Definition gfx.h:224
CVECTOR color
Definition gfx.h:234
u_long * ot
Definition gfx.h:225
s32 tpageX
VRAM position of the sheet's texture.
Definition gfx.h:226
void(* setFollowScroll)()
(on)
Definition gfx.h:251
void(* setClutRow)()
(row)
Definition gfx.h:247
VECTOR scale
ScaleMatrix's.
Definition gfx.h:238
void(* draw)()
(sheet, frame, x, y)
Definition gfx.h:246
void(* setRotation)()
(x, y, z); no prototype: callers would truncate s16 args
Definition gfx.h:249
s32 clutX
Definition gfx.h:228
void(* setTexture)()
(x, y)
Definition gfx.h:242
One part of a sprite sheet, a rectangle of its texture.
Definition gfx.h:285
s16 h
Definition gfx.h:289
s16 v
Definition gfx.h:287
s16 u
Definition gfx.h:286
s16 clutX
added to the drawer's CLUT position
Definition gfx.h:290
s16 w
Definition gfx.h:288
s16 clutY
Definition gfx.h:291
s16 mode
0: 4-bit texture, 1: 8-bit, with the drawer's other CLUT
Definition gfx.h:292
Definition gfx.h:352
s32 y
Definition gfx.h:354
s32 x
Definition gfx.h:353
The next free primitive of the packet buffer (getPrim), written as the kind that is drawn next.
Definition gfx.h:344
POLY_FT4 * ft4
Definition gfx.h:348
DR_TPAGE * tpage
Definition gfx.h:349
void * any
Definition gfx.h:345
SPRT * sprt
Definition gfx.h:346