persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
EnvironmentView.cpp
Go to the documentation of this file.
1#include "EnvironmentView.hpp"
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4#include "models/makoto.hpp"
6
7#include <nds.h>
8#include <string>
9
10namespace
11{
25std::string gritBaseName(const char* compiledFileName)
26{
27 std::string name(compiledFileName);
28 static const std::string suffix = ".img.bin";
29 if (name.size() > suffix.size() && name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0)
30 {
31 name.erase(name.size() - suffix.size());
32 }
33 return name;
34}
35} // namespace
36
37// -------------------------------------------------
38// Models
39
40const unsigned int* EnvironmentView::loadBitmap(const std::string& path, GraphicAsset& asset)
41{
42 asset = graphics->loadGraphic(path);
43 return reinterpret_cast<const unsigned int*>(asset.tiles);
44}
45
47{
48 GraphicAsset modelTextures[MODEL_MAKOTO_TEX_COUNT] = {};
49 const unsigned int* bitmapsMakoto[MODEL_MAKOTO_TEX_COUNT] = {nullptr};
50
51 const std::string basePath = fatBasePath + "models/makoto/";
52
53 // load textures
54 bitmapsMakoto[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_0] =
55 loadBitmap(basePath + "makoto_texture_0", modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_0]);
56 bitmapsMakoto[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_1] =
57 loadBitmap(basePath + "makoto_texture_1", modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_1]);
58 bitmapsMakoto[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_2] =
59 loadBitmap(basePath + "makoto_texture_2", modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_2]);
60 bitmapsMakoto[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_3] =
61 loadBitmap(basePath + "makoto_texture_3", modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_3]);
62 bitmapsMakoto[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_4] =
63 loadBitmap(basePath + "makoto_texture_4", modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_4]);
64
65 makoto_loadTextures(*animationCtrl, (const unsigned int**)bitmapsMakoto);
66
67 // unload texture graphics
68 graphics->unloadGraphic(modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_0]);
69 graphics->unloadGraphic(modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_1]);
70 graphics->unloadGraphic(modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_2]);
71 graphics->unloadGraphic(modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_3]);
72 graphics->unloadGraphic(modelTextures[MODEL_MAKOTO_TEX_MAKOTO_TEXTURE_4]);
73}
74
75// -------------------------------------------------
76// Environment
77
79{
80 GraphicAsset envTextures[MAX_ENVIRONMENT_TEXTURES] = {};
81 std::array<const unsigned int*, MAX_ENVIRONMENT_TEXTURES> bitmapsEnv = {nullptr};
82
83 const std::string basePath = fatBasePath + "environments/" + dbEntry->name + "/";
84
85 for (int i = 0; i < dbEntry->textureCount; ++i)
86 {
87 bitmapsEnv[i] = loadBitmap(basePath + gritBaseName(dbEntry->textures[i].name), envTextures[i]);
88 }
89
90 if (!env.load(dbEntry, bitmapsEnv))
91 {
92 logMessage(LogSeverity::ERROR, "Failed to load environment " + std::string(dbEntry->name));
93 textSub->drawText(
94 "EnvironmentView: failed to load environment " + std::string(dbEntry->name), 0, 0, TextColor::Red);
95 }
96
97 for (int i = 0; i < dbEntry->textureCount; ++i)
98 {
99 graphics->unloadGraphic(envTextures[i]);
100 }
101}
102
103// -------------------------------------------------
104// Lifecycle
105
107{
108 // clearing so nothing from the previous enviorment shows during load
109 render.cleanup3DView();
110
111 if (environment == nullptr)
112 {
113 environment = engine.createEntity();
114 graphics = engine.createComponent<GraphicsComponent>();
115 text = engine.createComponent<TextComponent>();
116 textSub = engine.createComponent<TextComponent>();
117 textSubAlt = engine.createComponent<TextComponent>();
118 textSubOptions = engine.createComponent<TextComponent>();
119 musicCmpt = engine.createComponent<MusicComponent>();
120 sfxCmpt = engine.createComponent<SFXComponent>();
121
122 environment->addComponent(graphics);
123 environment->addComponent(text);
124 environment->addComponent(textSub);
125 environment->addComponent(textSubAlt);
126 environment->addComponent(textSubOptions);
127 environment->addComponent(musicCmpt);
128 environment->addComponent(sfxCmpt);
129
130 UISystem::getInstance().setSFXComponent(sfxCmpt);
131 }
132
133 if (player != nullptr)
134 {
135 movement = engine.createComponent<MovementComponent>();
136 dialogue = engine.createComponent<DialogueComponent>();
137
138 player->addComponent(movement);
139 player->addComponent(dialogue);
140 }
141
142 // set modes
143 videoSetMode(MODE_5_3D | DISPLAY_BG3_ACTIVE);
144 videoSetModeSub(MODE_3_2D | DISPLAY_BG3_ACTIVE | DISPLAY_SPR_ACTIVE);
145
146 // set vram
147 vramSetBankA(VRAM_A_TEXTURE_SLOT0); // texture slot 0
148 vramSetBankB(VRAM_B_TEXTURE_SLOT1); // texture slot 1
149
150 vramSetBankC(VRAM_C_SUB_BG);
151 vramSetBankD(VRAM_D_SUB_SPRITE);
152 vramSetBankE(VRAM_E_MAIN_BG);
153 vramSetBankH(VRAM_H_SUB_BG_EXT_PALETTE);
154 vramSetBankI(VRAM_I_SUB_SPRITE_EXT_PALETTE);
155 bgExtPaletteEnableSub();
156
157 View3DConfig default3DConfig{
158 settings : GL_ANTIALIAS | GL_TEXTURE_2D | GL_FOG | GL_OUTLINE,
159 polyParams : POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FOG,
160 outlineColor : RGB15(0, 0, 0),
161 fogColor : RGBA(22, 25, 28, 31),
162 };
163 render.initialize3DView(default3DConfig);
164
165 // Fog - storing these for now
166 // glFogColor(22, 25, 28, 31); // daytime blue
167 // glFogColor(30, 25, 16, 31); // evening orange
168 // glFogColor(16, 17, 19, 31); // rainy gray
169
171 if (!dbEntry)
172 {
173 logMessage(LogSeverity::ERROR, "No EnvironmentDbEntry for this room");
174 return;
175 }
176
177 // setup sub screen
178 // https://mtheall.com/vram.html#SUB=1&T0=1&NT0=512&MB0=2&TB0=1&S0=0&T1=3&NT1=128&MB1=5&TB1=0&T2=1&NT2=512&MB2=3&TB2=3&S2=0&T3=1&NT3=512&MB3=4&TB3=5&S3=0
179 bgSharedSub1 = bgInitSub(0, BgType_Text4bpp, BgSize_T_256x256, 0, 1);
180 bgSharedSub2 = bgInitSub(2, BgType_Text4bpp, BgSize_T_256x256, 2, 2);
181 bgSharedSub3 = bgInitSub(1, BgType_Text4bpp, BgSize_T_256x256, 4, 3);
182
183 dmaFillHalfWords(0, bgGetMapPtr(bgSharedSub1), 2048);
184 dmaFillHalfWords(0, bgGetMapPtr(bgSharedSub2), 2048);
185 dmaFillHalfWords(0, bgGetMapPtr(bgSharedSub3), 2048);
186
187 // adjust sub screen image and console to sit correctly on each other
188 bgSetPriority(bgSharedSub1, 1);
189 bgSetPriority(bgSharedSub2, 2);
190 bgSetPriority(bgSharedSub3, 3);
191 bgUpdate();
192
193 // setup MovementComponent on player entity (room-specific map/tuning, generic call site)
195 movement->start();
196
197 setupCamera();
198 ae::broadcastEvent(Event::ConfigureCamera(camConfig));
199
200 // setup character model (identical across rooms)
201 std::string modelPath = fatBasePath + "models/";
202 animationCtrl->loadModel((modelPath + "makoto/makoto.bin").c_str());
203 setupModel();
204
205 // setup main screen text engine
206 int bgText = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
207 textVideoBuffer = (uint16_t*)bgGetGfxPtr(bgText);
208 bgSetPriority(bgText, 0); // set text layer on main to be on top of 3D view
209
210 // setup sub screen text engine
211 int bgTextSub = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 4, 0);
212 textVideoBufferSub = (uint16_t*)bgGetGfxPtr(bgTextSub);
213 bgSetPriority(bgTextSub, 0);
214
215 // config text/textSub
216 setupText();
217
218 // setup environment geometry/textures (fully generic, data-driven)
220
221 // setup UI
222 // NOTE: bg 0 is the 3D view
223 bgMain = {1, 2};
224 // NOTE: Setting the first index to anything other than bgSharedSub results in black bg (but sprites still load)
226
227 // initialize sub sprite engine with 1D mapping, 128 byte boundry, external palette support
228 oamInit(&oamSub, SpriteMapping_1D_128, true);
229
230 // setup UIScreen, UIMenu
231 setupUI();
232
233 // setup dialogue
235
236 // setup music (room-specific path/loop points)
237 setupMusic();
238
239 // setup view phases
240 prevPauseState = false;
241 prevDialogueState = false;
242 prevEnvironmentState = false;
243 isBattleMenuActive = false;
244 prevBattleState = false;
246
247 bgSetPriority(0, 2); // set 3D view on main to be behind text layer
248
249 lineSpacing = textSub->getLineSpacing();
250
251 logMessage(LogSeverity::DEBUG, "EnvironmentView initialized for: " + std::string(dbEntry->name));
252}
253
255{
256 glMatrixMode(GL_MODELVIEW);
257 glLoadIdentity();
258
259 bgUpdate();
260 oamUpdate(&oamSub);
261
262 if (dialogueScreen != nullptr)
263 {
264 dialogueScreen->updateAnimation();
265 }
266
267 if (pauseMenuScreen != nullptr)
268 {
269 pauseMenuScreen->updateAnimation();
270 }
271
272 switch (phase)
273 {
275 {
276 if (!prevBattleState)
277 {
278 prevBattleState = true;
279
280 ae::broadcastEvent(Event::HideAllScreens{});
281 ae::broadcastEvent(Event::ShowMenu{battleMenuCmpt});
282
283 movement->stop();
284 ae::broadcastEvent(Event::StopCamera{});
285
286 startBattle();
287 }
288
289 if (!BattleSystem::getInstance().isActive() && prevBattleState)
290 {
291 prevBattleState = false;
292
293 ae::broadcastEvent(Event::HideAllMenus{});
294 ae::broadcastEvent(Event::ShowScreen{menuHUDScreen});
295
297
298 movement->start();
299 ae::broadcastEvent(Event::StartCamera{});
300
302
303 setupMusic();
304 }
305
306 break;
307 }
308
309 case ViewPhase::PAUSE:
310 {
311 if (!prevPauseState)
312 {
313 movement->stop();
314 ae::broadcastEvent(Event::StopCamera{});
315
316 prevPauseState = true;
317
318 // ShowMenu clears textSub, so show the screen after it to keep the text displayed
319 ae::broadcastEvent(Event::ShowMenu{pauseMenuCmpt});
320 ae::broadcastEvent(Event::ShowScreen{pauseMenuScreen});
321 }
322
323 // menu clears textSub when navigating,so a redraw is required
324 if (systemKeysDown & (KEY_A | KEY_B | KEY_UP | KEY_DOWN))
325 {
326 pauseMenuScreen->renderText();
327 }
328
329 if ((systemKeysDown & KEY_X) || pauseMenuCmpt->isClosed)
330 {
331 prevPauseState = false;
332 textSub->clearScreen();
333 pauseMenuScreen->cancelAnimation();
334
335 ae::broadcastEvent(Event::HideAllMenus{});
336
337 prevEnvironmentState = false;
338
339 movement->start();
340 ae::broadcastEvent(Event::StartCamera{});
341
343 }
344
345 break;
346 }
347
349 {
350 bool isActive = dialogue->isActive();
351
352 if (!isActive && !prevDialogueState)
353 {
354 ae::broadcastEvent(Event::ShowScreen{dialogueScreen});
355
356 movement->stop();
357 ae::broadcastEvent(Event::StopCamera{});
358
359 if (dialogueFirstLine != nullptr)
360 {
362 }
363
364 prevDialogueState = true;
365 }
366 else if (!isActive && prevDialogueState)
367 {
368 ae::broadcastEvent(Event::HideAllScreens{});
369
370 prevDialogueState = false;
371 prevEnvironmentState = false;
372
373 movement->start();
374 ae::broadcastEvent(Event::StartCamera{});
375
377 }
378
379 break;
380 }
381
383 {
385 {
386 ae::broadcastEvent(Event::ShowScreen{menuHUDScreen});
388 }
389
390 CharacterPosition charPos = movement->isCharacterAt();
391 camPos = CameraSystem::getInstance().getCameraPosition();
392
393 if (systemKeysDown & KEY_X)
394 {
395 textSub->clearScreen();
396 prevEnvironmentState = false;
397
398 movement->stop();
399 ae::broadcastEvent(Event::StopCamera{});
400
402 break;
403 }
404
405 if (systemKeysDown & KEY_TOUCH)
406 {
407 touchRead(&touch);
408
409 if (menuHUDScreen->onTouch(&touch) == 1)
410 {
411 prevEnvironmentState = false;
412
413 movement->stop();
414 ae::broadcastEvent(Event::StopCamera{});
415
417 break;
418 }
419 }
420
421 ViewState tileResult = onTileCheck(movement->isTileAt(), systemKeysDown);
422
423 if (tileResult != ViewState::KEEP_CURRENT)
424 {
425 movement->stop();
426 ae::broadcastEvent(Event::StopCamera{});
427
428 musicCmpt->pauseMusic();
429 return tileResult;
430 }
431
432 gluLookAtf32(camPos.eye.x.raw_value(),
433 (camPos.eye.y + getCameraYOffset()).raw_value(),
434 camPos.eye.z.raw_value(),
435 camPos.target.x.raw_value(),
436 camPos.target.y.raw_value(),
437 camPos.target.z.raw_value(),
438 camPos.up.x.raw_value(),
439 camPos.up.y.raw_value(),
440 camPos.up.z.raw_value());
441
442 // environment
443 glPushMatrix();
444 glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FOG | POLY_ID(0));
445 env.draw();
446 env.drawBillboards(Globals::enableBillboards, camPos.eye.x, camPos.eye.y, camPos.eye.z);
447 glPopMatrix(1);
448
449 // model
450 glPushMatrix();
451
452 glTranslatef32(charPos.x.raw_value(), charPos.y.raw_value(), charPos.z.raw_value());
453 glRotateYi(MathManager::getInstance().degreesToCyclicAngle(charPos.facingAngle));
454 glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FOG | POLY_ID(1));
455 animationCtrl->render();
456 glPopMatrix(1);
457
458 glFlush(0);
459
461 {
462 if (frame % 60 == 30) // restricting this 1Hz otherwise it tanks performance
463 {
464 textSub->clearArea(1, 120, 128, 72);
465 char buf[128];
466 std::string debugText = "";
467 std::sprintf(buf, "Touch x = %04X, %04X\n", touch.rawx, touch.px);
468 debugText += buf;
469 std::sprintf(buf, "Touch y = %04X, %04X\n", touch.rawy, touch.py);
470 debugText += buf;
471 std::sprintf(buf,
472 "tile(x,z): %d, %d\n",
473 (int)(MathManager::getInstance().div(charPos.x + dbEntry->worldOffsetX, tileSize)),
474 (int)(MathManager::getInstance().div(charPos.z + dbEntry->worldOffsetZ, tileSize)));
475 debugText += buf;
476 std::sprintf(buf, "translate(x,z): %d, %d\n", (int)(charPos.x * 100), (int)(charPos.z * 100));
477 debugText += buf;
478 std::sprintf(buf,
479 "angle(w,c): %d, %d\n",
480 (int)(CameraSystem::getInstance().getAngle() * 100),
481 (int)(charPos.facingAngle * 100));
482 debugText += buf;
483
484 // screen height - 5 lines * (size of text in each line + spacing between each line)
485 textSub->drawText(debugText, 1, 192 - 5 * (fontSize + lineSpacing), TextColor::Red);
486 }
487 }
488
489 break;
490 }
491
492 default:
493 {
495 break;
496 }
497 }
498
499 animationCtrl->update();
500
502}
503
505{
506 logMessage(LogSeverity::DEBUG, "EnvironmentView cleaned up");
507
508 cleanupHook();
509
510 // entity
511 if (environment != nullptr)
512 {
513 engine.destroyEntity(environment);
514
515 environment = nullptr;
516 graphics = nullptr;
517 text = nullptr;
518 textSub = nullptr;
519 textSubAlt = nullptr;
520 textSubOptions = nullptr;
521 musicCmpt = nullptr;
522 }
523
524 // entity
525 if (player != nullptr)
526 {
527 engine.destroyComponent(movement);
528 engine.destroyComponent(dialogue);
529
530 movement = nullptr;
531 dialogue = nullptr;
532 }
533
534 dialogueScreen = nullptr;
535 menuHUDScreen = nullptr;
536 pauseMenuScreen = nullptr;
537 battleMenuCmpt = nullptr;
538 pauseMenuCmpt = nullptr;
539
540 dbEntry = nullptr;
541
542 dialogueFirstLine = nullptr;
543
544 animationCtrl->unloadTextures();
545 animationCtrl->stop();
546
547 env.cleanup();
548
550}
551
553{
556
558
559 pauseMenuScreen->setTextComponent(textSub);
560 pauseMenuScreen->setOptionsTextComponent(textSubOptions);
562 pauseMenuCmpt->setVisibleOptions(pauseMenuScreen->getVisibleOptions());
563 pauseMenuScreen->setYenText("9,999,999 Yen", TextColor::DarkestBlue);
564 pauseMenuScreen->setPersonaText("Ara Mitama", TextColor::DarkestBlue);
565 pauseMenuScreen->setArcanaText("Chariot", TextColor::DarkestBlue);
566 pauseMenuScreen->setLevelText("Level 99", TextColor::DarkestBlue);
567 pauseMenuScreen->setDescriptionText("View Social Link details", TextColor::White);
568}
569
570void EnvironmentView::setupMenuHUD(const std::string& location)
571{
573
574 menuHUDScreen->setTextComponent(textSub);
575 menuHUDScreen->setTextContent(location, TextColor::White);
576}
System for managing battle logic & states.
constexpr int MAX_ENVIRONMENT_TEXTURES
ViewState
Definition StateTypes.hpp:4
@ DarkestBlue
Definition TextTypes.hpp:24
@ White
Definition TextTypes.hpp:14
@ Red
Definition TextTypes.hpp:27
virtual void cleanup()
Definition BaseView.cpp:8
virtual const EnvironmentDbEntry * getEnvironmentDbEntry()=0
virtual ViewState onTileCheck(TileType tile, uint32_t pressed)=0
std::array< int, 2 > bgMain
TextComponent * textSubAlt
TextComponent * textSubOptions
void setupEnvironment()
Loads and uploads a room's environment geometry and textures, driven entirely by dbEntry.
virtual void cleanupHook()
virtual void setupText()=0
MenuHUDScreen * menuHUDScreen
MusicComponent * musicCmpt
const EnvironmentDbEntry * dbEntry
AnimationController * animationCtrl
MovementComponent * movement
Event::CameraPosition camPos
Event::ConfigureCamera camConfig
Dialogue * dialogueFirstLine
uint16_t * textVideoBuffer
std::array< int, 3 > bgSub
virtual void setupMusic()=0
ViewState update() override
Per-frame update for this room's view.
void init() override
One-time setup for a room.
SFXComponent * sfxCmpt
const ae::q20_12_t tileSize
GraphicsComponent * graphics
virtual void setupDialogue()
touchPosition touch
const unsigned int * loadBitmap(const std::string &path, GraphicAsset &asset)
Loads a single .grit asset and returns its raw tile pointer.
RenderManager & render
virtual void setupUI()
void cleanup() override
Tears down everything a room's view had set up.
void setupPauseMenu()
Sets up the pause menu.
virtual ae::q20_12_t getCameraYOffset() const
uint16_t * textVideoBufferSub
DialogueScreen * dialogueScreen
PauseMenu * pauseMenuCmpt
TextComponent * text
virtual void setupMovement()=0
virtual void setupCamera()
void setupMenuHUD(const std::string &location)
Sets up the menu HUD.
BattleMenu * battleMenuCmpt
virtual void startBattle()
TextComponent * textSub
ae::Entity * environment
PauseMenuScreen * pauseMenuScreen
DialogueComponent * dialogue
std::string optionsFontName
static bool enableDebugPrint
Definition globals.hpp:43
static bool enableBillboards
Definition globals.hpp:42
static MenuHUDScreen * getInstance()
static PauseMenuScreen * getInstance()
static PauseMenu * getInstance()
Definition PauseMenu.cpp:26
Holds global variables used game-wide.
std::string fatBasePath
Definition main.cpp:38
ae::Entity * player
Definition main.cpp:32
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.
ae::q20_12_t facingAngle
All parameters needed to configure a CameraSystem in one call.
Event to hide all menus.
Definition UIEvents.hpp:76
Event to trigger hiding all UIScreens.
Definition UIEvents.hpp:96
Event to show specified menu.
Definition UIEvents.hpp:65
Event to show specified screen.
Definition UIEvents.hpp:83
Activates the CameraSystem update loop.
Deactivates the CameraSystem update loop.
Holds loaded graphics data.
Data structure to store RGBA values as 8-bit signed integers.
A struct that holds initial config values for the TextComponent.
Data structure to store 3D view configuration parameters.