From 29a7923269fc815505dc5324133ce7f61f60502a Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 12 Nov 2024 00:28:48 -0500 Subject: [PATCH] not working --- CMakeLists.txt | 2 +- build_emscript.sh | 0 cloc.sh | 0 include/blt/gfx/renderer/font_renderer.h | 31 ++++++++++-- resources/fonts/a.out | Bin src/blt/gfx/model.cpp | 2 +- src/blt/gfx/renderer/font_renderer.cpp | 60 +++++++++++++++++++++-- 7 files changed, 85 insertions(+), 10 deletions(-) mode change 100644 => 100755 build_emscript.sh mode change 100644 => 100755 cloc.sh mode change 100644 => 100755 resources/fonts/a.out diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c635c6..22e0192 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.25) include(FetchContent) -set(BLT_GRAPHICS_VERSION 1.0.6) +set(BLT_GRAPHICS_VERSION 1.0.7) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) diff --git a/build_emscript.sh b/build_emscript.sh old mode 100644 new mode 100755 diff --git a/cloc.sh b/cloc.sh old mode 100644 new mode 100755 diff --git a/include/blt/gfx/renderer/font_renderer.h b/include/blt/gfx/renderer/font_renderer.h index 9b7db17..c1c1672 100644 --- a/include/blt/gfx/renderer/font_renderer.h +++ b/include/blt/gfx/renderer/font_renderer.h @@ -107,9 +107,28 @@ namespace blt::gfx public: struct compiled_text_t { - std::shared_ptr vao; - blt::size_t render_count; + friend font_renderer_t; + public: + explicit compiled_text_t(font_generator_t& generator); + + void change_text(std::string_view str); + + private: + void draw(); + + struct text_render_info_t + { + vertex_array_t vao{}; + font_texture_atlas* texture{}; + blt::size_t render_count = 0; + + text_render_info_t() = default; + }; + + font_generator_t& generator; + std::vector> renders; }; + public: explicit font_renderer_t(); @@ -122,11 +141,17 @@ namespace blt::gfx generator->add_font(file); } - compiled_text_t create_text(std::string_view str); + compiled_text_t* create_text(std::string_view str); + + void destroy_text(compiled_text_t* text); private: std::unique_ptr generator; std::unique_ptr font_shader; + + blt::hashmap_t text_ptr_to_index; + std::vector> added_texts; + std::vector> cached_text_objects; }; } diff --git a/resources/fonts/a.out b/resources/fonts/a.out old mode 100644 new mode 100755 diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 5914cdf..5115e58 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -148,8 +148,8 @@ namespace blt::gfx bind(); vbo.bind(); - glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); glEnableVertexAttribArray(attribute_number); + glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); unbind(); } } \ No newline at end of file diff --git a/src/blt/gfx/renderer/font_renderer.cpp b/src/blt/gfx/renderer/font_renderer.cpp index 30747cc..878d60d 100644 --- a/src/blt/gfx/renderer/font_renderer.cpp +++ b/src/blt/gfx/renderer/font_renderer.cpp @@ -42,7 +42,7 @@ namespace blt::gfx // returns an empty optional if there is no error code if (file.font.load_char(i)) continue; - auto face = *file.font.get(); + auto& face = *file.font.get(); auto width = face->glyph->bitmap.width; auto height = face->glyph->bitmap.rows; @@ -67,10 +67,11 @@ namespace blt::gfx auto end_width = used_width + width; auto end_height = used_height + height; used_width += width; - + +// BLT_TRACE("%d %d %d %d", begin_width, begin_height, width, height); // upload the texture upload(face->glyph->bitmap.buffer, GL_RED, 0, static_cast(begin_width), static_cast(begin_height), - static_cast(width), static_cast(height)); + static_cast(width), static_cast(height), GL_UNSIGNED_BYTE); glyphs.insert(std::pair{i, bounded_t{ font::glyph_t{ @@ -148,9 +149,58 @@ namespace blt::gfx blt::gfx::font::cleanup(); } - font_renderer_t::compiled_text_t font_renderer_t::create_text(std::string_view str) + font_renderer_t::compiled_text_t* font_renderer_t::create_text(std::string_view str) { - return font_renderer_t::compiled_text_t(); + std::unique_ptr ptr; + if (!cached_text_objects.empty()) + { + ptr = std::move(*cached_text_objects.begin()); + cached_text_objects.erase(cached_text_objects.begin()); + } else + { + ptr = std::make_unique(*generator); + } + ptr->change_text(str); + auto index = added_texts.size(); + added_texts.push_back(std::move(ptr)); + text_ptr_to_index[added_texts.back().get()] = index; + return added_texts.back().get(); } + void font_renderer_t::destroy_text(font_renderer_t::compiled_text_t* text) + { + auto index = text_ptr_to_index[text]; + text_ptr_to_index.erase(text); + cached_text_objects.push_back(std::move(added_texts[index])); + added_texts[index] = nullptr; + added_texts.erase(added_texts.begin() + static_cast(index)); + } + + void font_renderer_t::compiled_text_t::change_text(std::string_view str) + { + static std::vector vertices; + vertices.clear(); + + for (const auto& c : str) + { + + } + } + + font_renderer_t::compiled_text_t::compiled_text_t(font_generator_t& generator): generator(generator) + { + renders.emplace_back(); + auto& vao = renders.back()->vao; + auto vbo = vertex_array_t::make_vbo({}); + vbo->vbo.create(); + vbo->vbo.bind(); + vbo->vbo.allocate(sizeof(float) * 6 * 4, GL_DYNAMIC_DRAW); + vao.bind(); + vao.bindVBO(vbo, 0, 4, GL_FLOAT, 4 * sizeof(float), 0); + } + + void font_renderer_t::compiled_text_t::draw() + { + + } } \ No newline at end of file