From 378196d422e4a454634b63c61aa5cad856f7c452 Mon Sep 17 00:00:00 2001 From: jaby Date: Wed, 21 Jun 2023 22:36:31 +0200 Subject: [PATCH] Support simple sentances --- .../src/FontWriter/font_writer.cpp | 77 +++++++++++++++++++ .../src/FontWriter/font_writer.hpp | 14 ++++ examples/PoolBox/application/src/main.cpp | 24 +++--- 3 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 examples/PoolBox/application/src/FontWriter/font_writer.cpp create mode 100644 examples/PoolBox/application/src/FontWriter/font_writer.hpp diff --git a/examples/PoolBox/application/src/FontWriter/font_writer.cpp b/examples/PoolBox/application/src/FontWriter/font_writer.cpp new file mode 100644 index 00000000..1d524acb --- /dev/null +++ b/examples/PoolBox/application/src/FontWriter/font_writer.cpp @@ -0,0 +1,77 @@ +#include "../assets.hpp" +#include "font_writer.hpp" +#include +#include + +namespace FontWriter { + using namespace JabyEngine; + static GPU::TexPage::Linked CharTexPage[2] = { + GPU::TexPage({Assets::FontTIM.get_texture_x(), Assets::FontTIM.get_texture_y()}, GPU::TexturePageColor::$4bit).linked(), + GPU::TexPage({Assets::FontTIM.get_texture_x(), Assets::FontTIM.get_texture_y()}, GPU::TexturePageColor::$4bit).linked() + }; + static GPU::SPRT_16::Linked TextBuffer[2][TextBufferSize]; + static GPU::Link* TextLink; + static GPU::SPRT_16::Linked* TextPtr; + + static void reset_links() { + TextLink = &CharTexPage[GPU::Display::current_id]; + TextPtr = TextBuffer[GPU::Display::current_id]; + } + + void setup() { + reset_links(); + for(auto& char_array : TextBuffer) { + for(auto& single_char : char_array) { + single_char->set_identitiy(); + single_char->clut = GPU::PageClut(Assets::FontTIM.get_clut_x(), Assets::FontTIM.get_clut_y()); + } + } + } + + void write(int16_t x, int16_t y, const char* text) { + const auto* cur_text_end = &TextBuffer[GPU::Display::current_id][TextBufferSize]; + const auto org_x = x; + + while(TextPtr < cur_text_end) { + const char cur_char = *text; + text++; + + (*TextPtr)->position = {x, y}; + switch(cur_char) { + case '\0': + return; + case '\n': + y += 16; + x = org_x; + continue; + + case 'i': + case '!': + x += 12; + break; + + default: + x += 16; + } + + if(cur_char == '\0') { + break; + } + + 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(); + + TextLink = &TextLink->concat(*TextPtr); + TextPtr++; + } + } + + void render() { + const auto render_id = GPU::Display::current_id ^ 1; + + TextLink->terminate(); + GPU::render(CharTexPage[render_id]); + reset_links(); + } +} diff --git a/examples/PoolBox/application/src/FontWriter/font_writer.hpp b/examples/PoolBox/application/src/FontWriter/font_writer.hpp new file mode 100644 index 00000000..372cad4e --- /dev/null +++ b/examples/PoolBox/application/src/FontWriter/font_writer.hpp @@ -0,0 +1,14 @@ +#ifndef __FONT_WRITER_HPP__ +#define __FONT_WRITER_HPP__ +#include + +namespace FontWriter { + static constexpr auto TextBufferSize = 1024; + + void setup(); + + void write(int16_t x, int16_t y, 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 277b9103..c792bc21 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -1,25 +1,23 @@ +#include "FontWriter/font_writer.hpp" #include "assets.hpp" #include #include #include using namespace JabyEngine; -static auto FontPage = GPU::TexPage({Assets::FontTIM.get_texture_x(), Assets::FontTIM.get_texture_y()}, GPU::TexturePageColor::$4bit).linked(); -static auto FontTest = GPU::SPRT( - GPU::AreaI16({0, 0}, {256, 96}), {GPU::PagePosition(0, 0), GPU::PageClut(Assets::FontTIM.get_clut_x(), Assets::FontTIM.get_clut_y())}, - GPU::Color24::Grey() -).linked(); -static auto FontTestColor = GPU::SPRT( - GPU::AreaI16({0, 96}, {256, 96}), {GPU::PagePosition(0, 0), GPU::PageClut(Assets::FontTIM.get_clut_x(), Assets::FontTIM.get_clut_y())}, - GPU::Color24(0x0, 0x0, 0xA0) -).linked(); + +static void setup() { + Assets::load_for_main(); + FontWriter::setup(); +} void main() { - Assets::load_for_main(); - - FontPage.concat(FontTest).concat(FontTestColor); + setup(); + while(true) { + FontWriter::write(0, 32, "Cody is cute\n&\na BAAAAABY!!!"); + GPU::swap_buffers_vsync(1); - GPU::render(FontPage); + FontWriter::render(); } } \ No newline at end of file