2024-11-11 02:29:28 -05:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_WITH_GRAPHICS_FONT_RENDERER_H
|
|
|
|
#define BLT_WITH_GRAPHICS_FONT_RENDERER_H
|
|
|
|
|
|
|
|
#include <blt/gfx/font/font.h>
|
2024-11-11 16:47:25 -05:00
|
|
|
#include <blt/gfx/texture.h>
|
2024-11-11 19:21:42 -05:00
|
|
|
#include <blt/gfx/shader.h>
|
|
|
|
#include <blt/gfx/model.h>
|
2024-11-14 11:26:37 -05:00
|
|
|
#include <blt/gfx/framebuffer.h>
|
2024-11-11 16:47:25 -05:00
|
|
|
#include <blt/std/hashmap.h>
|
|
|
|
#include <blt/std/binary_tree.h>
|
2024-11-11 02:29:28 -05:00
|
|
|
|
|
|
|
namespace blt::gfx
|
|
|
|
{
|
|
|
|
|
2024-11-11 16:47:25 -05:00
|
|
|
class font_texture_atlas : public texture_gl2D
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct bounded_t
|
|
|
|
{
|
|
|
|
font::glyph_t glyph;
|
2024-11-12 18:26:34 -05:00
|
|
|
// starting point inside texture atlas
|
2024-11-11 16:47:25 -05:00
|
|
|
blt::vec2ui min;
|
2024-11-11 19:21:42 -05:00
|
|
|
// ending point of the glyph, inside texture atlas
|
2024-11-11 16:47:25 -05:00
|
|
|
blt::vec2ui max;
|
|
|
|
};
|
2024-11-11 19:21:42 -05:00
|
|
|
struct added_font_results_t
|
|
|
|
{
|
|
|
|
blt::u64 last_inserted_index;
|
|
|
|
blt::u64 min_index_used;
|
|
|
|
blt::u64 max_index_used;
|
|
|
|
};
|
|
|
|
public:
|
2024-11-12 19:30:28 -05:00
|
|
|
explicit font_texture_atlas(blt::i32 dimensions, blt::u32 padding);
|
2024-11-11 16:47:25 -05:00
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
added_font_results_t add_font(const font::font_file_t& file, float size);
|
2024-11-11 16:47:25 -05:00
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
added_font_results_t add_font(const font::font_file_t& file, blt::u64 start, float size);
|
2024-11-11 16:47:25 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
bounded_t& get_glyph(blt::u64 c, std::string_view font)
|
|
|
|
{ return glyphs.at(font).at(c); }
|
2024-11-11 16:47:25 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
[[nodiscard]] const bounded_t& get_glyph(blt::u64 c, std::string_view font) const
|
|
|
|
{ return glyphs.at(font).at(c); }
|
2024-11-11 16:47:25 -05:00
|
|
|
|
|
|
|
private:
|
2024-11-13 15:24:58 -05:00
|
|
|
blt::hashmap_t<std::string, blt::hashmap_t<blt::u64, bounded_t>> glyphs;
|
2024-11-11 16:47:25 -05:00
|
|
|
blt::u32 used_width = 0;
|
|
|
|
blt::u32 used_height = 0;
|
2024-11-12 19:30:28 -05:00
|
|
|
blt::u32 padding;
|
2024-11-11 16:47:25 -05:00
|
|
|
};
|
2024-11-11 02:29:28 -05:00
|
|
|
|
2024-11-11 19:21:42 -05:00
|
|
|
class font_generator_t
|
|
|
|
{
|
|
|
|
public:
|
2024-11-12 20:47:18 -05:00
|
|
|
|
|
|
|
struct size_bounds_t
|
2024-11-11 19:21:42 -05:00
|
|
|
{
|
|
|
|
float min_size, max_size;
|
|
|
|
blt::u64 min_char, max_char;
|
2024-11-12 20:47:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bounded_font_t
|
|
|
|
{
|
2024-11-13 15:24:58 -05:00
|
|
|
font_texture_atlas* atlas;
|
|
|
|
blt::u64 min_char, max_char;
|
2024-11-11 19:21:42 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
bounded_font_t(font_texture_atlas* atlas, u64 minChar, u64 maxChar): atlas(atlas), min_char(minChar), max_char(maxChar)
|
2024-11-11 19:21:42 -05:00
|
|
|
{}
|
2024-11-13 15:24:58 -05:00
|
|
|
// replace with font -> char range. use scaling.
|
2024-11-11 19:21:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2024-11-13 15:24:58 -05:00
|
|
|
explicit font_generator_t(blt::i32 dimensions, blt::u32 padding, float gen_size);
|
2024-11-11 19:21:42 -05:00
|
|
|
|
|
|
|
void add_font(const font::font_file_t& file);
|
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
inline font_texture_atlas& get_texture(const std::string& font, blt::u64 c)
|
2024-11-11 19:21:42 -05:00
|
|
|
{
|
2024-11-13 15:24:58 -05:00
|
|
|
for (auto& atlas : font_associations[font])
|
2024-11-11 19:21:42 -05:00
|
|
|
{
|
2024-11-12 18:26:34 -05:00
|
|
|
if (c >= atlas.min_char && c <= atlas.max_char)
|
2024-11-11 19:21:42 -05:00
|
|
|
return *atlas.atlas;
|
|
|
|
}
|
2024-11-13 15:24:58 -05:00
|
|
|
throw std::runtime_error("Character not found inside atlases with font '" + font + "'");
|
2024-11-11 19:21:42 -05:00
|
|
|
}
|
2024-11-12 18:26:34 -05:00
|
|
|
|
|
|
|
[[nodiscard]] blt::i32 get_dimensions() const
|
|
|
|
{ return dimensions; }
|
2024-11-13 15:24:58 -05:00
|
|
|
|
|
|
|
[[nodiscard]] float get_generated_size() const
|
|
|
|
{ return gen_size; }
|
2024-11-11 19:21:42 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
blt::i32 dimensions;
|
2024-11-12 19:30:28 -05:00
|
|
|
blt::u32 padding;
|
2024-11-13 15:24:58 -05:00
|
|
|
std::vector<std::unique_ptr<font_texture_atlas>> atlases;
|
|
|
|
blt::hashmap_t<std::string, std::vector<bounded_font_t>> font_associations;
|
|
|
|
float gen_size;
|
2024-11-11 19:21:42 -05:00
|
|
|
};
|
|
|
|
|
2024-11-11 02:29:28 -05:00
|
|
|
class font_renderer_t
|
|
|
|
{
|
|
|
|
public:
|
2024-11-14 16:01:53 -05:00
|
|
|
static constexpr blt::vec2f DEFAULT_SCALE{1, 1};
|
|
|
|
static constexpr blt::vec4 DEFAULT_COLOR = blt::make_color(0.8, 0.8, 0.8);
|
|
|
|
|
|
|
|
struct render_context_t
|
2024-11-14 11:26:37 -05:00
|
|
|
{
|
|
|
|
blt::vec2f position, bounds;
|
2024-11-14 16:01:53 -05:00
|
|
|
blt::vec4 color = DEFAULT_COLOR;
|
|
|
|
float z_index = 0;
|
|
|
|
float current_size = 0;
|
|
|
|
float rotation = 0;
|
|
|
|
|
|
|
|
explicit render_context_t(float current_size): current_size(current_size)
|
|
|
|
{}
|
|
|
|
|
|
|
|
render_context_t& setPosition(float x, float y)
|
|
|
|
{
|
|
|
|
position = {x, y};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setPosition(const blt::vec2f& new_position)
|
|
|
|
{
|
|
|
|
position = new_position;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setRotation(float r)
|
|
|
|
{
|
|
|
|
rotation = r;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setColor(const blt::vec4f& new_color)
|
|
|
|
{
|
|
|
|
color = new_color;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setColor(float r, float g, float b, float a = 1)
|
|
|
|
{
|
|
|
|
color = {r, g, b, a};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setBounds(const blt::vec2f& new_bounds)
|
|
|
|
{
|
|
|
|
bounds = new_bounds;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setBounds(float width, float height)
|
|
|
|
{
|
|
|
|
return setBounds({width, height});
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setUnrestrictedBounds()
|
|
|
|
{
|
|
|
|
bounds = {};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setSize(const float new_scale)
|
|
|
|
{
|
|
|
|
current_size = new_scale;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_context_t& setZIndex(float new_z_index)
|
|
|
|
{
|
|
|
|
z_index = new_z_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const vec2f& getPosition() const
|
|
|
|
{ return position; }
|
|
|
|
|
|
|
|
[[nodiscard]] const vec2f& getBounds() const
|
|
|
|
{ return bounds; }
|
|
|
|
|
|
|
|
[[nodiscard]] float getSize() const
|
|
|
|
{ return current_size; }
|
|
|
|
|
|
|
|
[[nodiscard]] float getRotation() const
|
|
|
|
{ return rotation; }
|
|
|
|
|
|
|
|
[[nodiscard]] const vec4& getColor() const
|
|
|
|
{ return color; }
|
2024-11-14 11:26:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct text_index_t
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
std::string font;
|
|
|
|
|
|
|
|
bool operator==(const text_index_t& rhs) const;
|
|
|
|
|
|
|
|
bool operator!=(const text_index_t& rhs) const;
|
|
|
|
};
|
|
|
|
|
2024-11-11 19:21:42 -05:00
|
|
|
struct compiled_text_t
|
|
|
|
{
|
2024-11-12 00:28:48 -05:00
|
|
|
friend font_renderer_t;
|
|
|
|
public:
|
2024-11-13 15:24:58 -05:00
|
|
|
compiled_text_t& setText(std::string_view str, float size, std::string_view font = "__default");
|
2024-11-12 00:28:48 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
compiled_text_t& recompile();
|
2024-11-12 18:26:34 -05:00
|
|
|
|
2024-11-14 16:01:53 -05:00
|
|
|
compiled_text_t& setPosition(float x, float y)
|
|
|
|
{
|
|
|
|
position = {x, y};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
compiled_text_t& setPosition(const blt::vec2f& pos)
|
|
|
|
{
|
|
|
|
position = pos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 16:01:53 -05:00
|
|
|
compiled_text_t& setRotation(float r)
|
|
|
|
{
|
|
|
|
rotation = r;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
compiled_text_t& setBounds(const blt::vec2f& limit)
|
2024-11-12 18:26:34 -05:00
|
|
|
{
|
2024-11-14 16:01:53 -05:00
|
|
|
if (limit == bounds)
|
|
|
|
return *this;
|
2024-11-13 15:24:58 -05:00
|
|
|
bounds = limit;
|
2024-11-14 11:26:37 -05:00
|
|
|
recompile();
|
2024-11-12 18:26:34 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:26:37 -05:00
|
|
|
compiled_text_t& setBounds(float width, float height)
|
2024-11-12 18:26:34 -05:00
|
|
|
{
|
2024-11-14 11:26:37 -05:00
|
|
|
return setBounds({width, height});
|
2024-11-12 18:26:34 -05:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:26:37 -05:00
|
|
|
compiled_text_t& setUnrestrictedBounds()
|
2024-11-12 18:26:34 -05:00
|
|
|
{
|
2024-11-14 11:26:37 -05:00
|
|
|
bounds = {};
|
2024-11-13 15:24:58 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
compiled_text_t& setColor(const blt::vec4f& c)
|
|
|
|
{
|
|
|
|
color = c;
|
2024-11-12 18:26:34 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
compiled_text_t& setColor(float r, float g, float b, float a = 1)
|
|
|
|
{
|
|
|
|
color = {r, g, b, a};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:26:37 -05:00
|
|
|
compiled_text_t& setSize(float size)
|
|
|
|
{
|
2024-11-14 16:01:53 -05:00
|
|
|
if (current_size == size)
|
|
|
|
return *this;
|
2024-11-14 11:26:37 -05:00
|
|
|
current_size = size;
|
|
|
|
scale = {size / generator.get_generated_size(), size / generator.get_generated_size()};
|
2024-11-14 16:01:53 -05:00
|
|
|
// size only changes computation if there are bounds on the text.
|
|
|
|
if (bounds.x() != 0 || bounds.y() != 0)
|
|
|
|
recompile();
|
2024-11-14 11:26:37 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
compiled_text_t& setZIndex(float s)
|
|
|
|
{
|
|
|
|
z_index = s;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
[[nodiscard]] const vec2f& getBounds() const
|
|
|
|
{ return bounds; }
|
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
[[nodiscard]] const vec2f& getPosition() const
|
|
|
|
{ return position; }
|
|
|
|
|
|
|
|
[[nodiscard]] const vec2f& getScale() const
|
|
|
|
{ return scale; }
|
|
|
|
|
|
|
|
[[nodiscard]] const vec4f& getColor() const
|
|
|
|
{ return color; }
|
|
|
|
|
|
|
|
[[nodiscard]] float getZIndex() const
|
|
|
|
{ return z_index; }
|
2024-11-14 16:01:53 -05:00
|
|
|
|
|
|
|
[[nodiscard]] float getRotation() const
|
|
|
|
{ return rotation; }
|
|
|
|
|
|
|
|
void save_to(render_context_t& save)
|
|
|
|
{
|
|
|
|
save.position = position;
|
|
|
|
save.bounds = bounds;
|
|
|
|
save.color = color;
|
|
|
|
save.z_index = z_index;
|
|
|
|
save.current_size = current_size;
|
|
|
|
save.rotation = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_from(const render_context_t& restore)
|
|
|
|
{
|
|
|
|
setPosition(restore.position);
|
|
|
|
setBounds(restore.bounds);
|
|
|
|
setColor(restore.color);
|
|
|
|
setZIndex(restore.z_index);
|
|
|
|
setSize(restore.current_size);
|
|
|
|
setRotation(restore.rotation);
|
|
|
|
}
|
2024-11-12 00:28:48 -05:00
|
|
|
|
|
|
|
private:
|
2024-11-13 15:24:58 -05:00
|
|
|
explicit compiled_text_t(font_generator_t& generator);
|
|
|
|
|
2024-11-14 16:01:53 -05:00
|
|
|
void bind() const
|
|
|
|
{
|
|
|
|
vao.bind();
|
|
|
|
}
|
|
|
|
|
2024-11-12 00:28:48 -05:00
|
|
|
void draw();
|
|
|
|
|
|
|
|
struct text_render_info_t
|
|
|
|
{
|
2024-11-12 18:26:34 -05:00
|
|
|
font_texture_atlas* texture = nullptr;
|
|
|
|
blt::size_t render_start = 0;
|
2024-11-12 00:28:48 -05:00
|
|
|
blt::size_t render_count = 0;
|
|
|
|
|
2024-11-12 18:26:34 -05:00
|
|
|
text_render_info_t(font_texture_atlas* texture, size_t renderStart, size_t renderCount):
|
|
|
|
texture(texture), render_start(renderStart), render_count(renderCount)
|
|
|
|
{}
|
2024-11-12 00:28:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
font_generator_t& generator;
|
2024-11-13 15:24:58 -05:00
|
|
|
std::string contents, font;
|
2024-11-12 18:26:34 -05:00
|
|
|
vertex_array_t vao{};
|
|
|
|
std::vector<text_render_info_t> renders;
|
2024-11-13 15:24:58 -05:00
|
|
|
blt::vec2f position, bounds;
|
2024-11-14 16:01:53 -05:00
|
|
|
blt::vec2f scale = DEFAULT_SCALE;
|
|
|
|
blt::vec4 color = DEFAULT_COLOR;
|
2024-11-12 18:26:34 -05:00
|
|
|
float z_index = 0;
|
2024-11-12 20:47:18 -05:00
|
|
|
float current_size = 0;
|
2024-11-14 16:01:53 -05:00
|
|
|
float rotation = 0;
|
2024-11-11 19:21:42 -05:00
|
|
|
};
|
2024-11-12 00:28:48 -05:00
|
|
|
|
2024-11-11 19:21:42 -05:00
|
|
|
public:
|
|
|
|
explicit font_renderer_t();
|
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
void create_default(float gen_size = 250, blt::i32 dimensions = 0);
|
2024-11-12 20:47:18 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
void create(float gen_size, blt::i32 dimensions = 0);
|
2024-11-11 19:21:42 -05:00
|
|
|
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
void add_font(const font::font_file_t& file)
|
|
|
|
{
|
|
|
|
generator->add_font(file);
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
void add_default_font(std::string_view path, std::string_view name = "__default");
|
2024-11-12 18:26:34 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
void add_default_font(const blt::u8* data, blt::size_t size, bool compressed = false, std::string_view name = "__default");
|
2024-11-12 18:26:34 -05:00
|
|
|
|
2024-11-13 15:24:58 -05:00
|
|
|
compiled_text_t* create_text(std::string_view str, float size, std::string_view font = "__default");
|
2024-11-12 00:28:48 -05:00
|
|
|
|
2024-11-14 16:01:53 -05:00
|
|
|
render_context_t& render_text(std::string_view str, float size, std::string_view font = "__default");
|
2024-11-14 11:26:37 -05:00
|
|
|
|
2024-11-12 00:28:48 -05:00
|
|
|
void destroy_text(compiled_text_t* text);
|
2024-11-12 18:26:34 -05:00
|
|
|
|
|
|
|
void render();
|
2024-11-11 16:47:25 -05:00
|
|
|
|
2024-11-11 02:29:28 -05:00
|
|
|
private:
|
2024-11-14 11:26:37 -05:00
|
|
|
std::unique_ptr<compiled_text_t> allocate_text();
|
|
|
|
|
2024-11-11 19:21:42 -05:00
|
|
|
std::unique_ptr<font_generator_t> generator;
|
|
|
|
std::unique_ptr<shader_t> font_shader;
|
2024-11-12 00:28:48 -05:00
|
|
|
|
2024-11-14 11:26:37 -05:00
|
|
|
blt::hashmap_t<text_index_t, compiled_text_t*> rendered_strings;
|
|
|
|
blt::hashmap_t<compiled_text_t*, text_index_t> text_ptr_to_text_index;
|
2024-11-14 16:01:53 -05:00
|
|
|
blt::hashmap_t<compiled_text_t*, std::vector<render_context_t>> render_list;
|
2024-11-14 11:26:37 -05:00
|
|
|
|
|
|
|
blt::hashset_t<compiled_text_t*> last_rendered_strings;
|
|
|
|
blt::hashset_t<compiled_text_t*> currently_rendered_strings;
|
|
|
|
|
2024-11-12 00:28:48 -05:00
|
|
|
blt::hashmap_t<compiled_text_t*, blt::size_t> text_ptr_to_index;
|
|
|
|
std::vector<std::unique_ptr<compiled_text_t>> added_texts;
|
2024-11-14 11:26:37 -05:00
|
|
|
std::vector<std::unique_ptr<compiled_text_t>> cached_deallocated_text_objects;
|
2024-11-11 02:29:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:26:37 -05:00
|
|
|
namespace blt::gfx::detail
|
|
|
|
{
|
|
|
|
inline void hash_combine(std::size_t& seed, const std::size_t& value)
|
|
|
|
{
|
|
|
|
seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specialization of std::hash for text_index_t
|
|
|
|
template<>
|
|
|
|
struct std::hash<blt::gfx::font_renderer_t::text_index_t>
|
|
|
|
{
|
|
|
|
std::size_t operator()(const blt::gfx::font_renderer_t::text_index_t& key) const
|
|
|
|
{
|
|
|
|
blt::size_t hash = 0;
|
|
|
|
blt::gfx::detail::hash_combine(hash, std::hash<std::string>{}(key.text));
|
|
|
|
blt::gfx::detail::hash_combine(hash, std::hash<std::string>{}(key.font));
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-11-11 02:29:28 -05:00
|
|
|
#endif //BLT_WITH_GRAPHICS_FONT_RENDERER_H
|