A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
GameScene.cc
Go to the documentation of this file.
1#include "GameScene.hh"
2
4
7
9
10namespace Kinoko::Scene {
11
14 m_heap->setName("DefaultGameSceneHeap");
15 m_nextSceneId = -1;
16
17#ifdef BUILD_DEBUG
20 ASSERT(heap);
21
23 for (u16 groupID = 0; groupID < m_groupSizeRecord.size(); ++groupID) {
25 }
26#endif // BUILD_DEBUG
27}
28
31 m_resources.clear();
32
33#ifdef BUILD_DEBUG
35 ASSERT(heap);
36
38 size_t sum = 0;
39 for (u16 groupID = 0; groupID < m_groupSizeRecord.size(); ++groupID) {
40 sum += m_groupSizeRecord.getGroupSize(groupID);
41 }
42
43 if (sum > m_totalMemoryUsed) {
44 WARN("MEMORY LEAK DETECTED: %zu bytes", sum - m_totalMemoryUsed);
45 getMemoryLeakTags();
46 }
47#endif // BUILD_DEBUG
48}
49
56
59 configure();
60 initScene();
61}
62
68
71 exit();
72 if (m_nextSceneId < 0) {
73 onReinit();
74 initScene();
75 } else {
77 }
78}
79
84
89
92 m_resources.push_back(EGG::egg_new<Resource>(archive, id));
93}
94
98
103 initEngines();
104#ifdef BUILD_DEBUG
105 checkMemory();
106#endif // BUILD_DEBUG
107}
108
111 if (m_nextSceneId >= 0) {
112 return;
113 }
114
117}
118
121 auto *resourceManager = System::ResourceManager::Instance();
122 for (auto iter = m_resources.begin(); iter != m_resources.end();) {
123 Resource *resource = *iter;
124 resourceManager->unmount(resource->archive);
125 iter = m_resources.erase(iter);
126 EGG::egg_delete(resource);
127 }
128}
129
130#ifdef BUILD_DEBUG
131void GameScene::checkMemory() {
133 ASSERT(heap);
134
135 heap->calcGroupSize(&m_groupSizeRecord);
136 size_t defaultSize = m_groupSizeRecord.getGroupSize(static_cast<u16>(GroupID::None));
137
138 // Because we're working with a class that can be inherited (but not doubly-inherited),
139 // it's possible that derived classes can have a different size compared to GameScene
140 // We don't need to know the exact derived class, though, as we have the memory block head
141 // The scene is the first, always group ID 0 allocation to happen in the scene's heap
144 SubOffset(this, sizeof(Abstract::Memory::MEMiExpBlockHead)));
145 size_t initialAllocSize = blockHead->m_size;
146 ASSERT(defaultSize >= initialAllocSize);
147
148 defaultSize -= initialAllocSize;
149 if (defaultSize > 0) {
150 WARN("Default memory usage found! %zu bytes", defaultSize);
151 }
152
153 for (u16 groupID = 1; groupID < m_groupSizeRecord.size(); ++groupID) {
154 size_t size = m_groupSizeRecord.getGroupSize(groupID);
155 if (size == 0) {
156 continue;
157 }
158
159 DEBUG("Group ID %d: Allocated %zu bytes", groupID, size);
160 }
161}
162
163void GameScene::getMemoryLeakTags() {
164 size_t count = getMemoryLeakTagCount();
165 if (count == 0) {
166 return;
167 }
168
169 owning_span<u32> tags = owning_span<u32>(count);
170 for (auto &tag : tags) {
171 tag = 0; // empty tag
172 }
173
175 GetAddrNum(&tags));
176
177 DEBUG("TAGGED MEMORY BLOCKS:");
178 printf("[%d", tags[0]);
179 for (u32 i = 1; i < tags.size(); ++i) {
180 printf(", %d", tags[i]);
181 }
182 printf("]\n");
183}
184
185size_t GameScene::getMemoryLeakTagCount() {
186 size_t count = 0;
188 GetAddrNum(&count));
189 return count;
190}
191
192void GameScene::ViewTags(void *block, Abstract::Memory::MEMiHeapHead * /*heap*/, uintptr_t param) {
193 Abstract::Memory::MEMiExpBlockHead *blockHead =
194 static_cast<Abstract::Memory::MEMiExpBlockHead *>(
195 SubOffset(block, sizeof(Abstract::Memory::MEMiExpBlockHead)));
196 owning_span<u32> *span = reinterpret_cast<owning_span<u32> *>(param);
197 for (auto &tag : *span) {
198 if (tag == 0) {
199 tag = blockHead->m_tag;
200 break;
201 }
202 }
203}
204
205void GameScene::IncreaseTagCount(void * /*block*/, Abstract::Memory::MEMiHeapHead * /*heap*/,
206 uintptr_t param) {
207 size_t &count = *reinterpret_cast<size_t *>(param);
208 ++count;
209}
210#endif // BUILD_DEBUG
211
212} // namespace Kinoko::Scene
#define DEBUG(m,...)
Definition Logger.hh:49
#define ASSERT(e)
Definition Logger.hh:25
#define WARN(m,...)
Definition Logger.hh:33
void visitAllocated(Visitor visitor, uintptr_t param)
High-level implementation of a memory heap for managing dynamic memory allocation....
Abstract::Memory::MEMiExpHeapHead * dynamicCastHandleToExp()
void calcGroupSize(GroupSizeRecord *record)
static ExpHeap * dynamicCastToExp(Heap *heap)
Definition Heap.hh:107
void setName(const char *name)
Definition Heap.hh:92
void changeSiblingScene(int nextSceneId)
SceneManager * m_sceneMgr
Definition Scene.hh:85
Heap * heap() const
Definition Scene.hh:55
static KartCamera * Instance()
Definition KartCamera.hh:63
EGG::ExpHeap::GroupSizeRecord m_groupSizeRecord
Definition GameScene.hh:60
virtual void onReinit()
Definition GameScene.hh:30
virtual void createEngines()=0
void appendResource(System::MultiDvdArchive *archive, s32 id)
Definition GameScene.cc:91
static void initCamera()
Definition GameScene.cc:81
virtual void destroyEngines()=0
static void calcCamera()
Definition GameScene.cc:86
virtual void calcEngines()=0
std::list< Resource *, EGG::Allocator< Resource * > > m_resources
List of all active resources in the scene.
Definition GameScene.hh:62
virtual void initEngines()=0
virtual void configure()=0
static KPadDirector * Instance()
static ResourceManager * Instance()
owning_span()
Uninitialized buffer.
Definition Types.hh:35
void egg_delete(const T *ptr)
Definition Heap.hh:154
T * egg_new(Args &&...args)
Definition Heap.hh:148
Pertains to scene handling.
Definition GameScene.cc:10
uint16_t u16
Definition Memory.hh:17
int32_t s32
Definition Memory.hh:13
uint32_t u32
Definition Memory.hh:18
Head of the memory block. Contains information about the block and a link in the corresponding used/f...
System::MultiDvdArchive * archive
Definition GameScene.hh:42
Resource(System::MultiDvdArchive *archive, s32 id)
Definition GameScene.cc:96