Support color in text

This commit is contained in:
Jaby 2023-06-22 21:52:34 +02:00
parent a4a7abe9b0
commit 343a4c1ab2
4 changed files with 53 additions and 16 deletions

View File

@ -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<const char*, GPU::Color24> {
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<uint8_t>((char_id & 0xF) << 4), static_cast<uint8_t>((char_id >> 4) << 4)};
(*TextPtr)->color = GPU::Color24::Grey();
(*TextPtr)->color = font_color;
TextLink = &TextLink->concat(*TextPtr);
TextPtr++;
}
end:
return pos;
}
void render() {

View File

@ -1,13 +1,16 @@
#ifndef __FONT_WRITER_HPP__
#define __FONT_WRITER_HPP__
#include <PSX/GPU/gpu_types.hpp>
#include <stdint.h>
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);
Position write(Position pos, const char* text);
void render();
}

View File

@ -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();

View File

@ -8,6 +8,24 @@ namespace JabyEngine {
S second;
};
template<typename T, typename S>
struct cplx_pair {
T first;
S second;
template<typename T2, typename S2>
constexpr cplx_pair<T, S>& operator=(const pair<T2, S2>& obj) {
this->first = obj.first;
this->second = obj.second;
return *this;
}
};
template <typename... Args>
constexpr cplx_pair<Args&...> tie(Args&... args) {
return {args...};
}
enum struct Progress {
InProgress = 0,
Done,