persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
DialogueScreen.cpp
Go to the documentation of this file.
1#include "DialogueScreen.hpp"
2#include "core/globals.hpp"
3#include "core/logger.hpp"
6
8
10{
11 if (instance == nullptr)
12 {
14 }
15}
16
18{
19 if (instance != nullptr)
20 {
21 delete instance;
22 }
23 instance = nullptr;
24}
25
27{
28 if (instance == nullptr)
29 {
30 create();
31 }
32 return instance;
33}
34
36{
37 // load palettes
38 dmaCopy(palette0, SPRITE_PALETTE_SUB, 16 * sizeof(u16));
39
40 // perform transformations
42 int i = 1;
44 {
45 oamRotateScale(oam, i++, st.angle, st.sx, st.sy);
46 }
47
48 // draw sprites
49 spriteId = 0;
51 {
52 // use alias for easy referencing
53 Sprite& sprite = sp.srs.sprite;
54 GraphicAsset& graphic = sp.ga;
55 SpriteRenderState& srs = sp.srs;
56
57 // copy sprite into vram
58 dmaCopy(graphic.tiles, sprite.gfx, graphic.tilesLen);
59
60 oamSet(oam,
61 spriteId++,
62 srs.x,
63 srs.y + panelOffset,
64 srs.priority,
66 srs.sprite.size,
67 srs.sprite.format,
68 srs.sprite.gfx,
69 srs.affineIndex,
70 srs.sizeDouble,
71 srs.hide,
72 srs.hflip,
73 srs.vflip,
74 srs.mosaic);
75 }
76}
77
79{
80 panelAnimator.cancelAll();
81 panelOffset = 40;
82 panelAnimation = panelAnimator.animate(panelOffset).from(40).to(0).duration(220).ease(UIEase::OutCubic).start();
83}
84
86{
87 panelAnimator.cancelAll();
89 panelAnimator.animate(panelOffset).from(panelOffset).to(40).duration(180).ease(UIEase::InCubic).start();
90}
91
93{
94 bool panelWasAnimating = panelAnimation.isPlaying();
95 bool bustWasAnimating = bustAnimation.isPlaying();
96 const ae::q20_12_t dt = MathManager::getInstance().div(ae::q20_12_t{1}, ae::q20_12_t{60});
97 panelAnimator.update(dt);
98 bustAnimator.update(dt);
99
100 if (isLoaded && (panelWasAnimating || bustWasAnimating || panelAnimation.isPlaying() || bustAnimation.isPlaying()))
101 {
104 }
105}
106
108{
109 return panelAnimation.isPlaying();
110}
111
113{
114 // create relevant entities, components
115 if (dialogue == nullptr)
116 {
117 dialogue = engine.createEntity();
118 graphics = engine.createComponent<GraphicsComponent>();
119 dialogue->addComponent(graphics);
120 }
121
122 // load sprites
123 for (SpritePayload& sp : spritePayloads)
124 {
125 // use alias for easy referencing
126 Sprite& sprite = sp.srs.sprite;
127 GraphicAsset& graphic = sp.ga;
128
129 // load graphic if not already loaded
130 if (graphic.id <= -1)
131 {
132 // allocating space for sprite
133 sprite.gfx = oamAllocateGfx(oam, sprite.size, sprite.format);
134
135 // load sprite into ram
136 graphic = graphics->loadSpriteGraphic(sp.spritePath, sp.spriteType, sp.spriteVariant);
137 }
138 }
139
140 // set palettes
143};
144
145void DialogueScreen::loadBusts(etl::array<etl::span<SpritePayload>, 10>* bustPayloads)
146{
147 if (bustPayloads == nullptr)
148 {
149 logMessage(LogSeverity::WARNING, "Called with null bustPayloads");
150 return;
151 }
152
153 for (etl::span<SpritePayload>& sp : *bustPayloads)
154 {
155 loadBust(sp);
156 }
157 this->bustPayloads = bustPayloads;
158 prevBust = nullptr;
159}
160
161void DialogueScreen::loadBust(etl::span<SpritePayload>& bustPayload)
162{
163 // load new bust
164 for (SpritePayload& sp : bustPayload)
165 {
166 // use alias for easy referencing
167 Sprite& sprite = sp.srs.sprite;
168 GraphicAsset& graphic = sp.ga;
169
170 // setup sprite
172
173 // load graphic if not already loaded
174 if (graphic.id <= -1)
175 {
176 // allocating space for sprite
177 sprite.gfx = oamAllocateGfx(oam, sprite.size, sprite.format);
178
179 // load sprite into ram
180 graphic = graphics->loadSpriteGraphic(sp.spritePath, sp.spriteType, sp.spriteVariant);
181 }
182 }
183}
184
185void DialogueScreen::renderBust(etl::span<SpritePayload>& bustPayload)
186{
187 // exit if no bust
188 if (bustPayload.empty())
189 {
190 return;
191 }
192 // don't re-render the bust
193 else if (prevBust != nullptr && (prevBust->data() == bustPayload.data()))
194 {
195 return;
196 }
197 // hide the old bust sprites
198 else if (prevBust != nullptr)
199 {
200 // hide sprites
201 oamClear(oam, spriteId, prevBust->size());
202
203 // clear palette
204 dmaFillHalfWords(0, SPRITE_PALETTE_SUB + (bustPaletteId * 16), 16 * sizeof(u16));
205 }
206
207 // update to new bust
208 prevBust = &bustPayload;
209 activeBust = &bustPayload;
210 bustAnimator.cancelAll();
211 bustOffsetX = 0;
212 auto focus = bustAnimator.sequence();
213 focus.append(bustAnimator.animate(bustOffsetX).from(0).to(-3).duration(50).ease(UIEase::OutQuad));
214 focus.append(bustAnimator.animate(bustOffsetX).from(-3).to(3).duration(90).ease(UIEase::InOutQuad));
215 focus.append(bustAnimator.animate(bustOffsetX).from(3).to(0).duration(70).ease(UIEase::OutQuad));
216 bustAnimation = focus.start();
217
218 // draw bust
219 int bustId = spriteId;
220 void* bustPalette = nullptr;
221 for (SpritePayload& sp : bustPayload)
222 {
223 // use alias for easy referencing
224 Sprite& sprite = sp.srs.sprite;
225 GraphicAsset& graphic = sp.ga;
226 SpriteRenderState& srs = sp.srs;
227
228 // skip; graphics have not been loaded
229 if (sprite.gfx == nullptr || graphic.id <= -1)
230 {
231 continue;
232 }
233
234 // save the latest palette
235 bustPalette = graphic.pal;
236
237 // copy sprite into vram
238 dmaCopy(graphic.tiles, sprite.gfx, graphic.tilesLen);
239
240 oamSet(oam,
241 bustId++,
242 srs.x + bustOffsetX,
243 srs.y,
244 srs.priority,
246 srs.sprite.size,
247 srs.sprite.format,
248 srs.sprite.gfx,
249 srs.affineIndex,
250 srs.sizeDouble,
251 srs.hide,
252 srs.hflip,
253 srs.vflip,
254 srs.mosaic);
255 }
256
257 // setup bust palette
258 dmaCopy(bustPalette, SPRITE_PALETTE_SUB + (bustPaletteId * 16), 16 * sizeof(u16));
259}
260
262{
263 if (activeBust == nullptr)
264 {
265 return;
266 }
267
268 int bustId = spriteId;
269 for (SpritePayload& sp : *activeBust)
270 {
271 Sprite& sprite = sp.srs.sprite;
272 if (sprite.gfx == nullptr || sp.ga.id <= -1)
273 {
274 continue;
275 }
276 oamSetXY(oam, bustId++, sp.srs.x + bustOffsetX, sp.srs.y);
277 }
278}
279
280void DialogueScreen::unloadBust(etl::span<SpritePayload>& bustPayload)
281{
282 for (SpritePayload& sp : bustPayload)
283 {
284 // use alias for easy referencing
285 Sprite& sprite = sp.srs.sprite;
286 GraphicAsset& graphic = sp.ga;
287
288 // free sprite vram
289 if (sprite.gfx != nullptr)
290 {
291 oamFreeGfx(oam, sprite.gfx);
292 sprite.gfx = nullptr;
293 }
294
295 // unload graphic if not already unloaded
296 if (graphic.id > -1)
297 {
298 // unload sprite
299 graphics->unloadGraphic(graphic);
300
301 // reset data
302 sprite.paletteAlpha = -1;
303 graphic = {};
304 }
305 }
306}
307
309{
310 panelAnimator.cancelAll();
311 bustAnimator.cancelAll();
314 panelOffset = 40;
315 bustOffsetX = 0;
316 activeBust = nullptr;
317
318 // hide sprites
320
321 // free sprites
322 for (SpritePayload& sp : spritePayloads)
323 {
324 // use alias for easy referencing
325 Sprite& sprite = sp.srs.sprite;
326 GraphicAsset& graphic = sp.ga;
327
328 // free sprite vram
329 if (sprite.gfx != nullptr)
330 {
331 oamFreeGfx(oam, sprite.gfx);
332 sprite.gfx = nullptr;
333 }
334
335 // unload graphic if not already unloaded
336 if (graphic.id > -1)
337 {
338 // unload sprite
339 graphics->unloadGraphic(graphic);
340
341 // reset data
342 graphic = {};
343 }
344 }
345
346 // free bust sprites
347 if (bustPayloads != nullptr)
348 {
349 for (etl::span<SpritePayload>& sp : *bustPayloads)
350 {
351 unloadBust(sp);
352 }
353
354 // reset array pointers
355 bustPayloads = nullptr;
356 prevBust = nullptr;
357 }
358
359 if (dialogue != nullptr)
360 {
361 engine.destroyEntity(dialogue);
362
363 dialogue = nullptr;
364 graphics = nullptr;
365 }
366}
367
369{
370 switch (action)
371 {
372 // default palette
374 {
375 dmaCopy(palette0, SPRITE_PALETTE_SUB, 16 * sizeof(u16));
376 break;
377 }
378
379 // option selection palette
381 {
382 dmaCopy(palette1, SPRITE_PALETTE_SUB, 16 * sizeof(u16));
383 break;
384 }
385
386 default:
387 {
388 break;
389 }
390 }
391}
Easing mode alias for UI property animations.
UIAction
Definition UITypes.hpp:23
@ SwitchToPalette1
Definition UITypes.hpp:25
@ SwitchToPalette0
Definition UITypes.hpp:24
bool isAnimating() const
GraphicAsset blueBlockGraphic
void loadBusts(etl::array< etl::span< SpritePayload >, 10 > *bustPayloads)
void load()
Loads graphics data into memory & pushes to VRAM.
UIAnimationHandle panelAnimation
etl::span< SpritePayload > * prevBust
etl::span< SpritePayload > * activeBust
etl::array< etl::span< SpritePayload >, 10 > * bustPayloads
void renderSprites() override
Applys sprite colour palette & draws sprites to screen.
std::string spritePath
UIAnimator bustAnimator
UIAnimator panelAnimator
void unloadBust(etl::span< SpritePayload > &bustPayload)
static DialogueScreen * instance
UIAnimationHandle bustAnimation
GraphicsComponent * graphics
etl::array< SpritePayload, 34 > spritePayloads
void loadBust(etl::span< SpritePayload > &bustPayload)
etl::array< SpriteTransform, 2 > spriteTransforms
static void create()
void unload()
Unloads graphics data from memory.
static DialogueScreen * getInstance()
void renderBust(etl::span< SpritePayload > &bustPayload)
void triggerAction(UIAction action) override
Trigger an action to be handled.
ae::Entity * dialogue
static void destroy()
Non-owning control handle for a UIAnimation managed by UIAnimator.
void removeSprites()
Clears the displayed sprites from the screen.
Definition UIScreen.cpp:11
OamState * oam
Definition UIScreen.hpp:12
bool isLoaded
Definition UIScreen.hpp:13
Holds global variables used game-wide.
GameEngine engine
Definition main.cpp:31
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
Holds loaded graphics data.
Data to manage the lifecycle of a sprite.
Data to render a Sprite.
Data to perform a transformation on a Sprite.
A simple sprite structure.
SpriteColorFormat format
int paletteAlpha
SpriteSize size