From 90731c1e52553a290f51cefc805308bc39c720dd Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 30 Nov 2023 20:55:36 -0500 Subject: [PATCH] Change to object orientated design --- examples/PoolBox/application/src/main.cpp | 9 +- support/include/FontWriter/font_writer.hpp | 30 +++++-- support/src/FontWriter/src/font_writer.cpp | 98 ++++++++-------------- 3 files changed, 60 insertions(+), 77 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 165dcaa7..fc60c91f 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -14,12 +14,13 @@ using NewFontWriter = ::JabyEngine::FontWriter; static object::Paco paco; static FontPrimitive font_buffer[2][256]; +static NewFontWriter new_font_writer; static void setup() { static constexpr auto FontTIM = SimpleTIM(320, 0, 320, DefaultFont::Info.VRAMSize.height); DefaultFont::load(&__heap_start, FontTIM); - NewFontWriter::setup(FontBufferInfo::from(font_buffer), FontTIM, DefaultFont::Info); + new_font_writer.setup(FontBufferInfo::from(font_buffer), FontTIM, DefaultFont::Info); Assets::load_for_main(); FontWriter::FontWriter::setup(); @@ -29,13 +30,11 @@ static void setup() { static void update() { FontWriter::FontWriter cursor; - auto new_cursor = NewFontWriter::start(Make::PositionI16(16, 16)); const auto end_pos = cursor.write(FontWriter::Position::create(0, 32), "Cody is cute\n&\na \x1b[8;0;0mBAAAAABY!!!"); cursor.write(end_pos, "\x1b[0;7;7mJaby was\nhere c:"); - new_cursor.write("0"); - + new_font_writer.write(Make::PositionI16(8, 8), "012345"); paco.update(); } @@ -43,7 +42,7 @@ static void render() { GPU::swap_buffers_vsync(1); FontWriter::FontWriter::render(); paco.render(); - NewFontWriter::render(); + new_font_writer.render(); } void main() { diff --git a/support/include/FontWriter/font_writer.hpp b/support/include/FontWriter/font_writer.hpp index d93529d2..4ba3f242 100644 --- a/support/include/FontWriter/font_writer.hpp +++ b/support/include/FontWriter/font_writer.hpp @@ -2,15 +2,29 @@ #include "Type/types.hpp" namespace JabyEngine { - struct FontWriter { - struct Cursor { - GPU::PositionI16 pos; + class FontWriter { + private: + GPU::TexPage::Linked tex_page; + FontBufferInfo prim_buffer; + FontPrimitive* cur_primitive; - void write(const char* str); - }; + void setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const GPU::SizeI16& font_size); - static void setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const FontInfo& font_info); - static Cursor start(const GPU::PositionI16& pos); - static void render(); + public: + static constexpr FontWriter empty() { + FontWriter instance; + + instance.tex_page = {0}; + instance.prim_buffer = FontBufferInfo::empty(); + instance.cur_primitive = nullptr; + return instance; + } + + void setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const FontInfo& font_info) { + FontWriter::setup(buffer_info, vram_dst, Make::SizeI16(font_info.FontSize.width, font_info.FontSize.height)); + } + + void write(GPU::PositionI16 pos, const char* str); + void render(); }; } \ No newline at end of file diff --git a/support/src/FontWriter/src/font_writer.cpp b/support/src/FontWriter/src/font_writer.cpp index f055b8f2..c4024817 100644 --- a/support/src/FontWriter/src/font_writer.cpp +++ b/support/src/FontWriter/src/font_writer.cpp @@ -3,79 +3,49 @@ #include #include -#include - namespace JabyEngine { - struct DoubleBuffer { - GPU::TexPage::Linked tex_page; - FontBufferInfo prim_buffer; - FontPrimitive* cur_primitive; - - static constexpr DoubleBuffer empty() { - return DoubleBuffer{.tex_page = {0}, .prim_buffer = FontBufferInfo::empty(), .cur_primitive = nullptr}; - } + void FontWriter :: setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const GPU::SizeI16& font_size) { + this->tex_page = Make::TexPage(vram_dst.get_texture_position(), GPU::TexturePageColor::$4bit).linked(); + this->prim_buffer = buffer_info; + this->cur_primitive = buffer_info.double_buffer[GPU::Display::current_id]; - void setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const GPU::SizeI16& font_size) { - this->tex_page = Make::TexPage(vram_dst.get_texture_position(), GPU::TexturePageColor::$4bit).linked(); - this->prim_buffer = buffer_info; - this->cur_primitive = buffer_info.double_buffer[GPU::Display::current_id]; - - for(size_t buffer_id = 0; buffer_id < 2; buffer_id++) { - for(size_t buffer_element_id = 0; buffer_element_id < buffer_info.single_buffer_length; buffer_element_id++) { - this->prim_buffer.double_buffer[buffer_id][buffer_element_id] = Make::SPRT( - Make::AreaI16(Make::PositionI16(0, 0), font_size), - Make::OffsetPageWithClut(Make::PageOffset(0, 0), Make::PageClut(vram_dst.get_clut_position())) - ).linked(); - } + for(size_t buffer_id = 0; buffer_id < 2; buffer_id++) { + for(size_t buffer_element_id = 0; buffer_element_id < buffer_info.single_buffer_length; buffer_element_id++) { + this->prim_buffer.double_buffer[buffer_id][buffer_element_id] = Make::SPRT( + Make::AreaI16(Make::PositionI16(0, 0), font_size), + Make::OffsetPageWithClut(Make::PageOffset(0, 0), Make::PageClut(vram_dst.get_clut_position())) + ).linked(); } } - - void write(GPU::PositionI16 pos, const char* str) { - const auto* primitive_end = &this->prim_buffer.double_buffer[GPU::Display::current_id][this->prim_buffer.single_buffer_length]; - - while(this->cur_primitive < primitive_end && *str != '\0') { - auto& primitive = *this->cur_primitive; - - this->cur_primitive++; - str++; - primitive->position = pos; - primitive.concat(*this->cur_primitive); - - pos.move(primitive->size.width, 0); - } - } - - void render() { - const auto update_id = GPU::Display::current_id; - const auto render_id = update_id ^ 1; - - this->cur_primitive -= 1; - if(this->cur_primitive >= this->prim_buffer.double_buffer[render_id]) { - this->cur_primitive->terminate(); - this->tex_page.concat(*this->prim_buffer.double_buffer[render_id]); - - GPU::render(this->tex_page); - } - - this->cur_primitive = this->prim_buffer.double_buffer[update_id]; - } - }; - - static auto double_buffer = DoubleBuffer::empty(); - - void FontWriter::Cursor :: write(const char* str) { - double_buffer.write(this->pos, str); } - void FontWriter :: setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const FontInfo& font_info) { - double_buffer.setup(buffer_info, vram_dst, Make::SizeI16(font_info.FontSize.width, font_info.FontSize.height)); - } + void FontWriter :: write(GPU::PositionI16 pos, const char* str) { + const auto* primitive_end = &this->prim_buffer.double_buffer[GPU::Display::current_id][this->prim_buffer.single_buffer_length]; - FontWriter::Cursor FontWriter :: start(const GPU::PositionI16& pos) { - return Cursor{.pos = pos}; + while(this->cur_primitive < primitive_end && *str != '\0') { + auto& primitive = *this->cur_primitive; + + this->cur_primitive++; + str++; + primitive->position = pos; + primitive.concat(*this->cur_primitive); + + pos.move(primitive->size.width, 0); + } } void FontWriter :: render() { - double_buffer.render(); + const auto update_id = GPU::Display::current_id; + const auto render_id = update_id ^ 1; + + this->cur_primitive -= 1; + if(this->cur_primitive >= this->prim_buffer.double_buffer[render_id]) { + this->cur_primitive->terminate(); + this->tex_page.concat(*this->prim_buffer.double_buffer[render_id]); + + GPU::render(this->tex_page); + } + + this->cur_primitive = this->prim_buffer.double_buffer[update_id]; } } \ No newline at end of file