persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
TextManager.cpp
Go to the documentation of this file.
1#include "TextManager.hpp"
2#include "core/logger.hpp"
3#include <sstream>
4
5// Helper constexpr to convert 8 bit RGB values to 5 bit RGB values for the Nintendo DS
6constexpr uint16_t nds_rgb(uint8_t r, uint8_t g, uint8_t b)
7{
8 return static_cast<uint16_t>((r >> 3) | (((g) >> 3) << 5) | (((b) >> 3) << 10) | BIT(15));
9}
10
11static const uint16_t customPalette[21] = {
12 ARGB16(1, 0, 0, 0), // Black 235
13 ARGB16(1, 31, 31, 31), // White 236
14 nds_rgb(0, 202, 105), // Dual Green 237
15 nds_rgb(18, 168, 88), // Dual Green 2 238
16 nds_rgb(28, 118, 55), // Dark Green 239
17 nds_rgb(0, 69, 40), // Darker Green 240
18 nds_rgb(4, 34, 18), // Darkest Green 241
19 nds_rgb(121, 206, 255), // Light Blue 242
20 nds_rgb(9, 137, 253), // Rich Blue 243
21 nds_rgb(0, 104, 208), // Dark Blue 244
22 nds_rgb(0, 44, 208), // Navy Blue 245
23 nds_rgb(0, 0, 36), // Darkest Blue 246
24 nds_rgb(245, 198, 164), // Light Orange 247
25 nds_rgb(198, 164, 245), // Light Purple 248
26 //Defaults
27 ARGB16(1, 31, 0, 0), // Red 249
28 ARGB16(1, 0, 31, 0), // Green 250
29 ARGB16(1, 0, 0, 31), // Blue 251
30 ARGB16(1, 31, 31, 0), // Yellow 252
31 ARGB16(1, 31, 0, 31), // Magenta 253
32 ARGB16(1, 0, 31, 31), // Cyan 254
33 ARGB16(1, 15, 15, 15), // Gray 255
34};
35
36std::unique_ptr<Font> TextManager::loadFont(std::string* name, int size)
37{
38 if (name == nullptr)
39 {
40 logMessage(LogSeverity::FATAL, "name cannot be nullptr");
41 }
42
43 auto font = std::make_unique<Font>();
44 std::string fullPath = "fonts/" + *name + "/size-" + std::to_string(size);
45
46 std::string fontBitmapPath = fullPath + ".img.bin";
47 font->bitmap = loadFontBitmap(&fontBitmapPath);
48 if (!font->bitmap)
49 {
50 logMessage(LogSeverity::FATAL, "Failed to load font bitmap from \n" + fullPath + ".img.bin");
51 }
52
53 std::string fontMetadataPath = fullPath + ".fnt";
54 if (!loadFontMetadata(&fontMetadataPath, font.get()))
55 {
56 logMessage(LogSeverity::FATAL, "Failed to load font metadata from \n" + fullPath + ".fnt");
57 }
58
59 std::string fontBitmapBoldPath = fullPath + "-bold.img.bin";
60 std::string fontMetadataBoldPath = fullPath + "-bold.fnt";
61 font->bitmapBold = loadFontBitmap(&fontBitmapBoldPath);
62 if (!font->bitmapBold || !loadFontMetadata(&fontMetadataBoldPath, font.get(), true))
63 {
64 font->boldLoaded = false;
65 free(font->bitmapBold);
66 font->bitmapBold = nullptr;
67 }
68 else
69 {
70 font->boldLoaded = true;
71 }
72
73 return font;
74}
75
77{
78 dmaCopy(customPalette, BG_PALETTE + 235, 21 * sizeof(uint16_t));
79 dmaCopy(customPalette, BG_PALETTE_SUB + 235, 21 * sizeof(uint16_t));
80}
81
82bool TextManager::loadPalette(std::string* path, bool sub)
83{
84 if (path == nullptr)
85 {
86 logMessage(LogSeverity::FATAL, "path cannot be nullptr");
87 }
88
89 FileBuffer buffer = io.openFileBuffer(*path);
90 if (buffer.get() == nullptr)
91 {
92 logMessage(LogSeverity::WARNING, "Failed to load palette from: " + *path);
93 return false;
94 }
95
96 std::uint16_t* fontPalette = reinterpret_cast<std::uint16_t*>(buffer.get());
97 if (sub)
98 {
99 dmaCopy(fontPalette, BG_PALETTE_SUB, 256 * sizeof(uint16_t));
100 }
101 else
102 {
103 dmaCopy(fontPalette, BG_PALETTE, 256 * sizeof(uint16_t));
104 }
105
106 return true;
107}
108
110{
111 dmaFillHalfWords(0, BG_PALETTE, 256 * sizeof(uint16_t));
112 dmaFillHalfWords(0, BG_PALETTE_SUB, 256 * sizeof(uint16_t));
113}
114
115std::uint8_t* TextManager::loadFontBitmap(std::string* path)
116{
117 if (path == nullptr)
118 {
119 logMessage(LogSeverity::FATAL, "path cannot be nullptr");
120 }
121
122 FileBuffer buffer = io.openFileBuffer(*path);
123 if (buffer.get() == nullptr)
124 {
125 logMessage(LogSeverity::WARNING, "Failed to load font bitmap from: " + *path);
126 return nullptr;
127 }
128
129 return reinterpret_cast<std::uint8_t*>(buffer.release());
130}
131
132bool TextManager::loadFontMetadata(std::string* path, Font* font, bool forBoldBitmap)
133{
134 if (path == nullptr)
135 {
136 logMessage(LogSeverity::FATAL, "path cannot be nullptr");
137 }
138
139 if (font == nullptr)
140 {
141 logMessage(LogSeverity::ERROR, "Font cannot be nullptr");
142 return false;
143 }
144
145 FileBuffer buffer = io.openFileBuffer(*path);
146 if (buffer.get() == nullptr)
147 {
148 logMessage(LogSeverity::WARNING, "Failed to load font metadata from: " + *path);
149 return false;
150 }
151
152 std::string content(reinterpret_cast<char*>(buffer.get()), buffer.length());
153
154 std::istringstream iss(content);
155 std::string line;
156 while (std::getline(iss, line))
157 {
158 if (line.starts_with("common ") && !forBoldBitmap)
159 {
160 font->lineHeight = extractIntValue(line, "lineHeight=");
161 font->bitmapWidth = extractIntValue(line, "scaleW=");
162 font->bitmapHeight = extractIntValue(line, "scaleH=");
163 continue;
164 }
165
166 //if there's no character in this line, move to the next
167 if (!line.starts_with("char "))
168 {
169 continue;
170 }
171
172 const int charID = extractIntValue(line, "id=");
173 if (charID < 0 || charID >= 256)
174 {
175 continue;
176 }
177
178 Glyph glyph{};
179 glyph.xPos = extractIntValue(line, "x=");
180 glyph.yPos = extractIntValue(line, "y=");
181 glyph.width = extractIntValue(line, "width=");
182 glyph.height = extractIntValue(line, "height=");
183 glyph.xOffset = extractIntValue(line, "xoffset=");
184 glyph.yOffset = extractIntValue(line, "yoffset=");
185 if (forBoldBitmap)
186 {
187 font->boldGlyphs[charID] = glyph;
188 }
189 else
190 {
191 font->glyphs[charID] = glyph;
192 }
193 }
194
195 return true;
196}
197
198void TextManager::clearScreen(uint16_t* videoBuffer)
199{
200 if (videoBuffer == nullptr)
201 {
202 logMessage(LogSeverity::WARNING, "Called with null videoBuffer");
203 return;
204 }
205 dmaFillHalfWords(0, videoBuffer, 256 * 256 * sizeof(uint8_t));
206}
207
208int TextManager::extractIntValue(const std::string& line, const std::string& key)
209{
210 std::size_t keyPos = line.find(key);
211 if (keyPos == std::string::npos)
212 {
213 return 0;
214 }
215
216 const std::size_t dataStart = keyPos + key.size();
217 if (dataStart >= line.size())
218 {
219 return 0;
220 }
221
222 char* endPtr = nullptr;
223 long val = std::strtol(line.c_str() + dataStart, &endPtr, 10);
224 if (endPtr == line.c_str() + dataStart)
225 {
226 logMessage(LogSeverity::WARNING, "Failed to parse int value for key: " + key);
227 return 0;
228 }
229
230 return static_cast<int>(val);
231}
static const uint16_t customPalette[21]
constexpr uint16_t nds_rgb(uint8_t r, uint8_t g, uint8_t b)
Manager for text loading.
Owns a heap-allocated file buffer and its length.
Definition IOManager.hpp:23
void * release()
Releases the file data to the caller.
void * get() const
Gets the owned file data without transferring ownership.
Definition IOManager.hpp:89
u32 length() const
Gets the file data length.
Definition IOManager.hpp:98
IOManager & io
void unloadPalette()
Unload the current palettes.
int extractIntValue(const std::string &line, const std::string &key)
Extract an integer value from a line of text based on a specified key.
void loadDefaultPalette()
Loads the predefined default palette.
bool loadPalette(std::string *paletteFilePath, bool sub=false)
Load a palette from a file.
std::unique_ptr< Font > loadFont(std::string *name, int size)
Load a font from a file.
void clearScreen(uint16_t *videoBuffer)
Clear the text layer by filling the video buffer with black.
bool loadFontMetadata(std::string *path, Font *font, bool forBoldBitmap=false)
Load the font metadata from a file.
std::uint8_t * loadFontBitmap(std::string *path)
Load the font bitmap from a file.
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.
Stores data for a font.
Definition TextTypes.hpp:69
Glyph glyphs[256]
Definition TextTypes.hpp:89
Glyph boldGlyphs[256]
Definition TextTypes.hpp:90
int bitmapHeight
Definition TextTypes.hpp:87
int lineHeight
Definition TextTypes.hpp:88
int bitmapWidth
Definition TextTypes.hpp:86
Stores data for a single glyph (character) in a font.
Definition TextTypes.hpp:55
int xPos
Definition TextTypes.hpp:56
int yPos
Definition TextTypes.hpp:57
int height
Used to check if the glyph was read in correctly. Setting it to 0 here wipes any old data.
Definition TextTypes.hpp:59
int width
Definition TextTypes.hpp:58
int yOffset
Definition TextTypes.hpp:61
int xOffset
Definition TextTypes.hpp:60