persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
MainMenuView.cpp
Go to the documentation of this file.
1#include "MainMenuView.hpp"
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4#include "data/audioDb.hpp"
6
7#include <nds.h>
8#include <stdio.h>
9#include <string>
10
12{
13 if (mainMenu == nullptr)
14 {
15 mainMenu = engine.createEntity();
16 graphics = engine.createComponent<GraphicsComponent>();
17 textMenu = engine.createComponent<TextComponent>();
18 musicCmpt = engine.createComponent<MusicComponent>();
19 sfxCmpt = engine.createComponent<SFXComponent>();
20
21 mainMenu->addComponent(graphics);
22 mainMenu->addComponent(textMenu);
23 mainMenu->addComponent(musicCmpt);
24 mainMenu->addComponent(sfxCmpt);
25
26 UISystem::getInstance().setSFXComponent(sfxCmpt);
27 }
28
29 // set video mode for 2 text layers and 2 extended rotation layer
30 videoSetMode(MODE_5_2D);
31 // set sub video mode for 4 text layers
32 videoSetModeSub(MODE_3_2D | DISPLAY_BG3_ACTIVE);
33
34 // map vram bank A to main engine background (slot 0)
35 vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
36 vramSetBankD(VRAM_D_MAIN_BG_0x06020000);
37 // map vram bank B to main engine sprites (slot 0)
38 vramSetBankB(VRAM_B_MAIN_SPRITE);
39 // map vram to sub screen
40 vramSetBankC(VRAM_C_SUB_BG);
41
42 // enable extended palettes
43 bgExtPaletteEnable();
44
45 // setup text engine
46 int bgTextSub = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 4, 0);
47 uint16_t* textVideoBufferSub = bgGetGfxPtr(bgTextSub);
48 bgSetPriority(bgTextSub, 0);
49 textMenu->configureText(TextConfig(textVideoBufferSub, &FONT_NAME, FONT_SIZE));
50
51 //populate version label
52
53 // setup main menu
55 snprintf(mainMenuCmpt->versionLabel,
56 sizeof(mainMenuCmpt->versionLabel),
57 "v%u.%u.%u",
58 (unsigned)gameVersion.majorVersion,
59 (unsigned)gameVersion.minorVersion,
60 (unsigned)gameVersion.patchVersion);
61
62 std::array<UIMenu*, 10> menus = {mainMenuCmpt};
63 ae::broadcastEvent(Event::ConfigureUIMenu{textMenu, menus});
64 ae::broadcastEvent(Event::ShowMenu{mainMenuCmpt});
65
66 // initialize backgrounds
67 // check https://mtheall.com/vram.html to ensure bg fit in vram
68 bg[0] = bgInit(0, BgType_Text8bpp, BgSize_T_512x512, 4, 0); // silhouette
69 bg[1] = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 9, 2); // door
70 bg[2] = bgInit(2, BgType_ExRotation, BgSize_ER_256x256, 8, 5); // fog
71
72 // need to set priority to properly display
73 // 0 is highest, 3 is lowest
74 bgSetPriority(bg[0], 0); // silhouette
75 bgSetPriority(bg[2], 1); // fog
76 bgSetPriority(bg[1], 2); // door
77
78 // reset background vram
79 dmaFillHalfWords(0, bgGetMapPtr(bg[0]), 8192);
80 dmaFillHalfWords(1, bgGetMapPtr(bg[1]), 2048);
81 dmaFillHalfWords(2, bgGetMapPtr(bg[2]), 2048);
82
83 // load graphics
84 std::string bgPath = "graphics/MainMenuView/backgrounds/";
85 GraphicAsset silhouetteBg = graphics->loadGraphic(bgPath + "menuSilhouetteBackground/menuSilhouetteBackground");
86 GraphicAsset doorBg = graphics->loadGraphic(bgPath + "doorBackground/doorBackground");
87 GraphicAsset fogBg = graphics->loadGraphic(bgPath + "fogBackground/fogBackground");
88
89 dmaCopy(silhouetteBg.tiles, bgGetGfxPtr(bg[0]), silhouetteBg.tilesLen);
90 dmaCopy(doorBg.tiles, bgGetGfxPtr(bg[1]), doorBg.tilesLen);
91 dmaCopy(fogBg.tiles, bgGetGfxPtr(bg[2]), fogBg.tilesLen);
92
93 dmaCopy(silhouetteBg.map, bgGetMapPtr(bg[0]), silhouetteBg.mapLen);
94 dmaCopy(doorBg.map, bgGetMapPtr(bg[1]), doorBg.mapLen);
95 dmaCopy(fogBg.map, bgGetMapPtr(bg[2]), fogBg.mapLen);
96
97 vramSetBankE(VRAM_E_LCD);
98 dmaCopy(silhouetteBg.pal, &VRAM_E_EXT_PALETTE[0][0], silhouetteBg.palLen);
99 dmaCopy(doorBg.pal, &VRAM_E_EXT_PALETTE[1][0], doorBg.palLen);
100 dmaCopy(fogBg.pal, &VRAM_E_EXT_PALETTE[2][0], fogBg.palLen);
101 vramSetBankE(VRAM_E_BG_EXT_PALETTE);
102
103 graphics->unloadGraphic(silhouetteBg);
104 graphics->unloadGraphic(doorBg);
105 graphics->unloadGraphic(fogBg);
106
107 ui.hideBg(bg[2]);
108 bgSetCenter(bg[2], 128, 96); // pivot point on the screen
109 bgSetScroll(bg[2], 128, 96); // pivot point on the image
110
111 // for slide in animation
112 bgSetScroll(bg[0], -silhouetteX, -silhouetteY);
113 bgUpdate();
114
115 setBrightness(3, -16);
116
117 // Setup door blend initially
118 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG1 | BLEND_DST_BACKDROP;
119 REG_BLDALPHA = 0 | (16 << 8);
120
121 fadeTimer.start(ae::q20_12_t{0.8});
123
124 //setup music before returning, don't do it earlier
126
127 logMessage(LogSeverity::DEBUG, "MainMenuView initialized");
128}
129
131{
132 switch (transitionPhase)
133 {
135 {
136 if (!doorFadeIn)
137 {
138 if (fadeTimer.isFinished())
139 {
140 setBrightness(1, 0);
141 doorFadeIn = true;
142 fadeTimer.start(ae::q20_12_t{2.0});
143 }
144 else
145 {
146 int brightness = -16 + (fadeTimer.getProgress().raw_value() >> 8);
147 setBrightness(1, brightness);
148 }
149 }
150 else
151 {
152 if (fadeTimer.isFinished())
153 {
155 REG_BLDALPHA = 16 | (0 << 8);
156 }
157 else
158 {
159 int fadeVal = fadeTimer.getProgress().raw_value() >> 8; // 0 to 16
160 REG_BLDALPHA = fadeVal | ((16 - fadeVal) << 8);
161 }
162 }
163 break;
164 }
165
167 {
169 {
170 // skip the animation if the user skipped it
171 if (systemKeysDown != 0)
172 {
173 silhouetteX = 0;
174 silhouetteY = 0;
176 }
177
178 // animate X (moving right towards 0)
179 if (silhouetteX < 0 && frame % 5 == 0)
180 {
181 silhouetteX += (-silhouetteX) / 6 + 1;
182 }
183
184 // animate Y (moving up towards 0)
185 if (silhouetteY > 0 && frame % 5 == 0)
186 {
187 silhouetteY -= (silhouetteY / 6) + 1;
188 }
189
190 if (silhouetteX >= 0)
191 {
193 silhouetteX = 0;
194 }
195 if (silhouetteY <= 0)
196 {
198 silhouetteY = 0;
199 brightness = 0;
200 }
201 bgSetScroll(bg[0], -silhouetteX, -silhouetteY);
202 break;
203 }
204
205 // fade in bottom screen text
206 if (brightness < 16 && frame % 4 == 0)
207 {
208 brightness++;
209 setBrightness(2, brightness - 16);
210 }
211
212 // setup blending for fog
213 if (!displayFog)
214 {
215 displayFog = true;
216 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG2 | BLEND_DST_BACKDROP | BLEND_DST_BG1;
217 ui.showBg(bg[2]);
218 }
219
220 // fade in fog
221 if (displayFog && fogOpacity < 6 && frame % 4 == 0)
222 {
223 fogOpacity++;
224 REG_BLDALPHA = fogOpacity | ((16 - fogOpacity) << 8);
225 }
226
227 // rotate fog
228 if (displayFog && frame % 4 == 0)
229 {
230 waveAngle += 50;
231 int angle = MathManager::getInstance().sin(static_cast<ae::angle16_t>(waveAngle)).raw_value();
232
233 int rotationSpeed = baseSpeed + ((angle * fluctuation) >> 12);
234 currentRotation += rotationSpeed;
235 bgSetRotateScale(bg[2], currentRotation, 256, 256);
236 }
237
238 break;
239 }
240
242 {
243 break;
244 }
245 }
246
248}
249
251{
252 AudioManager::getInstance().stopAudio();
253 // transition both screens to black
254 for (int i = 0; i > -16; i--)
255 {
256 setBrightness(3, i);
257
258 // wait a few frames
259 for (int duration = 0; duration <= 2; duration++)
260 {
261 // TODO: replace with timer util
262 swiWaitForVBlank();
263 }
264 }
265
266 if (mainMenu != nullptr)
267 {
268 engine.destroyEntity(mainMenu);
269
270 mainMenu = nullptr;
271 graphics = nullptr;
272 textMenu = nullptr;
273 musicCmpt = nullptr;
274 sfxCmpt = nullptr;
275 }
277}
ViewState
Definition StateTypes.hpp:4
Manages the mapping of UIScreens to these hardware layers.
@ ARIA_OF_THE_SOUL
Definition audioDb.hpp:42
virtual void cleanup()
Definition BaseView.cpp:8
MainMenu * mainMenuCmpt
bool isSilhouetteStillMoving
TransitionPhase transitionPhase
void cleanup() override
void init() override
UIManager & ui
TextComponent * textMenu
ae::Entity * mainMenu
MusicComponent * musicCmpt
std::string FONT_NAME
ViewState update() override
GraphicsComponent * graphics
SFXComponent * sfxCmpt
static MainMenu * getInstance()
Definition MainMenu.cpp:26
Holds global variables used game-wide.
constexpr GameVersion gameVersion
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.
All parameters needed to configure UIMenus.
Definition UIEvents.hpp:52
Event to show specified menu.
Definition UIEvents.hpp:65
Holds loaded graphics data.
A struct that holds initial config values for the TextComponent.