Reformat code to have non GL interface

This commit is contained in:
Corentin 2022-10-03 16:06:32 +09:00
commit 9ec93964ba
34 changed files with 644 additions and 362 deletions

View file

@ -3,17 +3,34 @@
#include <memory>
#include <string>
#include "window.hpp"
#include "uui/config.hpp"
namespace uui
{
class Window;
class Application
{
public:
enum Implementaion
{
OPENGL
};
virtual void run() = 0;
// virtual std::weak_ptr<Window> add_window(int width, int height, const std::string& title) = 0;
virtual void create_window(int width, int height, const std::string& title, InitFunction<Window> init) = 0;
virtual std::shared_ptr<Window> get_window(std::size_t index [[maybe_unused]]) const = 0;
virtual ~Application() {};
protected:
static bool application_created;
static std::shared_ptr<Application> current_application;
bool initialized;
virtual void init() = 0;
virtual void deinit() = 0;
friend std::shared_ptr<Application> create_application(Application::Implementaion implementation);
};
std::shared_ptr<Application> create_application(Application::Implementaion implementation);
} // namespace uui

View file

@ -1,30 +1,43 @@
#pragma once
#include <array>
#include <variant>
#include <memory>
#include "uui/config.hpp"
#include "uui/window.hpp"
namespace uui
{
class Container;
class Component
{
public:
Component(const position_t x, const position_t y, const position_t width, const position_t height):
position_x(x), position_y(y), size_width(width), size_height(height)
{
background_color = {0.0f, 0.0f, 0.0f, 0.0f};
}
virtual void render() = 0;
virtual void set_background_color(float r, float g, float b, float a = 1.0f) = 0;
protected:
position_t position_x;
position_t position_y;
position_t size_width;
position_t size_height;
#ifdef DEBUG
constexpr static bool debug_mode = true;
#else
constexpr static bool debug_mode = false;
#endif
std::shared_ptr<Window> window;
std::size_t window_index;
std::shared_ptr<Container> parent;
std::size_t parent_index;
uui::position_t position_x;
uui::position_t position_y;
uui::position_t size_width;
uui::position_t size_height;
bool coord_all_relative;
std::array<float, 4> background_color;
bool initialized;
virtual void render() = 0;
};
} // namespace uui

View file

@ -1,16 +1,11 @@
#pragma once
#include <functional>
#include <variant>
#include "graphic_context.hpp"
namespace Config
{
constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0;
}
namespace uui
{
template<typename T> using InitFunction = const std::function<void(T&)>&;
typedef std::variant<int, float> position_t;
typedef std::variant<int, float> size_t;
enum Direction

40
include/uui/container.hpp Normal file
View file

@ -0,0 +1,40 @@
#pragma once
#include <cstdlib>
#include <iostream>
#include <memory>
#include <vector>
#include "uui/config.hpp"
namespace uui
{
class Component;
class GlComponent;
class Container
{
friend class GlComponent;
public:
enum Type
{
WINDOW,
FLEX,
STACK
};
Type type;
protected:
#ifdef DEBUG
constexpr static bool debug_mode = true;
#else
constexpr static bool debug_mode = false;
#endif
virtual std::size_t push_child(std::shared_ptr<Component> child) = 0;
virtual std::shared_ptr<Component> get_child(std::size_t index) const = 0;
virtual void remove_child(std::size_t index) = 0;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const = 0;
};
} // namespace uui

16
include/uui/flex.hpp Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include <string>
#include <tuple>
#include "uui/stack.hpp"
namespace uui
{
class Flex: virtual public Stack
{
protected:
virtual std::tuple<int, int> get_child_position(const std::size_t child_index) const = 0;
};
} // namespace uui

View file

@ -0,0 +1,56 @@
#pragma once
#include <string>
#include <tuple>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
namespace uui
{
class Window;
class FontManager
{
friend class Window;
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];
unsigned int atlas_texture;
unsigned int atlas_width;
unsigned int atlas_height;
};
std::vector<Font> fonts;
virtual 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) = 0;
virtual std::tuple<int, int> get_text_size(const std::string& text, Font& font, float scale_x, float scale_y) = 0;
protected:
static constexpr int ATLAS_MAX_WIDTH = 1024;
bool initialized;
FT_Library ft;
virtual const Font& init_font(const std::string& font_path, std::size_t font_size) = 0;
};
} // namespace uui

22
include/uui/label.hpp Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <string>
#include <tuple>
#include "uui/component.hpp"
namespace uui
{
class Label: virtual public Component
{
protected:
std::size_t font_index;
std::string text;
std::tuple<float, float, float, float> text_color;
int text_width;
int text_height;
};
} // namespace uui

View file

@ -1,21 +1,17 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <variant>
#include <vector>
#include "graphic_context.hpp"
#include "uui/application.hpp"
#include "uui/config.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/font_manager.hpp"
namespace uui
{
template<typename T> using InitFunction = const std::function<void(T&)>&;
class GlWindow;
class GlApplication
class GlApplication final: virtual public Application
{
friend class GlWindow;
@ -28,69 +24,15 @@ public:
std::vector<std::shared_ptr<GlWindow>> windows;
GlApplication();
~GlApplication();
virtual ~GlApplication() final;
void run();
void create_window(int width, int height, const std::string& title, InitFunction<GlWindow> init);
std::shared_ptr<GlWindow> get_window(std::size_t index) const;
private:
static GlApplication* application_pointer;
};
class GlComponent;
class GlFlex;
class GlLabel;
class GlStack;
class GlWindow: public GlContainer, public std::enable_shared_from_this<GlWindow>
{
friend class GlApplication;
friend class GlComponent;
friend class GlFlex;
friend class GlLabel;
friend class GlStack;
public:
std::string title;
GlWindow(const GlWindow&) = delete;
GlWindow(GlWindow&&) = delete;
~GlWindow();
void create_component(
const position_t x, const position_t y, const position_t width, const position_t height,
InitFunction<GlComponent> init);
void create_label(
const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, InitFunction<GlLabel> init);
void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction<GlFlex> init);
void create_stack(const position_t x, const position_t y, const Direction direction, InitFunction<GlStack> init);
void run() final;
void create_window(int width, int height, const std::string& title, InitFunction<Window> init) final;
std::shared_ptr<Window> get_window(std::size_t index) const final;
protected:
GlApplication* app;
bool initialized;
int width;
int height;
bool render_needed;
bool resize_needed;
bool iconified;
GlFontManager font_manager;
GLFWwindow* glfw_window;
GLuint gl_program;
GlWindow(GlApplication* app, int width, int height, const std::string& title);
std::shared_ptr<GlWindow> getptr() { return shared_from_this(); }
void init();
void deinit();
void render();
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);
void init() final;
void deinit() final;
};
} // namespace uui

View file

@ -4,17 +4,19 @@
#include <memory>
#include "graphic_context.hpp"
#include "uui/opengl/component.hpp"
#include "uui/container.hpp"
#include "uui/config.hpp"
#include "uui/opengl/container.hpp"
#include "uui/style.hpp"
#include "window.hpp"
#include "uui/opengl/window.hpp"
namespace uui
{
class GlFlex;
class GlStack;
class GlComponent: public std::enable_shared_from_this<GlComponent>
class GlComponent: public std::enable_shared_from_this<GlComponent>, virtual public Component
{
friend class Container;
friend class GlContainer;
friend class GlWindow;
friend class GlFlex;
friend class GlStack;
@ -22,25 +24,10 @@ class GlComponent: public std::enable_shared_from_this<GlComponent>
public:
virtual ~GlComponent();
void set_background_color(float r, float g, float b, float a = 1.0f);
void set_background_color(float r, float g, float b, float a = 1.0f) final;
protected:
std::shared_ptr<GlWindow> window;
std::size_t window_index;
std::shared_ptr<GlContainer> parent;
std::size_t parent_index;
position_t position_x;
position_t position_y;
size_t size_width;
size_t size_height;
bool coord_all_relative;
Style style;
std::array<float, 4> background_color;
bool initialized;
std::shared_ptr<GlWindow> gl_window;
GLuint gl_vbo;
GLuint gl_vao;
GLuint gl_ebo;
@ -52,16 +39,17 @@ protected:
GlComponent(
std::shared_ptr<GlWindow> window, std::size_t window_index, const position_t x, const position_t y,
const position_t width, const position_t height):
GlComponent(window, window_index, window, window_index, x, y, width, height)
GlComponent(window, window_index, std::static_pointer_cast<GlContainer>(window), window_index, x, y, width, height)
{}
GlComponent(const GlComponent&) = delete;
GlComponent(GlComponent&&) = default;
std::shared_ptr<GlComponent> getptr() { return shared_from_this(); }
virtual void render();
virtual void init();
virtual void set_vbo_data();
virtual void deinit();
virtual void render();
virtual void on_resize();
};

View file

@ -6,51 +6,29 @@
#include <memory>
#include <vector>
#include "uui/container.hpp"
#include "uui/component.hpp"
namespace uui
{
class GlComponent;
class GlFlex;
class GlContainer
class GlContainer: virtual public Container
{
friend class GlComponent;
friend class GlFlex;
protected:
GlContainer() = default;
GlContainer(const GlContainer&) = default;
GlContainer(GlContainer&&) = default;
virtual std::size_t push_child(std::shared_ptr<GlComponent> child)
{
components.push_back(child);
return components.size() - 1;
}
virtual std::shared_ptr<GlComponent> get_child(std::size_t index) const { return components.at(index); }
virtual void remove_child(std::size_t index) { components.at(index) = nullptr; };
virtual std::size_t push_child(std::shared_ptr<Component> child [[maybe_unused]]) final;
std::size_t push_child(std::shared_ptr<GlComponent> child);
std::shared_ptr<Component> get_child(std::size_t index) const final;
virtual void remove_child(std::size_t index) final;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const override;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index) const { return {0, 0}; }
std::vector<std::shared_ptr<GlComponent>> components;
public:
#ifdef DEBUG
constexpr static bool debug_mode = true;
#else
constexpr static bool debug_mode = false;
#endif
enum Type
{
WINDOW,
FLEX,
STACK
};
std::vector<std::shared_ptr<GlComponent>> components;
Type type;
virtual ~GlContainer()
{
if constexpr(debug_mode)
std::cout << "Destroying Container" << std::endl;
components.clear();
}
~GlContainer();
};
} // namespace uui

View file

@ -4,14 +4,14 @@
#include <string>
#include <tuple>
#include "uui/opengl/component.hpp"
#include "uui/flex.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/label.hpp"
#include "uui/opengl/stack.hpp"
#include "uui/opengl/window.hpp"
namespace uui
{
class GlFlex: public GlStack
class GlFlex: virtual public GlStack, virtual public Flex
{
friend class GlWindow;

View file

@ -7,56 +7,27 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#include "graphic_context.hpp"
#include "uui/font_manager.hpp"
#include "uui/opengl/graphic_context.hpp"
namespace uui
{
class GlWindow;
class GlFontManager
class GlFontManager final: public FontManager
{
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 atlas_texture;
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);
const std::tuple<float, float, float, float>& text_color) final;
std::tuple<int, int> get_text_size(const std::string& text, Font& font, float scale_x, float scale_y) final;
private:
static constexpr int ATLAS_MAX_WIDTH = 1024;
bool initialized;
FT_Library ft;
GLuint gl_program;
GLuint gl_vao;
GLuint gl_vbo;

View file

@ -11,3 +11,8 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// clang-format off
namespace Config
{
constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0;
}

View file

@ -3,6 +3,7 @@
#include <string>
#include <tuple>
#include "uui/label.hpp"
#include "uui/opengl/component.hpp"
#include "uui/opengl/container.hpp"
@ -10,17 +11,12 @@ namespace uui
{
class GlFlex;
class GlStack;
class GlLabel: public GlComponent
class GlLabel final: virtual public GlComponent, virtual public Label
{
friend class GlWindow;
friend class GlFlex;
friend class GlStack;
std::size_t font_index;
float get_width() const;
float get_height() const;
protected:
std::string text;
std::tuple<float, float, float, float> text_color;
@ -38,7 +34,7 @@ protected:
GlLabel(window, window_index, window, window_index, x, y, text, text_color)
{}
void render();
void render() final;
};
} // namespace uui

View file

@ -6,24 +6,21 @@
#include "uui/opengl/component.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/label.hpp"
#include "uui/stack.hpp"
namespace uui
{
class GlStack: public GlComponent, public GlContainer
class GlStack: virtual public GlComponent, virtual public GlContainer, virtual public Stack
{
friend class GlWindow;
public:
void create_component(const position_t width, const position_t height, InitFunction<GlComponent> init);
void create_component(const position_t width, const position_t height, InitFunction<Component> init) override;
void create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color, InitFunction<GlLabel> init);
const std::string& text, const std::tuple<float, float, float, float>& text_color,
InitFunction<Label> init) override;
protected:
Direction direction;
int content_width;
int content_height;
GlStack(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction);
@ -33,10 +30,7 @@ protected:
GlStack(window, window_index, window, window_index, x, y, direction)
{}
float get_width() const;
float get_height() const;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index) const;
std::tuple<int, int> get_child_position(const std::size_t child_index) const override;
};
} // namespace uui

View file

@ -1,6 +0,0 @@
#pragma once
#include "uui/opengl/application.hpp"
#include "uui/opengl/component.hpp"
#include "uui/opengl/flex.hpp"
#include "uui/opengl/label.hpp"

View file

@ -1,3 +1,57 @@
#pragma once
#include "uui/config.hpp"
#include "uui/opengl/application.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/font_manager.hpp"
#include "uui/window.hpp"
namespace uui
{
class GlComponent;
class GlContainer;
class GlFlex;
class GlLabel;
class GlStack;
class GlWindow final: public std::enable_shared_from_this<GlWindow>, virtual public Window, virtual public GlContainer
{
friend class GlApplication;
friend class GlComponent;
friend class GlFlex;
friend class GlLabel;
friend class GlStack;
public:
GlWindow(const GlWindow&) = delete;
GlWindow(GlWindow&&) = delete;
~GlWindow();
void create_component(
const position_t x, const position_t y, const position_t width, const position_t height,
InitFunction<Component> init) final;
void create_label(
const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, InitFunction<Label> init) final;
void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction<Flex> init) final;
void create_stack(
const position_t x, const position_t y, const Direction direction, InitFunction<Stack> init) final;
protected:
void render() final;
std::shared_ptr<GlFontManager> gl_font_manager;
GLFWwindow* glfw_window;
GLuint gl_program;
GlWindow(std::shared_ptr<Application> app, int width, int height, const std::string& title);
std::shared_ptr<GlWindow> getptr() { return shared_from_this(); }
void init();
void deinit();
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

27
include/uui/stack.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <string>
#include <tuple>
#include "uui/component.hpp"
#include "uui/label.hpp"
namespace uui
{
class Stack: virtual public Component
{
public:
virtual void create_component(const position_t width, const position_t height, InitFunction<Component> init) = 0;
virtual void create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color,
InitFunction<Label> init) = 0;
protected:
Direction direction;
int content_width;
int content_height;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index) const = 0;
};
} // namespace uui

9
include/uui/uui.hpp Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#include "uui/application.hpp"
#include "uui/config.hpp"
#include "uui/component.hpp"
#include "uui/flex.hpp"
#include "uui/label.hpp"
#include "uui/stack.hpp"
#include "uui/window.hpp"

View file

@ -4,21 +4,56 @@
#include <string>
#include <vector>
#include "graphic_context.hpp"
#include "uui/component.hpp"
#include "uui/application.hpp"
#include "uui/config.hpp"
#include "uui/container.hpp"
#include "uui/font_manager.hpp"
namespace uui
{
class Window
class Component;
class Label;
class Flex;
class Stack;
class Window: virtual public Container
{
friend class Component;
public:
std::string title;
virtual void add_component(const Component& box) = 0;
std::shared_ptr<FontManager> font_manager;
virtual void create_component(
const position_t x, const position_t y, const position_t width, const position_t height,
InitFunction<Component> init) = 0;
virtual void create_label(
const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, InitFunction<Label> init) = 0;
virtual void create_flex(
const position_t x, const position_t y, const Direction direction, InitFunction<Flex> init) = 0;
virtual void create_stack(
const position_t x, const position_t y, const Direction direction, InitFunction<Stack> init) = 0;
int width() { return _width; };
int height() { return _height; };
protected:
GLFWwindow* glfw_window;
#ifdef DEBUG
constexpr static bool debug_mode = true;
#else
constexpr static bool debug_mode = false;
#endif
virtual void draw() = 0;
std::shared_ptr<Application> _app;
bool _initialized;
int _width;
int _height;
bool _render_needed;
bool _resize_needed;
bool _iconified;
virtual void render() = 0;
};
} // namespace uui