NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
d_system.cpp
1#include <cstddef>
2#include <game/bases/d_system.hpp>
3#include <game/bases/d_2d.hpp>
5#include <game/bases/d_dvd.hpp>
6#include <game/bases/d_dvd_err.hpp>
7#include <game/bases/d_dylink.hpp>
8#include <game/bases/d_effectmanager.hpp>
9#include <game/bases/d_fader.hpp>
10#include <game/bases/d_font_manager.hpp>
11#include <game/bases/d_game_key.hpp>
12#include <game/bases/d_graph.hpp>
13#include <game/bases/d_hbm.hpp>
14#include <game/bases/d_nand_thread.hpp>
15#include <game/bases/d_message.hpp>
16#include <game/bases/d_pad.hpp>
17#include <game/bases/d_remocon_manager.hpp>
18#include <game/bases/d_reset.hpp>
19#include <game/bases/d_rom_font_manager.hpp>
20#include <game/bases/d_s_stage.hpp>
21#include <game/bases/d_save_mng.hpp>
22#include <game/cLib/c_counter.hpp>
23#include <game/mLib/m_3d.hpp>
24#include <game/mLib/m_allocator.hpp>
25#include <game/mLib/m_heap.hpp>
26#include <game/mLib/m_pad.hpp>
27#include <game/mLib/m_video.hpp>
28#include <game/sLib/s_Phase.hpp>
29#include <lib/egg/core/eggSystem.h>
30#include <lib/egg/core/eggTextureBuffer.h>
31#include <lib/revolution/KPAD.h>
32#include <lib/revolution/VI.h>
33#include <constants/sjis_constants.h>
34
35const char dSystem::sc_EffectManagerHeap1Name[] = EFFECT_HEAP_1_NAME;
36const char dSystem::sc_EffectManagerHeap2Name[] = EFFECT_HEAP_2_NAME;
37
38EGG::Heap *dSystem::s_EffectManagerHeap1;
39EGG::Heap *dSystem::s_EffectManagerHeap2;
40
41OSThread *s_MainThread;
42
43template<> EGG::TSystem<>::Configuration *EGG::TSystem<>::sConfiguration = nullptr;
44
45static EGG::TSystem<>::Configuration s_configuration;
46
47EGG::Heap *dSys_c::ms_RootHeapMem1;
48EGG::Heap *dSys_c::ms_RootHeapMem2;
49
50const char dSystem::sc_FontManagerHeapName[] = FONT_MANAGER_HEAP_NAME;
51EGG::Heap *dSystem::s_FontManagerHeap;
52
53void dSystem::createEffectManagerHeap(EGG::Heap *heap1, EGG::Heap *heap2) {
54 s_EffectManagerHeap1 = mHeap::createFrmHeap(HEAP_SIZE_EFFECT_MANAGER1, heap1, sc_EffectManagerHeap1Name, 0x20, mHeap::OPT_NONE);
55 s_EffectManagerHeap2 = mHeap::createFrmHeap(HEAP_SIZE_EFFECT_MANAGER2, heap2, sc_EffectManagerHeap2Name, 0x20, mHeap::OPT_THREAD_SAFE);
56}
57
58void dSys_c::beginRender() {
59 EGG::TSystem<>::sConfiguration->mpDisplay->beginRender();
60}
61
62class dummy_unused_static_local {
63public:
64 dummy_unused_static_local() {}
65
66 int x[4];
67};
68
69void dSys_c::endRender() {
70 // [@LOCAL@ gets deadstripped, but the guard stays]
71 static const dummy_unused_static_local a;
72 static const dummy_unused_static_local b;
73 static const dummy_unused_static_local c;
74 EGG::TSystem<>::sConfiguration->mpDisplay->endRender();
75}
76
78 EGG::TSystem<>::sConfiguration->mpDisplay->beginFrame();
79 EGG::TSystem<>::sConfiguration->onBeginFrame();
80}
81
83 EGG::TSystem<>::sConfiguration->mpDisplay->endFrame();
84 EGG::TSystem<>::sConfiguration->onEndFrame();
85}
86
87bool dSys_c::setBlack(bool makeBlack) {
88 EGG::Display *display = EGG::TSystem<>::sConfiguration->mpDisplay;
89 EGG::Video *video = EGG::BaseSystem::mConfigData->getVideo();
90 if (makeBlack != video->isBlack() && (display->mScreenStateFlag & 1) == 0) {
91 display->mScreenStateFlag |= 1;
92 return true;
93 }
94 return false;
95}
96
97void dSys_c::calcAudio() {
98 dAudio::execute();
99}
100
101void dSys_c::setFrameRate(u8 rate) {
102 if (EGG::TSystem<>::sConfiguration->mpDisplay != nullptr) {
103 EGG::TSystem<>::sConfiguration->mpDisplay->mFrameRate = rate;
104 }
105}
106
107void dSys_c::setClearColor(nw4r::ut::Color color) {
108 if (EGG::TSystem<>::sConfiguration->mpDisplay != nullptr) {
109 EGG::TSystem<>::sConfiguration->mpDisplay->setClearColor(color);
110 }
111}
112
113nw4r::ut::Color dSys_c::getClearColor() {
114 if (EGG::TSystem<>::sConfiguration->mpDisplay != nullptr) {
115 return EGG::TSystem<>::sConfiguration->mpDisplay->getClearColor();
116 }
117 return nw4r::ut::Color::BLACK;
118}
119
121 s_MainThread = OSGetCurrentThread();
122
123 EGG::TSystem<>::Configuration *config = &s_configuration;
124 EGG::TSystem<>::sConfiguration = config;
125 EGG::BaseSystem::mConfigData = config;
126 config->initialize();
127
128 ms_RootHeapMem1 = mHeap::createExpHeap(-1, EGG::BaseSystem::mConfigData->mRootHeapMem1, "dSys_c::RootHeapMEM1", 0x20, mHeap::OPT_THREAD_SAFE);
129 ms_RootHeapMem2 = mHeap::createExpHeap(-1, EGG::BaseSystem::mConfigData->mRootHeapMem2, "dSys_c::RootHeapMEM2", 0x20, mHeap::OPT_THREAD_SAFE);
130 EGG::Heap *rootHeap1 = ms_RootHeapMem1;
131 EGG::Heap *rootHeap2 = ms_RootHeapMem2;
132
133 mVideo::create();
134
136 mHeap::createGameHeap1(HEAP_SIZE_GAME1, ms_RootHeapMem1);
137 mHeap::createGameHeap2(HEAP_SIZE_GAME2, ms_RootHeapMem2);
141 mAllocator_c::init(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
142
144 EGG::TextureBuffer::initialize(HEAP_SIZE_TEXTURE_BUFFER, ms_RootHeapMem2);
145 mHeap::setCurrentHeap(prevHeap);
146
147 mPad::create();
148 dGameKey_c::createInstance(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
149 dRemoconMng_c::create(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
150 for (int i = 0; i < 4; i++) {
151 KPADDisableDPD(i);
152 }
153 mPad::beginPad();
154 mPad::g_currentCore->setPosParam(0.1f, 1.0f);
155 mPad::endPad();
156
157 dGraph_c::create(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT], ms_RootHeapMem1, ms_RootHeapMem2);
158
159 dFader_c::createFader(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
160 dFader_c::setFader(dFader_c::FADE);
161
162 if (dSystem::s_FontManagerHeap == nullptr) {
163 dSystem::s_FontManagerHeap = mHeap::createFrmHeap(HEAP_SIZE_FONT_MANAGER, ms_RootHeapMem2, dSystem::sc_FontManagerHeapName, 0x20, mHeap::OPT_THREAD_SAFE);
164 }
165
166 dRomFontMgr_c::createInstance(dSystem::s_FontManagerHeap);
167
168 dDvdErr_c::createInstance(mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM1]);
169
170 cCounter_c::clear();
171
172 OSGetCurrentThread();
173 dDvd::create(OSGetThreadPriority() - 1, mHeap::g_commandHeap, mHeap::g_archiveHeap);
174
175 dSaveMng_c::create(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
176 dNandThread_c::create(mHeap::g_gameHeaps[mHeap::GAME_HEAP_DEFAULT]);
177 dReset::Manage_c::CreateInstance(mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM1]);
178 dHbm::Manage_c::CreateInstance(mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM1]);
179 dAudio::init(ms_RootHeapMem2);
180 dSystem::createEffectManagerHeap(ms_RootHeapMem1, ms_RootHeapMem2);
181 dScStage_c::createReplayDataHeap(ms_RootHeapMem2, HEAP_SIZE_REPLAY_DATA, mHeap::OPT_THREAD_SAFE);
182
183 rootHeap1->disableAllocation();
184 rootHeap2->disableAllocation();
185
187 setFrameRate(1);
188 setClearColor(255);
189
190 d2d::init();
191}
192
193namespace {
194
195sPhase_c::METHOD_RESULT_e myDylinkInitPhase_0(void *) {
196 dDyl::InitAsync();
197 return sPhase_c::OK;
198}
199
200sPhase_c::METHOD_RESULT_e myDylinkInitPhase_1(void *) {
201 if (dDyl::InitAsyncIsDone()) {
202 return sPhase_c::OK;
203 }
204 return sPhase_c::WAIT;
205}
206
207sPhase_c::METHOD_RESULT_e myDylinkInitPhase_4(void *) {
209 return sPhase_c::OK;
210}
211
212sPhase_c::phaseMethod myDylinkInitPhaseMethod[] = {
213 myDylinkInitPhase_0,
214 myDylinkInitPhase_1,
215 myDylinkInitPhase_4
216};
217
218sPhase_c myDylinkInitPhase(myDylinkInitPhaseMethod, ARRAY_SIZE(myDylinkInitPhaseMethod));
219
220} // anonymous namespace
221
223 bool dvdErr = dDvdErr_c::m_pInstance->isErrorOccured();
224 bool inHbm = dHbm::Manage_c::GetInstance()->m_1d0 == 2;
225 beginFrame();
226 m3d::calcMaterial();
227 dPad::beginPad_BR();
228 m3d::reset();
229 beginRender();
230
231 if (!inHbm && !dvdErr) {
232 dGraph_c::ms_Instance->painter();
234 }
235
236 dHbm::Manage_c::GetInstance()->DrawMenu();
238 dDvdErr_c::m_pInstance->draw();
239 dHbm::Manage_c::GetInstance()->DrawIcon();
240 endRender();
241
242 if (!inHbm) {
243 m2d::reset();
244 m3d::clear();
245 }
246
247 dDvdErr_c::m_pInstance->execute();
248 bool dvdErrNew = dDvdErr_c::m_pInstance->isErrorOccured();
249 if (dvdErrNew && !dvdErr) {
250 VIEnableDimming(1);
251 }
252
253 dHbm::Manage_c::GetInstance()->Calculate();
254 dReset::Manage_c::GetInstance()->Calculate();
255 inHbm = dHbm::Manage_c::GetInstance()->m_1d0 == 2;
256
257 dRemoconMng_c::execute();
258 dScStage_c::play();
259 dGameKey_c::m_instance->read();
260 dAudio::updateBgmInfo();
261
262 if (!inHbm && !dvdErrNew) {
263 if (myDylinkInitPhase.callMethod(nullptr) == sPhase_c::DONE) {
266 cCounter_c::m_exeFrame++;
267 }
268 dFader_c::calc();
269 }
270
271 calcAudio();
272 dPad::endPad_BR();
273 endFrame();
274 cCounter_c::m_gameFrame++;
275}
276
277mDvd_toMainRam_c *dSystem::l_breffCommand;
278mDvd_toMainRam_c *dSystem::l_breftCommand;
279
280sPhase_c::METHOD_RESULT_e dSystem::createEffectManagerPhase1(void *) {
281 if (l_breffCommand == nullptr) {
282 l_breffCommand = mDvd_toMainRam_c::create("/Effect/effect_wnmario.breff", 0, s_EffectManagerHeap2);
283 if (l_breffCommand == nullptr) {
284 return sPhase_c::WAIT;
285 }
286 }
287 if (l_breftCommand == nullptr) {
288 l_breftCommand = mDvd_toMainRam_c::create("/Effect/effect_wnmario.breft", 0, s_EffectManagerHeap2);
289 if (l_breftCommand == nullptr) {
290 return sPhase_c::WAIT;
291 }
292 }
293 if (!l_breffCommand->isDone() || !l_breftCommand->isDone()) {
294 return sPhase_c::WAIT;
295 }
296 return sPhase_c::OK;
297}
298
299sPhase_c::METHOD_RESULT_e dSystem::createEffectManagerPhase2(void *) {
300 EffectManager_c::create(s_EffectManagerHeap1, s_EffectManagerHeap2);
301 s_EffectManagerHeap2->dump();
302 EffectManager_c::setResource(l_breffCommand->mpData, l_breftCommand->mpData);
303
304 l_breffCommand->destroy();
305 l_breftCommand->destroy();
306
307 l_breffCommand = nullptr;
308 l_breftCommand = nullptr;
309
310 return sPhase_c::OK;
311}
312
313sPhase_c::METHOD_RESULT_e dSystem::createFontManagerPhase(void *) {
314 if (!dFontMng_c::create(s_FontManagerHeap)) {
315 return sPhase_c::WAIT;
316 }
317
318 s_FontManagerHeap->dump();
319 return sPhase_c::OK;
320}
321
322const char dSystem::sc_MessageManagerHeapName[] = MESSAGE_MANAGER_HEAP_NAME;
323EGG::Heap *dSystem::s_MessageManagerHeap;
324
325sPhase_c::METHOD_RESULT_e dSystem::createMessageManagerPhase(void *) {
326 if (s_MessageManagerHeap == nullptr) {
327 s_MessageManagerHeap = mHeap::createFrmHeap(HEAP_SIZE_MESSAGE_MANAGER, mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM2], dSystem::sc_MessageManagerHeapName, 0x20, mHeap::OPT_THREAD_SAFE);
328 }
329
330 if (!dMessage_c::create(s_MessageManagerHeap)) {
331 return sPhase_c::WAIT;
332 }
333
334 s_MessageManagerHeap->dump();
335 s_MessageManagerHeap->adjust();
336 return sPhase_c::OK;
337}
338
339void *dSystem::s_OrgMEM1ArenaLo;
340void *dSystem::s_NewMEM1ArenaLo;
341void *dSystem::s_OrgMEM1ArenaHi;
342void *dSystem::s_NewMEM1ArenaHi;
343
344void dSystem::fixArena() {
345 s_OrgMEM1ArenaLo = OSGetMEM1ArenaLo();
346 s_NewMEM1ArenaLo = (void *) ARENA_LO;
347 if (s_OrgMEM1ArenaLo < s_NewMEM1ArenaLo) {
348 OSSetMEM1ArenaLo(s_NewMEM1ArenaLo);
349 }
350
351 s_OrgMEM1ArenaHi = OSGetMEM1ArenaHi();
352 s_NewMEM1ArenaHi = (void *) ARENA_HI;
353 if (s_OrgMEM1ArenaHi > s_NewMEM1ArenaHi) {
354 OSSetMEM1ArenaHi(s_NewMEM1ArenaHi);
355 }
356}
357
358void dSystem::fixHeapsSub(EGG::ExpHeap *heap, int size) {
359 heap->setGroupID(255);
360
361 void *block = heap->alloc(size, -0x20);
362 if (block == nullptr) {
363 while (true);
364 }
365
366 while (true) {
367 size_t allocatableSize = heap->getAllocatableSize(4);
368 if (allocatableSize == 0) {
369 break;
370 }
371 heap->alloc(allocatableSize, 4);
372 }
373
374 heap->free(block);
375 heap->setGroupID(1);
376}
377
378void dSystem::fixHeaps() {
379 EGG::ExpHeap *gameHeap1 = mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM1];
380 EGG::ExpHeap *gameHeap2 = mHeap::g_gameHeaps[mHeap::GAME_HEAP_MEM2];
381 EGG::ExpHeap *archiveHeap = mHeap::g_archiveHeap;
382 fixHeapsSub(gameHeap1, HEAP_SIZE_GAME1 - 0x300000);
383 fixHeapsSub(gameHeap2, HEAP_SIZE_GAME2 - 0x200000);
384 fixHeapsSub(archiveHeap, HEAP_SIZE_ARCHIVE - 0x800000);
385}
static void initLoader()
Sets the callbacks for the scrapped relocatable profile system.
Definition d_base.cpp:82
static dHbm::Manage_c * GetInstance()
Gets a pointer to the instance of this class.
static dReset::Manage_c * GetInstance()
Gets a pointer to the instance of this class.
static void setStartScene()
Sets up the scene to be shown when the game boots up.
Definition d_scene.cpp:133
static dScene_c * createNextScene()
Creates and returns a root base for the next scene.
Definition d_scene.cpp:145
static void beginFrame()
Marks the beginning of a frame.
Definition d_system.cpp:77
static void create()
Initializes the game.
Definition d_system.cpp:120
static void endFrame()
Marks the end of a frame.
Definition d_system.cpp:82
static void execute()
Executes one frame of the game loop.
Definition d_system.cpp:222
static void mainLoop()
Executes the currently enabled operations on all the bases in the respective lists.
Definition f_manager.cpp:56
static void draw()
Draws the current fader.
Definition m_fader.cpp:8
A phase is a list of methods to be called in order.
Definition s_Phase.hpp:5
METHOD_RESULT_e
Return value of a phase method and callMethod().
Definition s_Phase.hpp:9
@ OK
Proceed to the next method in the phase.
Definition s_Phase.hpp:11
@ WAIT
Do not proceed to the next method in the phase.
Definition s_Phase.hpp:10
@ DONE
The phase is done.
Definition s_Phase.hpp:12
#define HEAP_SIZE_TEXTURE_BUFFER
The size of the heap used for texture buffers.
#define HEAP_SIZE_ARCHIVE
The size of the heap used for loading archives.
#define HEAP_SIZE_GAME1
The size of the first game heap.
#define ARENA_LO
The start of the arena.
#define ARENA_HI
The end of the arena.
#define HEAP_SIZE_EFFECT_MANAGER2
The size of the second heap used for the effect manager.
#define HEAP_SIZE_GAME2
The size of the second game heap.
#define HEAP_SIZE_REPLAY_DATA
The size of the heap used for replay data.
#define HEAP_SIZE_COMMAND
The size of the heap used for commands.
#define HEAP_SIZE_DYLINK
The size of the heap used for dynamic libraries.
#define HEAP_SIZE_MESSAGE_MANAGER
The size of the heap used for the message manager.
#define HEAP_SIZE_EFFECT_MANAGER1
The size of the first heap used for the effect manager.
#define HEAP_SIZE_FONT_MANAGER
The size of the heap used for the font manager.
void init()
Initializes the 2D engine.
Definition d_2d.cpp:11
EGG::FrmHeap * createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a frame heap.
Definition m_heap.cpp:75
@ GAME_HEAP_DEFAULT
The default game heap (alias of MEM1 or MEM2).
Definition m_heap.hpp:38
@ GAME_HEAP_MEM1
The game heap allocated in MEM1.
Definition m_heap.hpp:39
@ GAME_HEAP_MEM2
The game heap allocated in MEM2.
Definition m_heap.hpp:40
EGG::Heap * createGameHeap2(size_t size, EGG::Heap *parent)
Creates the MEM2 game heap. See createGameHeap().
Definition m_heap.cpp:207
EGG::ExpHeap * createExpHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates an expandable heap.
Definition m_heap.cpp:41
EGG::Heap * createGameHeap1(size_t size, EGG::Heap *parent)
Creates the MEM1 game heap. See createGameHeap().
Definition m_heap.cpp:203
EGG::Heap * createArchiveHeap(size_t size, EGG::Heap *parent)
Creates the archive heap. See createHeap().
Definition m_heap.cpp:211
EGG::Heap * createCommandHeap(size_t size, EGG::Heap *parent)
Creates the DVD command heap. See createHeap().
Definition m_heap.cpp:216
EGG::ExpHeap * g_commandHeap
The DVD command heap.
Definition m_heap.cpp:15
EGG::ExpHeap * g_archiveHeap
The archive resource heap.
Definition m_heap.cpp:14
@ OPT_THREAD_SAFE
Enables thread-safe memory block de/allocation.
Definition m_heap.hpp:32
@ OPT_NONE
No special allocation options.
Definition m_heap.hpp:29
EGG::Heap * createDylinkHeap(size_t size, EGG::Heap *parent)
Creates the REL linking heap. See createHeap().
Definition m_heap.cpp:221
EGG::ExpHeap * g_gameHeaps[GAME_HEAP_COUNT]
The game heaps.
Definition m_heap.cpp:13
EGG::Heap * setCurrentHeap(EGG::Heap *heap)
Sets the specified heap as the current heap.
Definition m_heap.cpp:37