persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
IntroView.cpp
Go to the documentation of this file.
1#include "IntroView.hpp"
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4#include "data/audioDb.hpp"
6
7#include <maxmod9.h>
8#include <nds.h>
9#include <string>
10
12{
13 if (intro == nullptr)
14 {
15 intro = engine.createEntity();
16 graphics = engine.createComponent<GraphicsComponent>();
17 text = engine.createComponent<TextComponent>();
18 musicCmpt = engine.createComponent<MusicComponent>();
19 sfxCmpt = engine.createComponent<SFXComponent>();
20
21 intro->addComponent(graphics);
22 intro->addComponent(text);
23 intro->addComponent(musicCmpt);
24 intro->addComponent(sfxCmpt);
25 }
26
27 // set video mode for 3 text layers and 1 extended rotation layer
28 videoSetMode(MODE_3_2D);
29 videoSetModeSub(MODE_3_2D);
30
31 // map vram bank A and D to main engine background (slot 0)
32 vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
33 vramSetBankD(VRAM_D_MAIN_BG_0x06020000);
34 // map vram bank B to sub engine background
35 vramSetBankC(VRAM_C_SUB_BG);
36 // map vram bank B to main engine sprites (slot 0)
37 vramSetBankB(VRAM_B_MAIN_SPRITE);
38
39 // enable extended palettes
40 bgExtPaletteEnable();
41 bgExtPaletteEnableSub();
42
43 // set brightness on both screen to completely dark (no visible image)
44 setBrightness(3, -16);
45
46 // initialize backgrounds
47 // check https://mtheall.com/vram.html to ensure bg fit in vram
48 bg[0] = bgInit(0, BgType_Text8bpp, BgSize_T_512x512, 11, 2); // silhouette
49 bg[1] = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 9, 0); // room
50 bg[2] = bgInit(2, BgType_Text8bpp, BgSize_T_256x256, 10, 3); // sky
51 bg[3] = bgInit(3, BgType_ExRotation, BgSize_ER_512x512, 19, 8); // overlay
52 bgSubLogo = bgInitSub(0, BgType_Text8bpp, BgSize_T_256x256, 0, 1); // logo
53 bgSubSky = bgInitSub(1, BgType_Text8bpp, BgSize_T_256x256, 1, 2); // sky (sub screen)
54 bgSubText = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 5, 0); // text (sub screen)
55 textVideoBufferSub = (uint16_t*)bgGetGfxPtr(bgSubText);
56
57 // need to set priority to properly display
58 // 0 is highest, 3 is lowest
59 bgSetPriority(bg[0], 1); // silhouette
60 bgSetPriority(bg[1], 1); // room
61 bgSetPriority(bg[2], 3); // sky
62 bgSetPriority(bg[3], 2); // overlay
63 // adjust sub screen image and console to sit correctly on each other
64 bgSetPriority(bgSubLogo, 1);
65 bgSetPriority(bgSubSky, 2);
66 bgSetPriority(bgSubText, 0);
67
68 // reset background vram
69 // 512x512 backgrounds use 8192 bytes of map memory
70 // calculated using (512 / 8) * (512 / 8) * 2
71 // the DS divides pixels into 8x8 tiles (hence we divide by 8) and we use 16-bit colour (which is 2 bytes)
72 dmaFillHalfWords(0, bgGetMapPtr(bg[0]), 8192); // silhouette
73 dmaFillHalfWords(0, bgGetMapPtr(bg[3]), 8192); // overlay
74 // 256x256 backgrounds use 2048 bytes of map memory
75 dmaFillHalfWords(0, bgGetMapPtr(bg[1]), 2048); // room
76 dmaFillHalfWords(0, bgGetMapPtr(bg[2]), 2048); // sky
77 dmaFillHalfWords(0, bgGetMapPtr(bgSubLogo), 2048);
78 dmaFillHalfWords(0, bgGetMapPtr(bgSubSky), 2048);
79
80 std::string bgPath = "graphics/IntroView/backgrounds/";
81 std::string spritePath = "graphics/IntroView/sprites/";
82
83 GraphicAsset silhouette = graphics->loadGraphic(bgPath + "silhouetteBackground/silhouetteBackground");
84 GraphicAsset room = graphics->loadGraphic(bgPath + "roomBackground/roomBackground");
85 GraphicAsset sky = graphics->loadGraphic(bgPath + "skyBackground/skyBackground");
86 GraphicAsset overlay = graphics->loadGraphic(bgPath + "overlayBackground/overlayBackground");
87 GraphicAsset skySub = graphics->loadGraphic(bgPath + "skyBackgroundSub/skyBackgroundSub");
88
89 GraphicAsset attribution = graphics->loadGraphic(bgPath + "attributionBackground/attributionBackground");
90 GraphicAsset logoLeft = graphics->loadGraphic(spritePath + "logoSpriteLeft/logoSpriteLeft");
91 GraphicAsset logoRight = graphics->loadGraphic(spritePath + "logoSpriteRight/logoSpriteRight");
92
93 // copy graphics to vram
94 dmaCopy(silhouette.tiles, bgGetGfxPtr(bg[0]), silhouette.tilesLen);
95 dmaCopy(room.tiles, bgGetGfxPtr(bg[1]), room.tilesLen);
96 dmaCopy(sky.tiles, bgGetGfxPtr(bg[2]), sky.tilesLen);
97 dmaCopy(overlay.tiles, bgGetGfxPtr(bg[3]), overlay.tilesLen);
98 dmaCopy(attribution.tiles, bgGetGfxPtr(bgSubLogo), attribution.tilesLen);
99 dmaCopy(skySub.tiles, bgGetGfxPtr(bgSubSky), skySub.tilesLen);
100
101 // copy maps to vram
102 dmaCopy(silhouette.map, bgGetMapPtr(bg[0]), silhouette.mapLen);
103 dmaCopy(room.map, bgGetMapPtr(bg[1]), room.mapLen);
104 dmaCopy(sky.map, bgGetMapPtr(bg[2]), sky.mapLen);
105 dmaCopy(overlay.map, bgGetMapPtr(bg[3]), overlay.mapLen);
106 dmaCopy(attribution.map, bgGetMapPtr(bgSubLogo), attribution.mapLen);
107 dmaCopy(skySub.map, bgGetMapPtr(bgSubSky), skySub.mapLen);
108
109 // can only write to extended palettes in LCD mode
110 vramSetBankE(VRAM_E_LCD); // for main engine
111 vramSetBankH(VRAM_H_LCD); // for subv engine
112
113 // copy palettes to extended palette area
114 dmaCopy(silhouette.pal, &VRAM_E_EXT_PALETTE[0][0], silhouette.palLen);
115 dmaCopy(room.pal, &VRAM_E_EXT_PALETTE[1][0], room.palLen);
116 dmaCopy(sky.pal, &VRAM_E_EXT_PALETTE[2][0], sky.palLen);
117 dmaCopy(overlay.pal, &VRAM_E_EXT_PALETTE[3][0], overlay.palLen);
118 dmaCopy(attribution.pal, &VRAM_H_EXT_PALETTE[0][0], attribution.palLen);
119 dmaCopy(skySub.pal, &VRAM_H_EXT_PALETTE[1][0], skySub.palLen);
120
121 // configure text component
123
124 // map vram to extended palette
125 vramSetBankE(VRAM_E_BG_EXT_PALETTE);
126 vramSetBankH(VRAM_H_SUB_BG_EXT_PALETTE);
127
128 ui.hideBg(bg[3]); // hide overlay
129 bgSetCenter(bg[3], 128, 96); // pivot point on the screen (at the screen's center)
130 bgSetScroll(bg[3], 256, 256); // pivot point on the image (at the image's center)
131
132 // showing logo as sprite
133 logoSprite[0] = {SpriteSize_64x64, SpriteColorFormat_256Color, 15};
134 logoSprite[1] = {SpriteSize_64x64, SpriteColorFormat_256Color, 15};
135
136 // initialize sub sprite engine with 1D mapping, 128 byte boundry, no external palette support
137 oamInit(&oamMain, SpriteMapping_1D_128, false);
138
139 // allocating space for sprite graphics
140 logoSprite[0].gfx = oamAllocateGfx(&oamMain, SpriteSize_64x64, SpriteColorFormat_256Color);
141 logoSprite[1].gfx = oamAllocateGfx(&oamMain, SpriteSize_64x64, SpriteColorFormat_256Color);
142
143 dmaCopy(logoLeft.tiles, logoSprite[0].gfx, logoLeft.tilesLen);
144 dmaCopy(logoRight.tiles, logoSprite[1].gfx, logoRight.tilesLen);
145
146 // NOTE: left and right will use the same palette. Just ensure that the order of colours when indexed
147 // is THE SAME for both images!
148 dmaCopy(logoRight.pal, SPRITE_PALETTE, logoRight.palLen);
149
150 // for slide in animation
151 // move camera to the empty right half of the 512px wide background
152 bgSetScroll(bg[0], -silhouetteX, -silhouetteY);
153 bgUpdate();
154
155 // unload all graphics now that it's copied to vram
156 graphics->unloadGraphic(silhouette);
157 graphics->unloadGraphic(room);
158 graphics->unloadGraphic(sky);
159 graphics->unloadGraphic(overlay);
160 graphics->unloadGraphic(attribution);
161 graphics->unloadGraphic(skySub);
162 graphics->unloadGraphic(logoLeft);
163 graphics->unloadGraphic(logoRight);
164
165 // point to music
166 sfxCmpt->registerSFX(SFX::SFX_0);
167 sfxCmpt->registerSFX(SFX::SFX_2);
169
170 // hide sub screen text and attribution text layer
171 REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG3 | BLEND_SRC_BG0 | BLEND_DST_BG0 | BLEND_DST_BG1 | BLEND_DST_BACKDROP;
172 REG_BLDALPHA_SUB = 0 | (16 << 8);
173
174 // hide main skyBackground
175 // blend control. takes effect mode / source / destination
176 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG2 | BLEND_DST_BACKDROP;
177
178 setBrightness(1, -16);
180 fadeTimer.start(ae::q20_12_t{1.1});
181
182 logMessage(LogSeverity::DEBUG, "IntroView initialized");
183}
184
186{
187 switch (transitionPhase)
188 {
190 {
191 if (!skyFadeIn)
192 {
193 if (fadeTimer.isFinished())
194 {
195 setBrightness(1, 0);
196 skyFadeIn = true;
197 fadeTimer.start(ae::q20_12_t{2});
198 }
199 else
200 {
201 int brightness = -16 + (fadeTimer.getProgress().raw_value() >> 8);
202 setBrightness(1, brightness);
203 }
204 }
205 else
206 {
207 if (fadeTimer.isFinished())
208 {
210 setBrightness(2, 0);
211 }
212 else
213 {
214 int fadeVal = fadeTimer.getProgress().raw_value() >> 8;
215
216 REG_BLDALPHA = fadeVal | ((16 - fadeVal) << 8);
217 setBrightness(2, -16 + fadeVal);
218 }
219 }
220
221 break;
222 }
223
225 {
226 bool transitionTriggered = false;
227
228 // transition to menu state on any input
229 if ((systemKeysDown & KEY_A) || (systemKeysDown & KEY_START) || (systemKeysDown & KEY_TOUCH))
230 {
231 sfxCmpt->playSFX(SFX::SFX_2, 255, 128);
233 transitionTriggered = true;
234 }
235 else if (systemKeysDown & KEY_B)
236 {
237 sfxCmpt->playSFX(SFX::SFX_0, 255, 128);
239 transitionTriggered = true;
240 }
241
242 if (transitionTriggered)
243 {
244 musicCmpt->pauseMusic();
246 fadeTimer.start(ae::q20_12_t{0.85});
247 break;
248 }
249
250 // scroll silhouette background
251 // animate X (moving right towards 0)
252 if (silhouetteX < 0 && frame % 5 == 0)
253 {
254 silhouetteX += (-silhouetteX) / 6 + 1;
255 if (silhouetteX > 0)
256 {
257 silhouetteX = 0;
258 }
259 }
260
261 // animate Y (moving up towards 0)
262 if (silhouetteY > 0 && frame % 5 == 0)
263 {
264 silhouetteY += (-silhouetteY) / 6 + 1;
265 if (silhouetteY < 0)
266 {
267 silhouetteY = 0;
268 }
269 }
270
271 bgSetScroll(bg[0], -silhouetteX, -silhouetteY);
272
273 // perform code after silhouette slide-in
274 if (silhouetteX < 0 || silhouetteY < 0)
275 {
277 }
278
279 if (animateText)
280 {
282
284 {
285 durationCounter = 0;
287
288 if (textAlpha >= 16)
289 {
290 textAlpha = 16;
291 textAlphaDirection = -1; // Start fading out
292 }
293 else if (textAlpha <= 0)
294 {
295 textAlpha = 0;
296 textAlphaDirection = 1; // Start fading in
297 }
298
299 REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG3 | BLEND_DST_BG0 | BLEND_DST_BG1 | BLEND_DST_BACKDROP;
300 REG_BLDALPHA_SUB = textAlpha | ((16 - textAlpha) << 8);
301 }
302 }
303
304 // setup logoSprite
305 if (!displayLogo)
306 {
307 displayLogo = true;
308 int spriteId = 0;
310 {
311 oamSet(&oamMain,
312 spriteId++,
313 srs.x,
314 srs.y,
315 srs.priority,
316 srs.sprite.paletteAlpha,
317 srs.sprite.size,
318 srs.sprite.format,
319 srs.sprite.gfx,
320 srs.affineIndex,
321 srs.sizeDouble,
322 srs.hide,
323 srs.hflip,
324 srs.vflip,
325 srs.mosaic);
326
327 oamMain.oamMemory[spriteId].attribute[0] |= ATTR0_TYPE_BLENDED;
328 }
329
330 // setup fade for main screen sprites
331 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_SPRITE | BLEND_DST_BG0 | BLEND_DST_BG1 | BLEND_DST_BG2;
332 REG_BLDALPHA = 0 | (16 << 8);
333
334 // setup fade for sub screen attribution text layer
335 REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG0 | BLEND_DST_BG1 | BLEND_DST_BACKDROP;
336 REG_BLDALPHA_SUB = 0 | (16 << 8);
337 }
338
339 // fade in attribution text layer and logoSprite
340 if (logoOpacity < 16 && frame % 4 == 0)
341 {
342 logoOpacity++;
343 REG_BLDALPHA = logoOpacity | ((16 - logoOpacity) << 8);
344 REG_BLDALPHA_SUB = logoOpacity | ((16 - logoOpacity) << 8);
345 }
346
347 // code after sprite fade in
348 if (logoOpacity < 16)
349 {
350 oamMain.oamMemory[0].attribute[0] &= ~ATTR0_TYPE_BLENDED; // disable sprite blending
351 oamMain.oamMemory[1].attribute[0] &= ~ATTR0_TYPE_BLENDED; // disable sprite blending
353 }
354
355 // setup animated text
356 if (logoOpacity >= 16 && !animateText)
357 {
358 animateText = true;
359 REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG3 | BLEND_DST_BG0 | BLEND_DST_BG1 | BLEND_DST_BACKDROP;
360 REG_BLDALPHA_SUB = textAlpha | ((16 - textAlpha) << 8);
361 text->drawText("\xFF\x02\x01Press Any Button", 35, 82, TextColor::White);
362 }
363
364 // setup blending for overlay
365 if (!displayOverlay)
366 {
367 displayOverlay = true;
368 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG3 | BLEND_DST_BG2;
369 REG_BLDALPHA = 0 | (16 << 8);
370 ui.showBg(bg[3]);
371 }
372
373 // fade in overlay
374 if (overlayOpacity < 6 && frame % 4 == 0)
375 {
377 REG_BLDALPHA = overlayOpacity | ((16 - overlayOpacity) << 8);
378 }
379
380 // rotate overlay
381 if (frame % 4 == 0)
382 {
383 waveAngle += 50;
384 int angle = MathManager::getInstance().sin(static_cast<ae::angle16_t>(waveAngle)).raw_value();
385 int rotationSpeed = baseSpeed + ((angle * fluctuation) >> 12);
386 currentRotation += rotationSpeed;
387 bgSetRotateScale(bg[3], currentRotation, 256, 256);
388 }
389
390 // default state
392 }
394 {
395 if (fadeTimer.isFinished())
396 {
397 setBrightness(3, -16);
398 return nextViewState;
399 }
400 else
401 {
402 ae::q20_12_t progress = fadeTimer.getProgress();
403 int fadeVal = progress.raw_value() >> 8; // 0-16
404 setBrightness(3, -fadeVal);
405 }
406 break;
407 }
408 }
409
411}
412
414{
415 if (intro != nullptr)
416 {
417 engine.destroyEntity(intro);
418
419 intro = nullptr;
420 graphics = nullptr;
421 text = nullptr;
422 musicCmpt = nullptr;
423 sfxCmpt = nullptr;
424 }
425
426 // clear all sprites from oam
427 oamClear(&oamMain, 0, 0);
428
429 // free allocated sprite vram
430 for (int i = 0; i < 2; ++i)
431 {
432 if (logoSprite[i].gfx != nullptr)
433 {
434 oamFreeGfx(&oamMain, logoSprite[i].gfx);
435 logoSprite[i].gfx = nullptr;
436 }
437 }
438
440}
@ SFX_2
@ SFX_0
ViewState
Definition StateTypes.hpp:4
@ White
Definition TextTypes.hpp:14
Manages the mapping of UIScreens to these hardware layers.
virtual void cleanup()
Definition BaseView.cpp:8
ViewState nextViewState
Definition IntroView.hpp:19
int baseSpeed
Definition IntroView.hpp:57
int durationCounter
Definition IntroView.hpp:43
SFXComponent * sfxCmpt
Definition IntroView.hpp:71
bool displayLogo
Definition IntroView.hpp:48
MusicComponent * musicCmpt
Definition IntroView.hpp:70
u16 waveAngle
Definition IntroView.hpp:55
void init() override
Definition IntroView.cpp:11
Sprite logoSprite[2]
Definition IntroView.hpp:22
ViewState update() override
TextComponent * text
Definition IntroView.hpp:66
bool skyFadeIn
Definition IntroView.hpp:20
void cleanup() override
bool animateText
Definition IntroView.hpp:41
int silhouetteY
Definition IntroView.hpp:38
int FONT_SIZE
Definition IntroView.hpp:62
etl::array< SpriteRenderState, 2 > spriteRenderStates
Definition IntroView.hpp:26
int logoOpacity
Definition IntroView.hpp:49
GraphicsComponent * graphics
Definition IntroView.hpp:65
int bgSubLogo
Definition IntroView.hpp:31
std::string FONT_NAME
Definition IntroView.hpp:61
int bg[4]
Definition IntroView.hpp:28
int overlayOpacity
Definition IntroView.hpp:53
int textAlphaDirection
Definition IntroView.hpp:45
Timer fadeTimer
Definition IntroView.hpp:17
int fluctuation
Definition IntroView.hpp:58
ae::Entity * intro
Definition IntroView.hpp:64
UIManager & ui
Definition IntroView.hpp:69
int textAlpha
Definition IntroView.hpp:44
TransitionPhase transitionPhase
Definition IntroView.hpp:18
u16 currentRotation
Definition IntroView.hpp:56
bool displayOverlay
Definition IntroView.hpp:52
int silhouetteX
Definition IntroView.hpp:37
int bgSubText
Definition IntroView.hpp:33
uint16_t * textVideoBufferSub
Definition IntroView.hpp:34
Holds global variables used game-wide.
volatile u32 systemKeysDown
Definition main.cpp:36
GameEngine engine
Definition main.cpp:31
volatile int32_t frame
Definition main.cpp:35
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 render a Sprite.
A struct that holds initial config values for the TextComponent.