From 5283a4487eb8449b967e0690b1cdd8a2f8470e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gaier?= Date: Fri, 1 Dec 2023 14:31:32 -0500 Subject: [PATCH] Display text --- examples/PoolBox/application/src/main.cpp | 2 +- include/PSX/GPU/gpu_types.hpp | 27 +++++++++++--------- support/include/FontWriter/font_writer.hpp | 8 +++--- support/src/FontWriter/src/font_writer.cpp | 29 +++++++++++++++++----- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index fc60c91f..28901902 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -34,7 +34,7 @@ static void update() { 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_font_writer.write(Make::PositionI16(8, 8), "012345"); + new_font_writer.write(Make::PositionI16(8, 8), "012345 ABCDEFGHIJKL\nabcedfghijkl"); paco.update(); } diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 4df3d652..e86e96f7 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -141,6 +141,8 @@ namespace JabyEngine { return Position{.x = x, .y = y}; } }; + typedef Position PositionI16; + typedef Position PositionU16; template struct Size { @@ -151,6 +153,10 @@ namespace JabyEngine { return Size{w, h}; } }; + typedef Size SizeI16; + typedef Size SizeU16; + // Type used for primitives + typedef PositionI16 Vertex; template struct Area { @@ -173,6 +179,8 @@ namespace JabyEngine { return this->position.move(this->size.width, this->size.height); } }; + typedef Area AreaI16; + typedef Area AreaU16; // Type used for primitives struct PageOffset : public internal::XYMovement { @@ -189,20 +197,15 @@ namespace JabyEngine { static constexpr PageOffset create(uint8_t u, uint8_t v) { return PageOffset{.x = u, .y = v}; //< Activate x and y to use XYMovement during constexpr } + + static constexpr PageOffset from_tile_id(int16_t id, int16_t row_count, SizeI16 size) { + const auto x = id%row_count; + const auto y = id/row_count; + + return PageOffset::create(static_cast(size.width*x), static_cast(size.height*y)); + } }; - typedef Position PositionI16; - typedef Position PositionU16; - - typedef Size SizeI16; - typedef Size SizeU16; - - typedef Area AreaI16; - typedef Area AreaU16; - - // Type used for primitives - typedef PositionI16 Vertex; - struct VertexColor { Vertex position; Color24 color; diff --git a/support/include/FontWriter/font_writer.hpp b/support/include/FontWriter/font_writer.hpp index 4ba3f242..74381022 100644 --- a/support/include/FontWriter/font_writer.hpp +++ b/support/include/FontWriter/font_writer.hpp @@ -4,8 +4,8 @@ namespace JabyEngine { class FontWriter { private: - GPU::TexPage::Linked tex_page; FontBufferInfo prim_buffer; + GPU::TexPage::Linked tex_page; FontPrimitive* cur_primitive; void setup(const FontBufferInfo& buffer_info, const SimpleTIM& vram_dst, const GPU::SizeI16& font_size); @@ -14,8 +14,8 @@ namespace JabyEngine { static constexpr FontWriter empty() { FontWriter instance; - instance.tex_page = {0}; instance.prim_buffer = FontBufferInfo::empty(); + instance.tex_page = {0}; instance.cur_primitive = nullptr; return instance; } @@ -24,7 +24,7 @@ namespace JabyEngine { 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(); + GPU::PositionI16 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 c4024817..4204e2c3 100644 --- a/support/src/FontWriter/src/font_writer.cpp +++ b/support/src/FontWriter/src/font_writer.cpp @@ -5,8 +5,8 @@ namespace JabyEngine { 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->tex_page = Make::TexPage(vram_dst.get_texture_position(), GPU::TexturePageColor::$4bit).linked(); this->cur_primitive = buffer_info.double_buffer[GPU::Display::current_id]; for(size_t buffer_id = 0; buffer_id < 2; buffer_id++) { @@ -19,19 +19,36 @@ namespace JabyEngine { } } - void FontWriter :: write(GPU::PositionI16 pos, const char* str) { + GPU::PositionI16 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]; + const auto font_size = this->prim_buffer.double_buffer[0][0]->size; + const auto row_count = 256/font_size.width; + const auto old_x = pos.x; while(this->cur_primitive < primitive_end && *str != '\0') { - auto& primitive = *this->cur_primitive; + const auto cur_char = *str++; + if(cur_char == '\n') { + pos.x = old_x; + pos.y += font_size.height; + continue; + } - this->cur_primitive++; - str++; - primitive->position = pos; + if(cur_char == ' ') { + pos.x += font_size.width; + continue; + } + + auto& primitive = *this->cur_primitive++; + + primitive->position = pos; + primitive->tex_offset = GPU::PageOffset::from_tile_id(cur_char - '!', row_count, font_size); + primitive->color = GPU::Color24::Grey(); primitive.concat(*this->cur_primitive); pos.move(primitive->size.width, 0); } + + return pos; } void FontWriter :: render() {