// pbKit text overlay functions // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2007 Guillaume Lamonoca // SPDX-FileCopyrightText: 2017 espes // SPDX-FileCopyrightText: 2017-2020 Jannik Vogel // SPDX-FileCopyrightText: 2018-2022 Stefan Schmidt // SPDX-FileCopyrightText: 2019 Lucas Jansson // SPDX-FileCopyrightText: 2021-2025 Erik Abair #include "pbkit_print.h" #include #include #include #include "pbkit_draw.h" #define ROWS 16 #define COLS 60 static char pb_text_screen[ROWS][COLS]; static int pb_next_row = 0; static int pb_next_col = 0; // clang-format off static unsigned char systemFont[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,56,56,56,56,56,0,56,56, 108,108,0,0,0,0,0,0,0,108,254,254,108,254,254,108, 48,126,224,124,14,254,252,48,98,230,204,24,48,102,206,140, 120,220,252,120,250,222,252,118,28,28,56,0,0,0,0,0, 14,28,28,28,28,28,28,14,112,56,56,56,56,56,56,112, 0,0,0,230,124,56,124,206,0,56,56,254,254,56,56,0, 0,0,0,0,0,28,28,56,0,0,0,0,124,124,0,0, 0,0,0,0,0,0,56,56,28,28,56,56,112,112,224,224, 124,254,238,238,238,254,254,124,56,120,248,56,56,254,254,254, 252,254,14,60,112,254,254,254,252,254,14,60,14,254,254,252, 238,238,238,254,254,14,14,14,254,254,224,252,14,254,254,252, 124,252,224,252,238,254,254,124,252,254,14,14,28,28,56,56, 124,254,238,124,238,254,254,124,124,254,238,126,14,254,254,252, 0,0,28,28,0,28,28,28,0,0,28,28,0,28,28,56, 6,14,28,56,56,28,14,6,0,0,124,124,0,124,124,124, 112,56,28,14,14,28,56,112,124,254,206,28,56,0,56,56, 124,198,190,182,190,182,200,126,124,254,238,254,238,238,238,238, 252,254,206,252,206,254,254,252,124,254,238,224,238,254,254,124, 252,254,238,238,238,254,254,252,254,254,224,248,224,254,254,254, 126,254,224,248,224,224,224,224,126,254,224,238,238,254,254,124, 238,238,238,254,238,238,238,238,254,254,56,56,56,254,254,254, 254,254,14,14,238,254,254,124,238,238,252,248,252,238,238,238, 224,224,224,224,224,254,254,126,130,198,238,254,254,238,238,238, 206,238,254,254,254,254,238,230,124,254,238,238,238,254,254,124, 252,254,238,238,252,224,224,224,124,254,238,238,254,254,252,118, 252,254,238,238,252,238,238,238,126,254,224,124,14,254,254,252, 254,254,56,56,56,56,56,56,238,238,238,238,238,254,254,124, 238,238,238,238,238,238,124,56,238,238,238,254,254,238,198,130, 238,238,124,56,124,238,238,238,238,238,124,124,56,56,112,112, 254,254,28,56,112,254,254,254,124,124,112,112,112,124,124,124, 112,112,56,56,28,28,14,14,124,124,28,28,28,124,124,124, 56,124,238,198,0,0,0,0,0,0,0,0,0,254,254,254, 56,56,28,0,0,0,0,0,0,124,254,238,254,238,238,238, 0,252,254,206,252,206,254,252,0,124,254,238,224,238,254,124, 0,252,254,238,238,238,254,252,0,254,254,224,248,224,254,254, 0,126,254,224,248,224,224,224,0,126,254,224,238,238,254,124, 0,238,238,238,254,238,238,238,0,254,254,56,56,56,254,254, 0,254,254,14,14,238,254,124,0,238,238,252,248,252,238,238, 0,224,224,224,224,224,254,126,0,130,198,238,254,254,238,238, 0,206,238,254,254,254,238,230,0,124,254,238,238,238,254,124, 0,252,254,238,238,252,224,224,0,124,254,238,238,254,252,118, 0,252,254,238,238,252,238,238,0,126,254,224,124,14,254,252, 0,254,254,56,56,56,56,56,0,238,238,238,238,238,254,124, 0,238,238,238,238,238,124,56,0,238,238,238,254,238,198,130, 0,238,238,124,56,124,238,238,0,238,238,124,124,56,56,112, 0,254,254,28,56,112,254,254,60,124,112,112,112,124,124,60, 56,56,56,0,56,56,56,56,120,124,28,28,28,124,124,120, 236,254,118,0,0,0,0,0,0,16,56,124,254,254,254,254, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; // clang-format on // Discards the first row of text and shifts all remaining text up by one. static void pb_scrollup (void) { for (int i = 0; i < ROWS - 1; ++i) { memcpy(&pb_text_screen[i][0], &pb_text_screen[i + 1][0], COLS); } memset(&pb_text_screen[ROWS - 1][0], 0, COLS); } static inline void goto_next_row () { if (++pb_next_row >= ROWS) { pb_next_row = ROWS - 1; pb_scrollup(); } pb_next_col = 0; } void pb_print_char (char c) { if (c == '\n') { goto_next_row(); } else if (c == '\r') { pb_next_col = 0; } else if (c == '\b') { if (--pb_next_col < 0) { pb_next_col = 0; } } else if (c >= ' ') { pb_text_screen[pb_next_row][pb_next_col++] = c; if (pb_next_col >= COLS) { goto_next_row(); } } } void pb_print (const char *format, ...) { char buffer[512]; int i; va_list argList; va_start(argList, format); vsprintf(buffer, format, argList); va_end(argList); for (i = 0; i < strlen(buffer); i++) { pb_print_char(buffer[i]); } } void pb_printat (int row, int col, const char *format, ...) { char buffer[512]; int i; if ((row >= 0) && (row < ROWS)) { pb_next_row = row; } if ((col >= 0) && (col < COLS)) { pb_next_col = col; } va_list argList; va_start(argList, format); vsprintf(buffer, format, argList); va_end(argList); for (i = 0; i < strlen(buffer); i++) { pb_print_char(buffer[i]); } } void pb_erase_text_screen (void) { pb_next_row = 0; pb_next_col = 0; memset(pb_text_screen, 0, sizeof(pb_text_screen)); } void pb_draw_text_screen (void) { int i, j, k, l, m, x1, x2, y; unsigned char c; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { c = pb_text_screen[i][j]; if ((c == ' ') || (c == '\t')) { pb_text_screen[i][j] = 0; } } } // convert pb_text_screen characters into push buffer commands // TODO: replace rectangle fill with texture copy when available! for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { c = pb_text_screen[i][j]; if (c) { for (l = 0, x1 = -1, x2 = -1; l < 8; l++, x1 = -1, x2 = -1) { for (k = 0, m = 0x80; k < 8; k++, m >>= 1) { if (systemFont[c * 8 + l] & m) { if (x1 >= 0) { x2 = 20 + j * 10 + k; } else { x1 = 20 + j * 10 + k; } } else { if (x2 >= 0) { y = 25 + i * 25 + l * 2; pb_fill(x1, y, x2 - x1 + 1, 2, 0xFFFFFF); x1 = x2 = -1; } else if (x1 >= 0) { y = 25 + i * 25 + l * 2; pb_fill(x1, y, 1, 2, 0xFFFFFF); x1 = -1; } } } if (x2 >= 0) { y = 25 + i * 25 + l * 2; pb_fill(x1, y, x2 - x1 + 1, 2, 0xFFFFFF); } else if (x1 >= 0) { y = 25 + i * 25 + l * 2; pb_fill(x1, y, 1, 2, 0xFFFFFF); } } } } } }