#include "../assets.hpp" #include "font_writer.hpp" #include #include namespace FontWriter { using namespace JabyEngine; FontWriter::Pool::Buffer FontWriter::Pool :: buffer[2]; GPU::Link* FontWriter::Pool :: last_link = nullptr; GPU::SPRT_16::Linked* FontWriter::Pool :: text_ptr = nullptr; void FontWriter::Pool :: reset_links() { Pool::last_link = &Pool::buffer[GPU::Display::current_id].page; Pool::text_ptr = Pool::buffer[GPU::Display::current_id].text_buffer; } void FontWriter::Pool::Buffer :: setup() { this->page = GPU::TexPage::create(GPU::PositionU16::create(Assets::FontTIM.get_texture_x(), Assets::FontTIM.get_texture_y()), GPU::TexturePageColor::$4bit).linked(); for(auto& single_char : this->text_buffer) { single_char.set_link_identitiy(); single_char->set_identitiy(); single_char->clut = GPU::PageClut::create(Assets::FontTIM.get_clut_x(), Assets::FontTIM.get_clut_y()); } } void FontWriter::Pool :: setup() { Pool::reset_links(); for(auto& buffer : Pool::buffer) { buffer.setup(); } } void FontWriter::Pool :: render() { const auto render_id = GPU::Display::current_id ^ 1; Pool::last_link->terminate(); GPU::render(Pool::buffer[render_id].page); reset_links(); } Position FontWriter :: 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::from_rgb(char_to_color(text[1]), char_to_color(text[3]), char_to_color(text[5]))}; }; const auto* cur_text_end = &Pool::buffer[GPU::Display::current_id].text_buffer[Pool::Buffer::BufferSize]; const auto org_x = pos.x; auto font_color = GPU::Color24::Grey(); while(Pool::text_ptr < cur_text_end) { const char cur_char = *text; text++; (*Pool::text_ptr)->position = pos; switch(cur_char) { case '\0': goto end; case '\n': pos.y += 16; pos.x = org_x; continue; case '\x1b': tie(text, font_color) = parse_esc(text, font_color); continue; case 'i': case '!': pos.x += 12; break; default: pos.x += 16; } const uint8_t char_id = cur_char - '!'; (*Pool::text_ptr)->tex_offset = GPU::PageOffset::create(((char_id & 0xF) << 4), ((char_id >> 4) << 4)); (*Pool::text_ptr)->color = font_color; Pool::last_link = &Pool::last_link->concat(*Pool::text_ptr); Pool::text_ptr++; } end: return pos; } }