persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
TextSystem.cpp
Go to the documentation of this file.
1#include "TextSystem.hpp"
2#include "TextLayout.hpp"
3#include "core/logger.hpp"
4
5#include <algorithm>
6
7void TextSystem::drawText(const std::string& text,
8 Font* font,
9 uint16_t* videoBuffer,
10 int startX,
11 int startY,
12 int color,
13 int letterSpacing,
14 int lineSpacing,
15 int spaceWidth,
16 int xBoundary)
17{
18 if (!font || !videoBuffer)
19 {
20 logMessage(LogSeverity::WARNING, "Called with null font or videoBuffer");
21 return;
22 }
23
24 auto textObj =
25 createText(text, font, videoBuffer, startX, startY, color, letterSpacing, lineSpacing, spaceWidth, xBoundary);
26
27 while (textObj->cursorPos < (int)textObj->content.size())
28 {
29 drawNextFromText(textObj.get());
30 textObj->cursorPos++;
31 }
32}
33
34void TextSystem::appearText(std::unique_ptr<Text>& appearingText,
35 const std::string& content,
36 Font* font,
37 uint16_t* videoBuffer,
38 int startX,
39 int startY,
40 int color,
41 int letterSpacing,
42 int lineSpacing,
43 int spaceWidth,
44 int maxX,
45 int maxY,
46 bool hyphenate)
47{
48 if (!font || !videoBuffer)
49 {
50 logMessage(LogSeverity::WARNING, "Called with null font or videoBuffer");
51 return;
52 }
53
54 if (appearingText != nullptr)
55 {
56 appearingText.reset();
57 }
58 appearingText = createText(
59 content, font, videoBuffer, startX, startY, color, letterSpacing, lineSpacing, spaceWidth, maxX, maxY);
60 appearingText->hyphenate = hyphenate;
61}
62
63void TextSystem::appearTextSkip(Text* appearingText)
64{
65 if (appearingText != nullptr)
66 {
67 while (appearingText->cursorPos < (int)appearingText->content.size() && !appearingText->pageBreak)
68 {
69 drawNextFromText(appearingText);
70 if (!appearingText->pageBreak)
71 {
72 appearingText->cursorPos++;
73 }
74 }
75 }
76}
77
79{
80 return appearingText != nullptr && appearingText->pageBreak;
81}
82
84{
85 if (!appearTextPaused(appearingText))
86 {
87 return;
88 }
89
90 clearArea(appearingText->videoBuffer,
91 appearingText->startX,
92 appearingText->startY,
93 appearingText->maxX - appearingText->startX,
94 appearingText->maxY - appearingText->startY);
95 appearingText->cursorX = appearingText->startX;
96 appearingText->cursorY = appearingText->startY;
97 appearingText->counter = 0;
98 appearingText->pageBreak = false;
99}
100
102{
103 if (appearingText == nullptr)
104 {
105 return true;
106 }
107 if (appearingText->cursorPos >=
108 (int)appearingText->content.size()) // not really sure if this is needed but it should be safe to check anyway
109 {
110 return true;
111 }
112 return false;
113}
114
115void TextSystem::drawGlyph(const Glyph& glyph,
116 Font* font,
117 uint16_t* videoBuffer,
118 int cursorX,
119 int cursorY,
120 int color,
121 bool bold,
122 bool italic,
123 bool underline)
124{
125 if (!font || !videoBuffer)
126 {
127 logMessage(LogSeverity::WARNING, "Called with null font or videoBuffer");
128 return;
129 }
130
131 for (int y = 0; y < glyph.height; y++)
132 {
133 int distY = italic ? glyph.height - 1 - y : 0;
134 int italicOffset =
135 italic ? (distY * SLANT_FACTOR) >> 8 : 0;
136 for (int x = 0; x < glyph.width; x++)
137 {
138 int bitmapX = glyph.xPos + x;
139 int bitmapY = glyph.yPos + y;
140 int bitmapIndex = bitmapY * font->bitmapWidth + bitmapX;
141
142 if (bitmapIndex >= font->bitmapWidth * font->bitmapHeight)
143 {
144 logMessage(LogSeverity::ERROR, "Bitmap index out of bounds");
145 continue;
146 }
147
148 int pixelValue = bold ? font->bitmapBold[bitmapIndex] : font->bitmap[bitmapIndex];
149 if (pixelValue > 0)
150 {
151 int screenX = cursorX + x + italicOffset;
152 int screenY = cursorY + glyph.yOffset + y;
153 if (screenX >= 0 && screenX < 256 && screenY >= 0 && screenY < 192)
154 {
155 drawPixel(videoBuffer, screenX, screenY, color);
156 }
157 }
158 }
159 }
160 if (underline)
161 {
162 int underlineY = cursorY + font->lineHeight - 2;
163 for (int x = 0; x < glyph.width; x++)
164 {
165 int screenX = cursorX + x;
166 if (screenX >= 0 && screenX < 256 && underlineY >= 0 && underlineY < 192)
167 {
168 drawPixel(videoBuffer, screenX, underlineY, color);
169 }
170 }
171 }
172}
173
174void TextSystem::clearArea(uint16_t* videoBuffer, int x, int y, int width, int height)
175{
176 if (!videoBuffer)
177 {
178 logMessage(LogSeverity::WARNING, "Called with null videoBuffer");
179 return;
180 }
181
182 for (int row = 0; row < height; ++row)
183 {
184 int rowStart = x;
185 int rowEnd = x + width;
186 int pixelY = y + row;
187
188 if (pixelY < 0 || pixelY >= 192)
189 {
190 continue;
191 }
192
193 rowStart = std::max(rowStart, 0);
194 rowEnd = std::min(rowEnd, 256);
195 if (rowStart < rowEnd)
196 {
197 // Clear unaligned edge pixels individually, then fill the aligned span with DMA.
198 if (rowStart % 2 != 0)
199 {
200 drawPixel(videoBuffer, rowStart, pixelY, TextColor::Transparent);
201 ++rowStart;
202 }
203 if (rowEnd % 2 != 0)
204 {
205 --rowEnd;
206 drawPixel(videoBuffer, rowEnd, pixelY, TextColor::Transparent);
207 }
208
209 dmaFillHalfWords(0, videoBuffer + (pixelY * 256 + rowStart) / 2, static_cast<uint32_t>(rowEnd - rowStart));
210 }
211 }
212}
213
215{
216 unsigned char c = text->content[text->cursorPos];
217
219 if (c == '\n')
220 {
221 text->cursorX = text->startX;
222 text->cursorY += text->font->lineHeight + text->lineSpacing;
223 }
224 else if (c == ' ')
225 {
226 if (text->cursorX == text->startX)
227 {
228 return;
229 }
231 {
232 text->cursorX = text->startX;
233 text->cursorY += text->font->lineHeight + text->lineSpacing;
234 }
235 else
236 {
237 if (text->underline)
238 {
239 underlineGap(text->cursorX,
240 text->cursorY + text->font->lineHeight - 2,
241 text->spaceWidth,
242 text->videoBuffer,
243 text->activeColor);
244 }
245 text->cursorX += text->spaceWidth;
246 }
247 }
248 else if (c == TextInstructionMarker)
249 {
250 c = getNextChar(text);
252 {
253 c = getNextChar(text);
254 if (c == TextInstruction::Reset)
255 {
256 text->activeColor = text->baseColor;
257 }
258 else if (c < 256)
259 {
260 text->activeColor = static_cast<int>(c);
261 }
262 }
263 else if (c == TextInstruction::StyleChange)
264 {
265 c = getNextChar(text);
266 TextLayout::applyStyle(*text->font, c, text->bold, text->italic, text->underline);
267 }
268 }
269 else if (text->font->glyphs[c].width == 0)
270 {
271 text->cursorX += text->spaceWidth;
272 }
273 else
274 {
276 if (TextLayout::pageFull(*text))
277 {
278 return;
279 }
280
281 Glyph g = text->bold ? text->font->boldGlyphs[c] : text->font->glyphs[c];
282
283 const int oldX = text->cursorX;
284 const int oldY = text->cursorY;
285 if (TextLayout::prepareGlyph(*text, g))
286 {
287 const Glyph& hyphen = text->bold ? text->font->boldGlyphs['-'] : text->font->glyphs['-'];
288 drawGlyph(hyphen,
289 text->font,
290 text->videoBuffer,
291 oldX,
292 oldY,
293 text->activeColor,
294 text->bold,
295 text->italic,
296 text->underline);
297 }
298 if (text->pageBreak)
299 {
300 return;
301 }
302
303 drawGlyph(g,
304 text->font,
305 text->videoBuffer,
306 text->cursorX,
307 text->cursorY,
308 text->activeColor,
309 text->bold,
310 text->italic,
311 text->underline);
312 if (text->underline)
313 {
314 underlineGap(text->cursorX + g.width,
315 text->cursorY + text->font->lineHeight - 2,
316 text->letterSpacing,
317 text->videoBuffer,
318 text->activeColor);
319 }
320 text->cursorX += g.width + text->letterSpacing;
321 }
322}
323
324std::unique_ptr<Text> TextSystem::createText(const std::string& text,
325 Font* font,
326 uint16_t* videoBuffer,
327 int startX,
328 int startY,
329 int color,
330 int letterSpacing,
331 int lineSpacing,
332 int spaceWidth,
333 int maxX,
334 int maxY)
335{
336 auto newText = std::make_unique<Text>();
337 newText->cursorX = startX;
338 newText->cursorY = startY;
339 newText->startX = startX;
340 newText->startY = startY;
341 newText->content = text;
342 newText->baseColor = color;
343 newText->activeColor = color;
344 newText->font = font;
345 newText->videoBuffer = videoBuffer;
346 newText->cursorPos = 0; // Start at the beginning of the text
347 newText->bold = false;
348 newText->italic = false;
349 newText->underline = false;
350 newText->letterSpacing = letterSpacing;
351 newText->lineSpacing = lineSpacing;
352 newText->spaceWidth = spaceWidth;
353 newText->maxX = maxX;
354 newText->maxY = maxY;
355 newText->pageBreak = false;
356 return newText;
357}
358
360{
361 if (text->cursorPos + 1 < (int)text->content.size())
362 {
363 return text->content[++text->cursorPos];
364 }
365 return 0xFE;
366}
367
368void TextSystem::drawPixel(uint16_t* videoBuffer, int x, int y, int paletteValue)
369{
370 int wordIndex = (y * 256 + x) / 2;
371 u16 currentWord = videoBuffer[wordIndex];
372 if (x % 2 == 0) //Clear the lower 8 bits, then inject our 8-bit color index
373 {
374 videoBuffer[wordIndex] = (currentWord & 0xFF00) | (paletteValue & 0xFF);
375 }
376 else //Clear the upper 8 bits, then inject our 8-bit color index shifted up
377 {
378 videoBuffer[wordIndex] = (currentWord & 0x00FF) | ((paletteValue & 0xFF) << 8);
379 }
380}
381
382void TextSystem::underlineGap(int startX, int y, int width, uint16_t* videoBuffer, int color)
383{
384 for (int x = 0; x < width; x++)
385 {
386 int screenX = startX + x;
387 if (screenX >= 0 && screenX < 256 && y >= 0 && y < 192)
388 {
389 drawPixel(videoBuffer, screenX, y, color);
390 }
391 }
392}
393
394void TextSystem::testBitmap(Font* font, uint16_t* videoBuffer)
395{
396 for (int y = 0; y < 256; y++)
397 {
398 for (int x = 0; x < 256; x++)
399 {
400 int index = font->bitmap[y * font->bitmapWidth + x];
401 int pixelValue = font->bitmap[index];
402 if (pixelValue > 0)
403 {
404 drawPixel(videoBuffer, x, y, DualGreen);
405 }
406 }
407 }
408}
409
410void TextSystem::testPalette(uint16_t* videoBuffer)
411{
412 for (int i = 0; i < 128; i += 2)
413 {
414 for (int y = 0; y < 50; y++)
415 {
416 drawPixel(videoBuffer, i, y, i);
417 drawPixel(videoBuffer, i + 1, y, i);
418 }
419 }
420 for (int i = 0; i < 128; i += 2)
421 {
422 for (int y = 0; y < 50; y++)
423 {
424 drawPixel(videoBuffer, i, y + 50, i + 128);
425 drawPixel(videoBuffer, i + 1, y + 50, i + 128);
426 }
427 }
428}
Provides text layout utilities for measuring and positioning text.
Manages rendering text from a bitmap.
@ StyleChange
Definition TextTypes.hpp:42
@ Reset
Definition TextTypes.hpp:46
@ ColorChange
Definition TextTypes.hpp:41
constexpr unsigned char TextInstructionMarker
Definition TextTypes.hpp:49
@ DualGreen
Definition TextTypes.hpp:15
@ Transparent
Definition TextTypes.hpp:12
char getNextChar(Text *text)
Get the next character from a given Text object.
bool appearTextDone(Text *appearingText)
Check if the text being rendered with appearText has finished appearing.
void clearArea(uint16_t *videoBuffer, int x, int y, int width, int height)
Clear a rectangular area of the text video buffer.
void underlineGap(int startX, int y, int width, uint16_t *videoBuffer, int color)
Underline a specificed area.
void drawText(const std::string &text, Font *font, uint16_t *videoBuffer, int x, int y, int color, int letterSpacing, int lineSpacing, int spaceWidth, int xBoundary)
Draw text to the screen.
Definition TextSystem.cpp:7
void drawPixel(uint16_t *videoBuffer, int x, int y, int paletteValue)
Draw a single pixel to the video buffer.
void appearTextNextPage(Text *appearingText)
Clear the text area and continue the paused text from the top of it.
void appearText(std::unique_ptr< Text > &appearingText, const std::string &text, Font *font, uint16_t *videoBuffer, int x, int y, int color, int letterSpacing, int lineSpacing, int spaceWidth, int maxX=256, int maxY=0, bool hyphenate=false)
Create a Text object and renders each character with a delay to simulate typing effect.
std::unique_ptr< Text > createText(const std::string &text, Font *font, uint16_t *videoBuffer, int startX, int startY, int color, int letterSpacing, int lineSpacing, int spaceWidth, int maxX=256, int maxY=0)
Create a Text object and initialize its properties.
void testPalette(uint16_t *videoBuffer)
Test function to draw the currently loaded palette to the screen.
void drawGlyph(const Glyph &glyph, Font *font, uint16_t *videoBuffer, int x, int y, int color, bool bold, bool italic, bool underline)
Draw a single glyph to the screen.
void appearTextSkip(Text *appearingText)
If a text is currently being rendered with appearText, this function will immediately render the rest...
void testBitmap(Font *font, uint16_t *videoBuffer)
Test function to draw the font bitmap to the screen.
bool appearTextPaused(Text *appearingText)
Check if the text being rendered with appearText is waiting for the next page.
int SLANT_FACTOR
128 = 1 pixel shift every 2 rows, 64 = 1 pixel shift every 4 rows
void drawNextFromText(Text *text)
Draw the next character from the text struct to the screen.
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.
bool pageFull(Text &text)
void applyStyle(Font &font, unsigned char flags, bool &bold, bool &italic, bool &underline)
bool prepareGlyph(Text &text, const Glyph &glyph)
bool shouldWrapWord(const Text &text)
Stores data for a font.
Definition TextTypes.hpp:69
Glyph glyphs[256]
Definition TextTypes.hpp:89
std::uint8_t * bitmapBold
Definition TextTypes.hpp:85
std::uint8_t * bitmap
Definition TextTypes.hpp:84
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
A struct that represents a block of text being rendered on the screen.
Definition TextTypes.hpp:98
int startX
int letterSpacing
int cursorX
Definition TextTypes.hpp:99
bool underline
bool bold
uint16_t * videoBuffer
int maxY
Bottom edge used for pagination. 0 disables pagination.
bool pageBreak
Set when the next glyph would not fit above maxY. Output pauses until the next page is requested.
int maxX
Right edge (exclusive) used for word wrapping.
int baseColor
int cursorPos
std::string content
int lineSpacing
int spaceWidth
int cursorY
int activeColor
int counter
Font * font
bool italic
int startY