not working

main
Brett 2024-11-12 00:28:48 -05:00
parent 4f68e5afce
commit 29a7923269
7 changed files with 85 additions and 10 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
include(FetchContent) include(FetchContent)
set(BLT_GRAPHICS_VERSION 1.0.6) set(BLT_GRAPHICS_VERSION 1.0.7)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1) set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})

0
build_emscript.sh Normal file → Executable file
View File

0
cloc.sh Normal file → Executable file
View File

View File

@ -107,9 +107,28 @@ namespace blt::gfx
public: public:
struct compiled_text_t struct compiled_text_t
{ {
std::shared_ptr<vertex_array_t> vao; friend font_renderer_t;
blt::size_t render_count; 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<std::unique_ptr<text_render_info_t>> renders;
}; };
public: public:
explicit font_renderer_t(); explicit font_renderer_t();
@ -122,11 +141,17 @@ namespace blt::gfx
generator->add_font(file); 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: private:
std::unique_ptr<font_generator_t> generator; std::unique_ptr<font_generator_t> generator;
std::unique_ptr<shader_t> font_shader; std::unique_ptr<shader_t> font_shader;
blt::hashmap_t<compiled_text_t*, blt::size_t> text_ptr_to_index;
std::vector<std::unique_ptr<compiled_text_t>> added_texts;
std::vector<std::unique_ptr<compiled_text_t>> cached_text_objects;
}; };
} }

0
resources/fonts/a.out Normal file → Executable file
View File

View File

@ -148,8 +148,8 @@ namespace blt::gfx
bind(); bind();
vbo.bind(); vbo.bind();
glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset);
glEnableVertexAttribArray(attribute_number); glEnableVertexAttribArray(attribute_number);
glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset);
unbind(); unbind();
} }
} }

View File

@ -42,7 +42,7 @@ namespace blt::gfx
// returns an empty optional if there is no error code // returns an empty optional if there is no error code
if (file.font.load_char(i)) if (file.font.load_char(i))
continue; continue;
auto face = *file.font.get(); auto& face = *file.font.get();
auto width = face->glyph->bitmap.width; auto width = face->glyph->bitmap.width;
auto height = face->glyph->bitmap.rows; auto height = face->glyph->bitmap.rows;
@ -67,10 +67,11 @@ namespace blt::gfx
auto end_width = used_width + width; auto end_width = used_width + width;
auto end_height = used_height + height; auto end_height = used_height + height;
used_width += width; used_width += width;
// BLT_TRACE("%d %d %d %d", begin_width, begin_height, width, height);
// upload the texture // upload the texture
upload(face->glyph->bitmap.buffer, GL_RED, 0, static_cast<blt::i32>(begin_width), static_cast<blt::i32>(begin_height), upload(face->glyph->bitmap.buffer, GL_RED, 0, static_cast<blt::i32>(begin_width), static_cast<blt::i32>(begin_height),
static_cast<blt::i32>(width), static_cast<blt::i32>(height)); static_cast<blt::i32>(width), static_cast<blt::i32>(height), GL_UNSIGNED_BYTE);
glyphs.insert(std::pair{i, bounded_t{ glyphs.insert(std::pair{i, bounded_t{
font::glyph_t{ font::glyph_t{
@ -148,9 +149,58 @@ namespace blt::gfx
blt::gfx::font::cleanup(); 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<font_renderer_t::compiled_text_t> 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<compiled_text_t>(*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<blt::ptrdiff_t>(index));
}
void font_renderer_t::compiled_text_t::change_text(std::string_view str)
{
static std::vector<float> 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()
{
}
} }