From 343a4c1ab2489d380f6d9ded4cfebf25eb44b6c5 Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 22 Jun 2023 21:52:34 +0200 Subject: [PATCH] Support color in text --- .../src/FontWriter/font_writer.cpp | 41 +++++++++++++------ .../src/FontWriter/font_writer.hpp | 7 +++- examples/PoolBox/application/src/main.cpp | 3 +- include/PSX/Auxiliary/types.hpp | 18 ++++++++ 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/examples/PoolBox/application/src/FontWriter/font_writer.cpp b/examples/PoolBox/application/src/FontWriter/font_writer.cpp index 1d524acb..bf80eb3a 100644 --- a/examples/PoolBox/application/src/FontWriter/font_writer.cpp +++ b/examples/PoolBox/application/src/FontWriter/font_writer.cpp @@ -28,43 +28,58 @@ namespace FontWriter { } } - void write(int16_t x, int16_t y, const char* text) { + Position write(Position pos, const char* text) { + static const auto parse_esc = [](const char* text, const GPU::Color24& color) -> pair { + static const auto char_to_color = [](char number) constexpr -> uint8_t { + return (1 << (number - '0')) - 1; + }; + if(text[0] != '[' || text[2] != ';' || text[4] != ';' || text[6] != 'm') { + return {text, color}; + } + + return {text + 7, GPU::Color24(char_to_color(text[1]), char_to_color(text[3]), char_to_color(text[5]))}; + }; const auto* cur_text_end = &TextBuffer[GPU::Display::current_id][TextBufferSize]; - const auto org_x = x; + const auto org_x = pos.x; + auto font_color = GPU::Color24::Grey(); while(TextPtr < cur_text_end) { const char cur_char = *text; text++; - (*TextPtr)->position = {x, y}; + (*TextPtr)->position = pos; switch(cur_char) { case '\0': - return; + goto end; + case '\n': - y += 16; - x = org_x; + pos.y += 16; + pos.x = org_x; continue; + case '\x1b': + tie(text, font_color) = parse_esc(text, font_color); + continue; + case 'i': case '!': - x += 12; + pos.x += 12; break; default: - x += 16; - } - - if(cur_char == '\0') { - break; + pos.x += 16; } const uint8_t char_id = cur_char - '!'; (*TextPtr)->page = {static_cast((char_id & 0xF) << 4), static_cast((char_id >> 4) << 4)}; - (*TextPtr)->color = GPU::Color24::Grey(); + (*TextPtr)->color = font_color; TextLink = &TextLink->concat(*TextPtr); TextPtr++; } + + end: + return pos; } void render() { diff --git a/examples/PoolBox/application/src/FontWriter/font_writer.hpp b/examples/PoolBox/application/src/FontWriter/font_writer.hpp index 372cad4e..8df03c4e 100644 --- a/examples/PoolBox/application/src/FontWriter/font_writer.hpp +++ b/examples/PoolBox/application/src/FontWriter/font_writer.hpp @@ -1,14 +1,17 @@ #ifndef __FONT_WRITER_HPP__ #define __FONT_WRITER_HPP__ +#include #include namespace FontWriter { + using Position = JabyEngine::GPU::PositionI16; + static constexpr auto TextBufferSize = 1024; void setup(); - void write(int16_t x, int16_t y, const char* text); - void render(); + Position write(Position pos, const char* text); + void render(); } #endif //!__FONT_WRITER_HPP__ \ No newline at end of file diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index c792bc21..1f8d9abc 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -15,7 +15,8 @@ void main() { setup(); while(true) { - FontWriter::write(0, 32, "Cody is cute\n&\na BAAAAABY!!!"); + const auto end_pos = FontWriter::write({0, 32}, "Cody is cute\n&\na \x1b[8;0;0mBAAAAABY!!!"); + FontWriter::write(end_pos, "\x1b[0;7;7mJaby was\nhere c:"); GPU::swap_buffers_vsync(1); FontWriter::render(); diff --git a/include/PSX/Auxiliary/types.hpp b/include/PSX/Auxiliary/types.hpp index 0bcd6a34..38de46fb 100644 --- a/include/PSX/Auxiliary/types.hpp +++ b/include/PSX/Auxiliary/types.hpp @@ -8,6 +8,24 @@ namespace JabyEngine { S second; }; + template + struct cplx_pair { + T first; + S second; + + template + constexpr cplx_pair& operator=(const pair& obj) { + this->first = obj.first; + this->second = obj.second; + return *this; + } + }; + + template + constexpr cplx_pair tie(Args&... args) { + return {args...}; + } + enum struct Progress { InProgress = 0, Done,