persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
Environment.cpp
Go to the documentation of this file.
1#include "Environment.hpp"
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4#include <math.h>
5#include <nds.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string>
9
11{
12 for (int i = 0; i < MAX_ENVIRONMENT_TEXTURES; i++)
13 {
14 displayLists[i] = nullptr;
15 dlSizes[i] = 0;
16 textureIDs[i] = 0;
17 }
18}
19
21 std::array<const unsigned int*, MAX_ENVIRONMENT_TEXTURES> bitmaps)
22{
23 cleanup();
24
25 // Guard against a missing/oversized db entry before touching it
26 if (!entry || entry->textureCount > MAX_ENVIRONMENT_TEXTURES)
27 {
28 logMessage(LogSeverity::ERROR, "EnvironmentDbEntry textures exceeds MAX_ENVIRONMENT_TEXTURES or entry is null");
29 return false;
30 }
31
32 dbEntry = entry;
33
34 const std::string fullBinaryPath = fatBasePath + "environments/" + entry->name + "/" + entry->binaryFile;
35
37 {
38 logMessage(LogSeverity::DEBUG, "Opening '" + fullBinaryPath + "'");
39 }
40
41 FILE* file = fopen(fullBinaryPath.c_str(), "rb");
42 if (!file)
43 {
44 logMessage(LogSeverity::ERROR, "Failed to open environment file: " + fullBinaryPath);
45 cleanup();
46 return false;
47 }
48
49 char magic[4];
50 if (fread(magic, 1, 4, file) != 4)
51 {
52 logMessage(LogSeverity::ERROR, "Failed to read environment magic header from: " + fullBinaryPath);
53 fclose(file);
54 cleanup();
55 return false;
56 }
57
58 if (magic[0] != 'E' || magic[1] != 'N' || magic[2] != 'V' || magic[3] != '1')
59 {
60 logMessage(LogSeverity::ERROR, "Invalid environment magic header in: " + fullBinaryPath);
61 fclose(file);
62 cleanup();
63 return false;
64 }
65
66 u32 groupCount = 0;
67 if (fread(&groupCount, sizeof(u32), 1, file) != 1)
68 {
69 logMessage(LogSeverity::ERROR, "Failed to read groupCount from: " + fullBinaryPath);
70 fclose(file);
71 cleanup();
72 return false;
73 }
74
75 if (groupCount > (u32)entry->textureCount)
76 {
77 logMessage(LogSeverity::ERROR, "groupCount exceeds entry->textureCount in: " + fullBinaryPath);
78 fclose(file);
79 cleanup();
80 return false;
81 }
82
83 // Display list load
84 for (u32 i = 0; i < groupCount; i++)
85 {
86 if (fread(&dlSizes[i], sizeof(u32), 1, file) != 1)
87 {
88 logMessage(LogSeverity::ERROR, "Failed to read display list size in: " + fullBinaryPath);
89 fclose(file);
90 cleanup(); // frees any displayLists[0..i) already allocated this call
91 return false;
92 }
93
94 displayLists[i] = nullptr;
95
96 if (dlSizes[i] > 0)
97 {
98 displayLists[i] = (u32*)malloc((dlSizes[i] + 1) * sizeof(u32));
99
100 if (!displayLists[i])
101 {
102 logMessage(LogSeverity::ERROR, "Failed to allocate memory for environment display list");
103 fclose(file);
104 cleanup();
105 return false;
106 }
107
108 displayLists[i][0] = dlSizes[i];
109
110 if (fread(&displayLists[i][1], sizeof(u32), dlSizes[i], file) != dlSizes[i])
111 {
112 fclose(file);
113 cleanup();
114 return false;
115 }
116 }
117 }
118
119 fclose(file);
120
121 // Texture upload
122 for (int i = 0; i < entry->textureCount; i++)
123 {
124 textureIDs[i] = 0;
125
126 if (bitmaps.empty() || !bitmaps[i])
127 {
128 continue;
129 }
130
131 render.uploadTexture(textureIDs[i],
132 GL_RGBA,
133 entry->textures[i].width,
134 entry->textures[i].height,
135 TEXGEN_TEXCOORD | GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T,
136 bitmaps[i]);
137 }
138
139 logMessage(LogSeverity::DEBUG, "Environment loaded successfully: " + std::string(entry->name));
140
141 return true;
142}
143
145{
146 if (!dbEntry)
147 {
148 return;
149 }
150
151 for (int i = 0; i < dbEntry->textureCount; i++)
152 {
153 if (!textureIDs[i])
154 {
155 continue;
156 }
157
158 render.renderDisplayList(displayLists[i], textureIDs[i]);
159 }
160}
161
162void Environment::drawBillboards(bool faceCamera, ae::q20_12_t camX, ae::q20_12_t camY, ae::q20_12_t camZ)
163{
164 if (!dbEntry || dbEntry->billboardCount == 0)
165 {
166 return;
167 }
168
169 for (int i = 0; i < dbEntry->billboardCount; i++)
170 {
171 const auto& bb = dbEntry->billboards[i];
172 // Skip any billboard that references an out-of-range texture slot
173 if (bb.texSlot >= dbEntry->textureCount)
174 {
175 continue;
176 }
177 // Skip any billboard that references a texture slot that has no bound texture ID
178 if (!textureIDs[bb.texSlot])
179 {
180 continue;
181 }
182
183 render.renderTexturedBillboard(bb, textureIDs[bb.texSlot], faceCamera, camX, camY, camZ);
184 }
185}
186
188{
189 for (int i = 0; i < MAX_ENVIRONMENT_TEXTURES; i++)
190 {
191 if (displayLists[i])
192 {
193 free(displayLists[i]);
194 displayLists[i] = nullptr;
195 }
196
197 dlSizes[i] = 0;
198
199 if (textureIDs[i])
200 {
201 // Previously this only zeroed the id without ever releasing the
202 // underlying GPU texture slot, leaking VRAM texture memory on
203 // every single room transition.
204 render.deleteTexture(textureIDs[i]);
205 }
206 }
207
208 dbEntry = nullptr;
209}
constexpr int MAX_ENVIRONMENT_TEXTURES
int textureIDs[MAX_ENVIRONMENT_TEXTURES]
const EnvironmentDbEntry * dbEntry
void cleanup()
Frees all display lists and deletes all textures owned by this Environment, and clears the current da...
void drawBillboards(bool faceCamera, ae::q20_12_t camX, ae::q20_12_t camY, ae::q20_12_t camZ)
Renders every billboard quad defined for the currently loaded environment.
RenderManager & render
Environment()
Constructs an empty Environment with all texture and display-list slots cleared and no database entry...
void draw()
Renders every loaded texture/display-list pair for the currently loaded environment.
u32 * displayLists[MAX_ENVIRONMENT_TEXTURES]
u32 dlSizes[MAX_ENVIRONMENT_TEXTURES]
bool load(const EnvironmentDbEntry *entry, std::array< const unsigned int *, MAX_ENVIRONMENT_TEXTURES > bitmaps)
Loads environment geometry (display lists) and textures from the compiled .bin file described by an E...
static bool enableDebugPrint
Definition globals.hpp:43
Holds global variables used game-wide.
std::string fatBasePath
Definition main.cpp:38
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.
const EnvironmentTexture * textures