Working FontManager and Label implementation (WIP)

This commit is contained in:
Corentin 2021-07-12 17:30:44 +09:00
commit af6967151e
15 changed files with 608 additions and 61 deletions

View file

@ -7,6 +7,7 @@
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// clang-format off

View file

@ -6,6 +6,7 @@
#include <vector>
#include "graphic_context.hpp"
#include "uui/opengl/font_manager.hpp"
namespace uui
{
@ -33,11 +34,13 @@ private:
static GlApplication* application_pointer;
};
class GlLabel;
class GlComponent;
class GlWindow
{
friend class GlApplication;
friend class GlComponent;
friend class GlLabel;
public:
std::string title;
@ -50,14 +53,21 @@ public:
std::shared_ptr<GlComponent> create_component(
const std::variant<int, float> x, const std::variant<int, float> y, const std::variant<int, float> width,
const std::variant<int, float> height);
std::shared_ptr<GlLabel> create_label(
const std::variant<int, float> x, const std::variant<int, float> y, const std::string& text,
const std::tuple<float, float, float, float>& text_color);
private:
protected:
GlApplication* app;
GlFontManager font_manager;
int width;
int height;
bool need_resize_refresh;
bool iconified;
size_t index;
bool initialized;
@ -72,6 +82,8 @@ private:
void draw();
static void glfw_resize_callback(GLFWwindow* window, int width, int height);
static void glfw_iconify_callback(GLFWwindow* window, int iconified);
static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
};
} // namespace uui

View file

@ -1,7 +1,7 @@
#pragma once
#include <array>
// #include <memory>
#include <memory>
#include <variant>
#include "graphic_context.hpp"
@ -20,7 +20,7 @@ public:
void set_background_color(float r, float g, float b, float a = 0.0f);
private:
protected:
std::shared_ptr<GlWindow> window;
std::variant<int, float> position_x;
@ -41,11 +41,11 @@ private:
std::shared_ptr<GlWindow> window, const std::variant<int, float> x, const std::variant<int, float> y,
const std::variant<int, float> width, const std::variant<int, float> height);
void init();
void set_vbo_data();
void deinit();
void render();
void on_resize();
virtual void init();
virtual void set_vbo_data();
virtual void deinit();
virtual void render();
virtual void on_resize();
};
} // namespace uui

View file

@ -0,0 +1,68 @@
#pragma once
#include <array>
#include <string>
#include <tuple>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "graphic_context.hpp"
namespace uui
{
class GlWindow;
class GlFontManager
{
friend class GlWindow;
public:
struct character_info
{
float advance_x; // advance.x
float advance_y; // advance.y
float bitmap_width; // bitmap.width;
float bitmap_height; // bitmap.rows;
float bitmap_left; // bitmap_left;
float bitmap_top; // bitmap_top;
float texture_x; // x offset of glyph in texture coordinates
float texture_y; // y offset of glyph in texture coordinates
};
struct Font
{
character_info char_infos[256];
GLuint tex;
unsigned int atlas_width;
unsigned int atlas_height;
};
std::vector<Font> fonts;
GlFontManager();
~GlFontManager();
void render_text(
const std::string& text, Font& font, float x, float y, float scale_x, float scale_y,
const std::tuple<float, float, float, float>& text_color);
std::tuple<int, int> get_text_size(const std::string& text, Font& font, float scale_x, float scale_y);
private:
static constexpr int MAX_WIDTH = 1024;
bool initialized;
FT_Library ft;
GLuint gl_program;
GLuint gl_vao;
GLuint gl_vbo;
void init();
void deinit();
const Font& init_font(const std::string& font_path, size_t font_size);
};
} // namespace uui

View file

@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <tuple>
#include "uui/opengl/component.hpp"
namespace uui
{
class GlLabel: public GlComponent
{
friend class GlWindow;
size_t font_index;
float get_width() const;
float get_height() const;
protected:
std::string text;
const std::tuple<float, float, float, float>& text_color;
int text_width;
int text_height;
GlLabel(
std::shared_ptr<GlWindow> window, const std::variant<int, float> x, const std::variant<int, float> y, const std::string& text,
const std::tuple<float, float, float, float>& text_color);
void render();
};
} // namespace uui