1
0
Fork 0
mirror of https://gitlab.com/niansa/llama_nds.git synced 2025-03-06 20:53:28 +01:00

Make sure enough characters can be rendered using pixel budget

This commit is contained in:
niansa 2023-04-07 20:49:51 +02:00
parent 42723347f1
commit c17cc85aed
2 changed files with 31 additions and 25 deletions

View file

@ -7,7 +7,7 @@
#include <nds/touch.h> #include <nds/touch.h>
#include <nds/input.h> #include <nds/input.h>
#define SCREEN_BORDER_PADDING 4 #define SCREEN_BORDER_PADDING 8
#define FONT_PADDING 1 #define FONT_PADDING 1
#define INPUT_LINE_HEIGHT (FONT_HEIGHT+FONT_PADDING) #define INPUT_LINE_HEIGHT (FONT_HEIGHT+FONT_PADDING)
@ -18,7 +18,7 @@ NDSUI *NDSUI::currentInstance;
consteval consteval
unsigned NDSUI::calcMaxCharsInLine(unsigned int width) { unsigned NDSUI::calcMaxCharsInLine(unsigned int width) {
return width / (FONT_WIDTH + FONT_PADDING); return width / (FONT_WIDTH + FONT_PADDING) - 1;
} }
consteval consteval
unsigned NDSUI::calcMaxLinesInRow(unsigned height) { unsigned NDSUI::calcMaxLinesInRow(unsigned height) {
@ -44,8 +44,17 @@ void NDSUI::renderMessageLog() {
// Log messages renderer // Log messages renderer
auto renderLine = [this, y = int(boxDims.bottomRight.y - FONT_PADDING - FONT_HEIGHT), boxDims] (std::string_view line) mutable { 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)); // Skip empty lines
y -= FONT_PADDING + FONT_HEIGHT; 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) { if (y < 0) {
throw std::exception(); // Stop rendering any more lines throw std::exception(); // Stop rendering any more lines
} }
@ -89,11 +98,12 @@ void NDSUI::renderInputLine() {
print(&inputLineBuf[startIdx], textTopLeft, RGB15(28, 28, 28)); 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); const bool *fontChar = font_get_character(character);
int x = 0; int x = 0;
int y = 0; int y = 0;
unsigned no = 0; unsigned row = 0;
unsigned x_start = 0;
for (const bool *thispixel = fontChar; ; thispixel++) { for (const bool *thispixel = fontChar; ; thispixel++) {
if (x == FONT_WIDTH) { if (x == FONT_WIDTH) {
x = 0; x = 0;
@ -101,27 +111,23 @@ bool NDSUI::printChar(const char character, Position2D *topleftpos, const int co
} if (y == FONT_HEIGHT) { } if (y == FONT_HEIGHT) {
break; break;
} if (*thispixel) { } if (*thispixel) {
// Calculate spareout if (row == 0) x_start = x;
unsigned spareout = font_pixels / 500; row++;
// Skip pixel for spareout } else if (row) {
if (!(no++ % spareout)) { if (currentFontBoxCount < 125 || row > 2 || (y % 3)) {
// Draw pixel with melonDS fallback for debug builds glBoxFilled(topleftpos->x + x_start, topleftpos->y + y, topleftpos->x + x - (!bold)*1, topleftpos->y + y, color);
# ifdef NDEBUG currentFontBoxCount++;
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++;
} }
row = 0;
} }
x++; ++x;
} }
return fontChar != fontBitmapUnk; 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) { for (const char c : str) {
printChar(c, &topleftpos, color); printChar(c, &topleftpos, color, bold);
topleftpos.x += FONT_WIDTH + FONT_PADDING; topleftpos.x += FONT_WIDTH + FONT_PADDING;
} }
} }
@ -164,7 +170,7 @@ NDSUI::NDSUI() {
void NDSUI::run() { void NDSUI::run() {
currentInstance = this; currentInstance = this;
font_pixels = 0; currentFontBoxCount = 0;
// Update keyboard // Update keyboard
keyboardUpdate(); keyboardUpdate();
// Draw // Draw
@ -179,7 +185,7 @@ void NDSUI::run() {
LineWrappedString NDSUI::createLogMessage(const char *username, std::string_view message) { LineWrappedString NDSUI::createLogMessage(const char *username, std::string_view message) {
// Do line-wrapping, create and return Message instance // 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) { void LineWrappedString::split(std::string_view str, unsigned maxCharsPerLine) {

View file

@ -61,7 +61,7 @@ class NDSUI {
unsigned inputLineBufHead = 0; unsigned inputLineBufHead = 0;
std::vector<LineWrappedString> messages; std::vector<LineWrappedString> messages;
std::unique_ptr<basiccoro::SingleEvent<std::string>> userInputHandler = nullptr; std::unique_ptr<basiccoro::SingleEvent<std::string>> userInputHandler = nullptr;
size_t font_pixels; unsigned currentFontBoxCount;
static void kbCallback(int key); static void kbCallback(int key);
@ -78,8 +78,8 @@ class NDSUI {
static consteval static consteval
unsigned calcMaxLinesInRow(unsigned height); unsigned calcMaxLinesInRow(unsigned height);
bool printChar(const char character, 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); void print(std::string_view str, Position2D topleftpos, const int color, bool bold = false);
void handleInput(char); void handleInput(char);