From c17cc85aed68dc5607fab81c3ac5b0af4c56b1ac Mon Sep 17 00:00:00 2001 From: niansa Date: Fri, 7 Apr 2023 20:49:51 +0200 Subject: [PATCH] Make sure enough characters can be rendered using pixel budget --- NDSUI.cpp | 50 ++++++++++++++++++++++++++++---------------------- NDSUI.hpp | 6 +++--- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/NDSUI.cpp b/NDSUI.cpp index 1b6710d..e0ae4ba 100644 --- a/NDSUI.cpp +++ b/NDSUI.cpp @@ -7,7 +7,7 @@ #include #include -#define SCREEN_BORDER_PADDING 4 +#define SCREEN_BORDER_PADDING 8 #define FONT_PADDING 1 #define INPUT_LINE_HEIGHT (FONT_HEIGHT+FONT_PADDING) @@ -18,7 +18,7 @@ NDSUI *NDSUI::currentInstance; consteval unsigned NDSUI::calcMaxCharsInLine(unsigned int width) { - return width / (FONT_WIDTH + FONT_PADDING); + return width / (FONT_WIDTH + FONT_PADDING) - 1; } consteval unsigned NDSUI::calcMaxLinesInRow(unsigned height) { @@ -44,8 +44,17 @@ void NDSUI::renderMessageLog() { // Log messages renderer auto renderLine = [this, y = int(boxDims.bottomRight.y - FONT_PADDING - FONT_HEIGHT), boxDims] (std::string_view line) mutable { - print(line, {boxDims.topLeft.x + FONT_PADDING, unsigned(y)}, RGB15(28, 28, 28)); - y -= FONT_PADDING + FONT_HEIGHT; + // Skip empty lines + if (line.empty()) return; + // Check if line is bold + bool bold = line[0] == '\t'; + if (bold) { + line = {line.data()+1, line.size()-1}; + } + // Print line itself + print(line, {boxDims.topLeft.x + FONT_PADDING, unsigned(y)}, RGB15(28, 28, 28), bold); + // Go to next line + y -= FONT_PADDING * 3 + FONT_HEIGHT; if (y < 0) { throw std::exception(); // Stop rendering any more lines } @@ -89,11 +98,12 @@ void NDSUI::renderInputLine() { print(&inputLineBuf[startIdx], textTopLeft, RGB15(28, 28, 28)); } -bool NDSUI::printChar(const char character, Position2D *topleftpos, const int color) { +bool NDSUI::printChar(const char character, Position2D *topleftpos, const int color, bool bold) { const bool *fontChar = font_get_character(character); int x = 0; int y = 0; - unsigned no = 0; + unsigned row = 0; + unsigned x_start = 0; for (const bool *thispixel = fontChar; ; thispixel++) { if (x == FONT_WIDTH) { x = 0; @@ -101,27 +111,23 @@ bool NDSUI::printChar(const char character, Position2D *topleftpos, const int co } if (y == FONT_HEIGHT) { break; } if (*thispixel) { - // Calculate spareout - unsigned spareout = font_pixels / 500; - // Skip pixel for spareout - if (!(no++ % spareout)) { - // Draw pixel with melonDS fallback for debug builds - # ifdef NDEBUG - glPutPixel(topleftpos->x + x, topleftpos->y + y, color); - # else - glBoxFilled(topleftpos->x + x, topleftpos->y + y, topleftpos->x + x, topleftpos->y + y, color); - # endif - font_pixels++; + if (row == 0) x_start = x; + row++; + } else if (row) { + if (currentFontBoxCount < 125 || row > 2 || (y % 3)) { + glBoxFilled(topleftpos->x + x_start, topleftpos->y + y, topleftpos->x + x - (!bold)*1, topleftpos->y + y, color); + currentFontBoxCount++; } + row = 0; } - x++; + ++x; } return fontChar != fontBitmapUnk; } -void NDSUI::print(std::string_view str, Position2D topleftpos, const int color) { +void NDSUI::print(std::string_view str, Position2D topleftpos, const int color, bool bold) { for (const char c : str) { - printChar(c, &topleftpos, color); + printChar(c, &topleftpos, color, bold); topleftpos.x += FONT_WIDTH + FONT_PADDING; } } @@ -164,7 +170,7 @@ NDSUI::NDSUI() { void NDSUI::run() { currentInstance = this; - font_pixels = 0; + currentFontBoxCount = 0; // Update keyboard keyboardUpdate(); // Draw @@ -179,7 +185,7 @@ void NDSUI::run() { LineWrappedString NDSUI::createLogMessage(const char *username, std::string_view message) { // Do line-wrapping, create and return Message instance - return LineWrappedString(" "+std::string(username)+"\n"+std::string(message)+"\n", calcMaxCharsInLine(calcMessageLogDimensions().width())); + return LineWrappedString("\t "+std::string(username)+"\n"+std::string(message)+"\n", calcMaxCharsInLine(calcMessageLogDimensions().width())); } void LineWrappedString::split(std::string_view str, unsigned maxCharsPerLine) { diff --git a/NDSUI.hpp b/NDSUI.hpp index e42e9c4..0e571de 100644 --- a/NDSUI.hpp +++ b/NDSUI.hpp @@ -61,7 +61,7 @@ class NDSUI { unsigned inputLineBufHead = 0; std::vector messages; std::unique_ptr> userInputHandler = nullptr; - size_t font_pixels; + unsigned currentFontBoxCount; static void kbCallback(int key); @@ -78,8 +78,8 @@ class NDSUI { static consteval unsigned calcMaxLinesInRow(unsigned height); - bool printChar(const char character, Position2D *topleftpos, const int color); - void print(std::string_view str, Position2D topleftpos, const int color); + bool printChar(const char character, Position2D *topleftpos, const int color, bool bold = false); + void print(std::string_view str, Position2D topleftpos, const int color, bool bold = false); void handleInput(char);