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

View file

@ -20,7 +20,7 @@ class Config:
COMMON_FLAGS = '-std=c++17 -fno-semantic-interposition'
COMMON_DEBUG_FLAGS = '-g -Og -DDEBUG'
COMMON_RELEASE_FLAGS = '-O2 -flto'
COMMON_RELEASE_FLAGS = '-O2 -flto=auto'
COMPILE_FLAGS = f'-Wall -Wextra -Wpedantic -Wconversion -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`'
LINK_FLAGS = '-lpthread -ldl `pkg-config --libs glfw3 vulkan gl x11 freetype2`'

View file

@ -1,6 +1,6 @@
#include <iostream>
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
using namespace std;
@ -8,12 +8,12 @@ int main()
{
try
{
uui::GlApplication app;
// uui::GlApplication app2; // exception : application already created
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
auto app = uui::create_application(uui::Application::OPENGL);
// auto app2 = uui::create_application(uui::Application::OPENGL); // exception : application already created
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
for(int i = 0; i < 100; i += 1) window.create_label(4 * i, 4 * i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, {});
});
app.run();
app->run();
}
catch(const std::exception& error)
{

View file

@ -1,6 +1,6 @@
#include <iostream>
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
using namespace std;
@ -10,92 +10,92 @@ int main()
{
cout << "Starting example 1" << endl;
{
uui::GlApplication app;
// uui::GlApplication app2; // exception : application already created
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
window.create_component(0, 100, 0.5f, 50, [](uui::GlComponent& component) {
auto app = uui::create_application(uui::Application::OPENGL);
// auto app2 = uui::create_application(uui::Application::OPENGL); // exception : application already created
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
window.create_component(0, 100, 0.5f, 50, [](uui::Component& component) {
component.set_background_color(0.8f, 0.1f, 0.5f);
});
window.create_component(0.25f, 50, 0.4f, 150, [](uui::GlComponent& component) {
window.create_component(0.25f, 50, 0.4f, 150, [](uui::Component& component) {
component.set_background_color(0.3f, 0.8f, 0.4f, 0.5f);
});
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) {
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::Label& label) {
label.set_background_color(0.1f, 0.2f, 0.5f, 0.8f);
});
window.create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, [](uui::GlLabel& label) {
window.create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, [](uui::Label& label) {
label.set_background_color(0.5f, 0.5f, 0.1f);
});
window.create_label(0.75f, 0.75f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.1f}, {});
window.create_flex(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlFlex& flex) {
flex.create_component(100, 50, [](uui::GlComponent& component) {
window.create_flex(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::Flex& flex) {
flex.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
flex.create_component(100, 20, [](uui::GlComponent& component) {
flex.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
window.create_flex(50, 400, uui::Direction::VERTICAL, [](uui::GlFlex& flex) {
flex.create_component(50, 100, [](uui::GlComponent& component) {
window.create_flex(50, 400, uui::Direction::VERTICAL, [](uui::Flex& flex) {
flex.create_component(50, 100, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
flex.create_component(100, 20, [](uui::GlComponent& component) {
flex.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
});
app.create_window(800, 600, "OpenGl 2", [](uui::GlWindow& window) {
window.create_stack(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlStack& stack) {
stack.create_component(100, 50, [](uui::GlComponent& component) {
app->create_window(800, 600, "OpenGl 2", [](uui::Window& window) {
window.create_stack(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::Stack& stack) {
stack.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
stack.create_component(100, 20, [](uui::GlComponent& component) {
stack.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
window.create_stack(50, 400, uui::Direction::VERTICAL, [](uui::GlStack& stack) {
stack.create_component(100, 50, [](uui::GlComponent& component) {
window.create_stack(50, 400, uui::Direction::VERTICAL, [](uui::Stack& stack) {
stack.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
stack.create_component(100, 20, [](uui::GlComponent& component) {
stack.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
});
app.run();
app->run();
}
cout << "\nStarting example 2" << endl;
{
uui::GlApplication app;
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::GlComponent& component) {
auto app = uui::create_application(uui::Application::OPENGL);
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::Component& component) {
component.set_background_color(0.8f, 0.1f, 0.5f);
});
window.create_component(0.75f, 0.75f, 0.25f, 0.25f, [](uui::GlComponent& component) {
window.create_component(0.75f, 0.75f, 0.25f, 0.25f, [](uui::Component& component) {
component.set_background_color(0.2f, 0.1f, 0.5f);
});
});
app.create_window(600, 600, "OpenGL 2!", [](uui::GlWindow& window) {
window.create_component(0, 0.5f, 100, 0.5f, [](uui::GlComponent& component) {
app->create_window(600, 600, "OpenGL 2!", [](uui::Window& window) {
window.create_component(0, 0.5f, 100, 0.5f, [](uui::Component& component) {
component.set_background_color(0.2f, 0.5f, 0.3f);
});
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) {
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::Label& label) {
label.set_background_color(0.1f, 0.2f, 0.5f);
});
});
app.run();
app->run();
}
}
catch(const std::exception& error)

View file

@ -1,14 +1,26 @@
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
int main()
{
uui::GlApplication app;
app.create_window(800, 600, "UUI sample", [](uui::GlWindow& window) {
auto app = uui::create_application(uui::Application::OPENGL);
app->create_window(800, 600, "UUI sample", [](uui::Window& window) {
window.create_component(
0, 0, 200, 100, [](uui::GlComponent& component) { component.set_background_color(1.0f, 0.f, 0.f, 0.5f); });
0, 0, 200, 100, [](uui::Component& component) { component.set_background_color(1.0f, 0.f, 0.f, 0.5f); });
window.create_label(0.5f, 0.5f, "Hello world!", {0.8f, 0.8f, 0.8f, 1.0f}, nullptr);
});
app.run();
app->run();
// app->run();
// app = uui::create_application(uui::Application::OPENGL);
// app->create_window(800, 600, "test", [](uui::Window& window) {
// window.create_flex(10, 10, uui::HORIZONTAL, [](uui::Flex& flex) {
// flex.create_label("Hello!", {0.5f, 0.f, 0.f, 1.0f}, [](uui::Label& label) {
// label.set_background_color(0.2f, 0.2f, 0.6f, 1.0f);
// });
// flex.create_label("World!", {1.0, 1.0, 1.0, 1.0}, {});
// });
// });
// app->run();
return 0;
}

View file

@ -1,3 +1,20 @@
#include "uui/application.hpp"
bool uui::Application::application_created = false;
#include <stdexcept>
#include "uui/opengl/application.hpp"
std::shared_ptr<uui::Application> uui::Application::current_application;
std::shared_ptr<uui::Application> uui::create_application(uui::Application::Implementaion implementation)
{
switch(implementation)
{
case uui::Application::Implementaion::OPENGL:
if(uui::Application::current_application != nullptr)
throw std::runtime_error("Application error: the application already created");
std::shared_ptr<uui::Application> new_application(new uui::GlApplication());
uui::Application::current_application = new_application;
return new_application;
}
}

View file

@ -3,16 +3,32 @@
#include <chrono>
#include <iostream>
uui::GlApplication* uui::GlApplication::application_pointer = nullptr;
#include "uui/opengl/window.hpp"
uui::GlApplication::GlApplication()
{
if(application_pointer != nullptr)
throw std::runtime_error("GlApplication error: the application already created");
if constexpr(debug_mode)
std::cout << "Creating the GlApplication in debug mode" << std::endl;
else
std::cout << "Creating the GlApplication in release mode" << std::endl;
initialized = false;
init();
}
uui::GlApplication::~GlApplication()
{
if constexpr(debug_mode)
std::cout << "Destroying the GlApplication" << std::endl;
if(initialized)
deinit();
}
void uui::GlApplication::init()
{
if constexpr(debug_mode)
std::cout << "Initialize the GlApplication" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
@ -23,22 +39,24 @@ uui::GlApplication::GlApplication()
if constexpr(uui::GlApplication::debug_mode)
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
application_pointer = this;
initialized = true;
}
uui::GlApplication::~GlApplication()
void uui::GlApplication::deinit()
{
if constexpr(debug_mode)
std::cout << "Destroying the GlApplication" << std::endl;
std::cout << "Deinit the GlApplication" << std::endl;
windows.clear();
glfwTerminate();
application_pointer = nullptr;
initialized = false;
}
void uui::GlApplication::run()
{
if constexpr(debug_mode)
std::cout << "Starting the GlApplication" << std::endl;
if(!initialized)
init();
auto start_time = std::chrono::high_resolution_clock::now();
auto end_time = start_time;
@ -75,6 +93,8 @@ void uui::GlApplication::run()
glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window);
glfwSwapInterval(1);
}
if constexpr(debug_mode)
std::cout << "\nGlApplication closing GlWindow : " << window->title << std::endl;
window->deinit();
windows.erase(it);
it -= 1;
@ -85,7 +105,7 @@ void uui::GlApplication::run()
window->render();
else
{
if(window->render_needed && !window->iconified)
if(window->_render_needed && !window->_iconified)
window->render();
}
}
@ -95,12 +115,12 @@ void uui::GlApplication::run()
}
if constexpr(debug_mode)
std::cout << "Ending the GlApplication" << std::endl;
current_application = nullptr;
}
void uui::GlApplication::create_window(
int width, int height, const std::string& title, uui::InitFunction<GlWindow> init)
void uui::GlApplication::create_window(int width, int height, const std::string& title, uui::InitFunction<Window> init)
{
std::shared_ptr<uui::GlWindow> window(new uui::GlWindow(this, width, height, title));
auto window = std::shared_ptr<uui::GlWindow>(new uui::GlWindow(current_application, width, height, title));
window->init();
windows.push_back(window);
glfwSwapInterval(1);
@ -114,7 +134,7 @@ void uui::GlApplication::create_window(
init(*window);
}
std::shared_ptr<uui::GlWindow> uui::GlApplication::get_window(std::size_t index) const
std::shared_ptr<uui::Window> uui::GlApplication::get_window(std::size_t index) const
{
if(index > windows.size())
throw std::runtime_error("Application doesn't have window at given index");

View file

@ -3,17 +3,30 @@
#include <iostream>
#include "uui/opengl/application.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/gl_utils.hpp"
uui::GlComponent::GlComponent(
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 position_t width, const position_t height):
window(window),
window_index(window_index), parent(parent), parent_index(parent_index), position_x(x), position_y(y),
size_width(width), size_height(height), initialized(false)
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const position_t width, const position_t height)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlBox" << std::endl;
if constexpr(debug_mode)
std::cout << "Creating a GlComponent " << parent_index << std::endl;
gl_window = window;
this->window = std::static_pointer_cast<uui::Window>(window);
this->window_index = window_index;
this->parent = parent;
this->parent_index = parent_index;
this->parent_index = parent_index;
initialized = false;
position_x = x;
position_y = y;
size_width = width;
size_height = height;
coord_all_relative = true;
auto parse_coords = [&](position_t& value) {
@ -36,7 +49,7 @@ uui::GlComponent::GlComponent(
uui::GlComponent::~GlComponent()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Closing a GlComponent" << std::endl;
std::cout << "Destructor GlComponent " << parent_index << std::endl;
if(initialized)
deinit();
// if(parent != window)
@ -54,7 +67,7 @@ void uui::GlComponent::set_background_color(float r, float g, float b, float a /
void uui::GlComponent::init()
{
if(initialized)
throw std::runtime_error("GlBox error : calling init while already initialized");
throw std::runtime_error("GlComponent error : calling init while already initialized");
glGenVertexArrays(1, &gl_vao);
glGenBuffers(1, &gl_vbo);
@ -88,16 +101,16 @@ void uui::GlComponent::set_vbo_data()
{
float vert_x = position_x.index() == 1
? std::get<1>(position_x)
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width);
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width());
float vert_y = position_y.index() == 1
? std::get<1>(position_y)
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height);
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height());
float vert_w = size_width.index() == 1
? std::get<1>(size_width)
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width);
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width());
float vert_h = size_height.index() == 1
? std::get<1>(size_height)
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height);
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height());
float x_min = (2.0f * vert_x) - 1.0f;
float x_max = x_min + (2.0f * vert_w);
float y_min = (-2.0f * vert_y) + 1.0f;
@ -136,12 +149,18 @@ void uui::GlComponent::set_vbo_data()
void uui::GlComponent::deinit()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlComponent " << parent_index << std::endl;
if(!initialized)
throw std::runtime_error("GlBox error : calling deinit while not initialized");
throw std::runtime_error("GlComponent error : calling deinit while not initialized");
glDeleteVertexArrays(1, &gl_vao);
glDeleteBuffers(1, &gl_vbo);
glDeleteBuffers(1, &gl_ebo);
window = nullptr;
parent = nullptr;
initialized = false;
}

View file

@ -0,0 +1,37 @@
#include "uui/opengl/container.hpp"
#include "uui/opengl/component.hpp"
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::Component> child [[maybe_unused]])
{
throw std::runtime_error("GlContainer error : should only push GlComponent child, not Component");
}
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::GlComponent> child)
{
components.push_back(child);
// components.push_back(std::static_pointer_cast<uui::Component>(child));
return components.size() - 1;
}
std::shared_ptr<uui::Component> uui::GlContainer::get_child(std::size_t index) const
{
return components.at(index);
}
void uui::GlContainer::remove_child(std::size_t index)
{
components.at(index) = nullptr;
};
std::tuple<int, int> uui::GlContainer::get_child_position(const std::size_t child_index [[maybe_unused]]) const
{
return {0, 0};
}
uui::GlContainer::~GlContainer()
{
if constexpr(debug_mode)
std::cout << "Destroying GlContainer" << std::endl;
components.clear();
}

View file

@ -7,10 +7,11 @@
uui::GlFlex::GlFlex(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
GlStack(window, window_index, parent, parent_index, x, y, direction)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
std::cout << "Creating a GlFlex " << parent_index << std::endl;
type = Type::FLEX;
coord_all_relative = false;
@ -20,10 +21,10 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
{
const int initial_x = position_x.index() == 0
? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width()));
const int initial_y = position_y.index() == 0
? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height()));
if(child_index == 0)
return {initial_x, initial_y};
@ -37,11 +38,11 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
{
auto& component = components[index];
if(component->size_width.index() == 1)
width = static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width));
width = static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width()));
else
width = std::get<0>(component->size_width);
if(component->size_height.index() == 1)
height = static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height));
height = static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height()));
else
height = std::get<0>(component->size_height);
}
@ -64,7 +65,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_height > max_height)
max_height = child_height;
if(x != initial_x && x + child_width > window->width)
if(x != initial_x && x + child_width > window->width())
{
x = initial_x + child_width;
y += max_height;
@ -76,7 +77,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(x != initial_x && x + child_width > window->width)
if(x != initial_x && x + child_width > window->width())
{
x = initial_x;
y += max_height;
@ -98,7 +99,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_width > max_width)
max_width = child_width;
if(y != initial_y && y + child_height > window->height)
if(y != initial_y && y + child_height > window->height())
{
y = initial_y + child_height;
x += max_width;
@ -110,7 +111,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(y != initial_y && y + child_height > window->height)
if(y != initial_y && y + child_height > window->height())
{
y = initial_y;
x += max_width;

View file

@ -11,7 +11,10 @@
#include "uui/opengl/gl_utils.hpp"
#include "uui/shader.hpp"
uui::GlFontManager::GlFontManager(): initialized(false) {}
uui::GlFontManager::GlFontManager()
{
initialized = false;
}
uui::GlFontManager::~GlFontManager()
{
@ -25,7 +28,7 @@ void uui::GlFontManager::init()
throw std::runtime_error("GlFontManager error : calling init while initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Init glFontManager" << std::endl;
std::cout << "Init GlFontManager" << std::endl;
if(FT_Init_FreeType(&ft))
throw std::runtime_error("ERROR::FREETYPE: Could not init FreeType Library");
@ -117,6 +120,9 @@ void uui::GlFontManager::deinit()
if(!initialized)
throw std::runtime_error("GlFontManager error : calling deinit while not initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlFontManager" << std::endl;
for(const auto& font: fonts) glDeleteTextures(1, &font.atlas_texture);
glDeleteBuffers(1, &gl_vbo);
glDeleteBuffers(1, &gl_vao);

View file

@ -2,6 +2,7 @@
#include <iostream>
#include "uui/opengl/font_manager.hpp"
#include "uui/opengl/gl_utils.hpp"
uui::GlLabel::GlLabel(
@ -11,12 +12,12 @@ uui::GlLabel::GlLabel(
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
text(text), text_color(text_color)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlLabel" << std::endl;
if constexpr(debug_mode)
std::cout << "Creating GlLabel " << parent_index << std::endl;
font_index = 0;
std::tie(text_width, text_height) =
window->font_manager.get_text_size(text, window->font_manager.fonts[font_index], 1.0f, 1.0f);
window->font_manager->get_text_size(text, window->font_manager->fonts[font_index], 1.0f, 1.0f);
size_width = text_width;
size_height = text_height;
coord_all_relative = false;
@ -24,15 +25,15 @@ uui::GlLabel::GlLabel(
void uui::GlLabel::render()
{
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width)
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width())
: static_cast<float>(std::get<0>(position_x));
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height)
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height())
: static_cast<float>(std::get<0>(position_y));
text_y += static_cast<float>(text_height) - 1;
glBindVertexArray(gl_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
window->font_manager.render_text(
text.c_str(), window->font_manager.fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
window->font_manager->render_text(
text.c_str(), window->font_manager->fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
glBindVertexArray(0);
}

View file

@ -3,34 +3,36 @@
#include <iostream>
#include "uui/opengl/gl_utils.hpp"
#include "uui/opengl/label.hpp"
uui::GlStack::GlStack(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
direction(direction)
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
std::cout << "Creating GlStack " << parent_index << std::endl;
this->direction = direction;
type = Type::STACK;
coord_all_relative = false;
}
void uui::GlStack::create_component(
const uui::position_t width, const uui::position_t height, uui::InitFunction<uui::GlComponent> init)
const uui::position_t width, const uui::position_t height, uui::InitFunction<uui::Component> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
glfwMakeContextCurrent(gl_window->glfw_window);
std::shared_ptr<uui::GlComponent> component(new uui::GlComponent(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
gl_window, gl_window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
components.size(), 0, 0, width, height));
push_child(component);
component->init();
std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1);
window->components.push_back(component);
gl_window->components.push_back(component);
if(component != nullptr)
init(*component);
@ -38,19 +40,19 @@ void uui::GlStack::create_component(
void uui::GlStack::create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color,
uui::InitFunction<uui::GlLabel> init)
uui::InitFunction<uui::Label> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
glfwMakeContextCurrent(gl_window->glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
gl_window, gl_window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
components.size(), 0, 0, text, text_color));
push_child(label);
push_child(std::static_pointer_cast<uui::GlComponent>(label));
label->init();
std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1);
window->components.push_back(label);
gl_window->components.push_back(label);
if(label != nullptr && init != nullptr)
init(*label);
@ -59,9 +61,9 @@ void uui::GlStack::create_label(
std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_index) const
{
int x = position_x.index() == 0 ? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width()));
int y = position_y.index() == 0 ? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height()));
if(child_index == 0)
return {x, y};
@ -75,7 +77,7 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
{
auto& component = components[index];
if(component->size_width.index() == 1)
x += static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width));
x += static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width()));
else
x += std::get<0>(component->size_width);
}
@ -88,7 +90,7 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
{
auto& component = components[index];
if(component->size_height.index() == 1)
y += static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height));
y += static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height()));
else
y += std::get<0>(component->size_height);
}

View file

@ -6,11 +6,13 @@
#include "uui/opengl/flex.hpp"
#include "uui/opengl/gl_utils.hpp"
#include "uui/opengl/label.hpp"
#include "uui/opengl/flex.hpp"
#include "uui/opengl/stack.hpp"
#include "uui/shader.hpp"
static void APIENTRY glDebugOutput(
GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char* message,
const void* userParam)
GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length [[maybe_unused]], const char* message,
const void* userParam [[maybe_unused]])
{
// ignore non-significant error/warning codes
if(id == 131169 || id == 131185 || id == 131218 || id == 131204)
@ -59,59 +61,68 @@ void uui::GlWindow::glfw_resize_callback(GLFWwindow* window, int width, int heig
glfwMakeContextCurrent(window);
auto gl_window = reinterpret_cast<uui::GlWindow*>(glfwGetWindowUserPointer(window));
glViewport(0, 0, width, height);
gl_window->width = width;
gl_window->height = height;
gl_window->render_needed = true;
gl_window->resize_needed = true;
gl_window->_width = width;
gl_window->_height = height;
gl_window->_render_needed = true;
gl_window->_resize_needed = true;
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f);
glfwMakeContextCurrent(window);
glUseProgram(gl_window->font_manager.gl_program);
glUseProgram(gl_window->gl_font_manager->gl_program);
glUniformMatrix4fv(
glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE,
glGetUniformLocation(gl_window->gl_font_manager->gl_program, "projection"), 1, GL_FALSE,
glm::value_ptr(projection));
}
void uui::GlWindow::glfw_iconify_callback(GLFWwindow* window, int iconified)
{
auto gl_window = reinterpret_cast<uui::GlWindow*>(glfwGetWindowUserPointer(window));
gl_window->iconified = iconified;
gl_window->_iconified = iconified;
}
void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode [[maybe_unused]], int action, int mods [[maybe_unused]])
{
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
uui::GlWindow::GlWindow(uui::GlApplication* app, int width, int height, const std::string& title):
app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false),
iconified(false)
uui::GlWindow::GlWindow(std::shared_ptr<uui::Application> app, int width, int height, const std::string& title)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlWindow" << std::endl;
_app = app;
_initialized = false;
_width = width;
_height = height;
_render_needed = true;
_resize_needed = false;
_iconified = false;
if constexpr(debug_mode)
std::cout << "Creating GlWindow : " << title << std::endl;
this->title = title;
type = Type::WINDOW;
}
uui::GlWindow::~GlWindow()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Closing a GlWindow" << std::endl;
if(initialized)
if constexpr(debug_mode)
std::cout << "Destructor GlWindow : " << title << std::endl;
if(_initialized)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Destructor-deinit a GlWindow" << std::endl;
if constexpr(debug_mode)
std::cout << "Destructor-deinit GlWindow : " << title << std::endl;
deinit();
}
}
void uui::GlWindow::init()
{
if(initialized)
if(_initialized)
throw std::runtime_error("GlWindow error : calling init while already initialized");
glfw_window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
gl_font_manager = std::make_shared<uui::GlFontManager>();
font_manager = std::static_pointer_cast<uui::FontManager>(gl_font_manager);
glfw_window = glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL);
if(glfw_window == NULL)
throw std::runtime_error("GLWindow : failed to create GLFW window");
glfwMakeContextCurrent(glfw_window);
@ -207,32 +218,35 @@ void uui::GlWindow::init()
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
font_manager.init();
glUseProgram(font_manager.gl_program);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f);
gl_font_manager->init();
glUseProgram(gl_font_manager->gl_program);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(_width), static_cast<float>(_height), 0.0f);
glUniformMatrix4fv(
glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glGetUniformLocation(gl_font_manager->gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(gl_program);
initialized = true;
_initialized = true;
}
void uui::GlWindow::deinit()
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : calling deinit while not initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit a GlWindow" << std::endl;
std::cout << "Deinit GlWindow : " << title << std::endl;
glfwMakeContextCurrent(glfw_window);
glDeleteProgram(gl_program);
for(auto& component: components) component->deinit();
for(auto& gl_component: components) gl_component->deinit();
components.clear();
font_manager.deinit();
gl_font_manager->deinit();
glfwDestroyWindow(glfw_window);
initialized = false;
_app = nullptr;
font_manager = nullptr;
gl_font_manager = nullptr;
_initialized = false;
}
void uui::GlWindow::render()
@ -242,22 +256,22 @@ void uui::GlWindow::render()
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(gl_program);
if(resize_needed)
if(_resize_needed)
{
for(auto& component: components) component->on_resize();
resize_needed = false;
_resize_needed = false;
}
for(auto& component: components) component->render();
glfwSwapBuffers(glfw_window);
render_needed = false;
_render_needed = false;
}
void uui::GlWindow::create_component(
const uui::position_t x, const uui::position_t y, const uui::position_t width, const uui::position_t height,
uui::InitFunction<GlComponent> init)
uui::InitFunction<Component> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(glfw_window);
@ -272,15 +286,15 @@ void uui::GlWindow::create_component(
void uui::GlWindow::create_label(
const uui::position_t x, const uui::position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, uui::InitFunction<GlLabel> init)
const std::tuple<float, float, float, float>& text_color, uui::InitFunction<Label> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create label while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlLabel> label(
new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color));
push_child(label);
push_child(std::static_pointer_cast<uui::GlComponent>(label));
label->init();
if(label != nullptr && init != nullptr)
@ -288,14 +302,14 @@ void uui::GlWindow::create_label(
}
void uui::GlWindow::create_flex(
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<GlFlex> init)
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<Flex> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create flex while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlFlex> flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction));
push_child(flex);
push_child(std::static_pointer_cast<uui::GlComponent>(flex));
flex->init();
if(flex != nullptr && init != nullptr)
@ -303,16 +317,17 @@ void uui::GlWindow::create_flex(
}
void uui::GlWindow::create_stack(
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<GlStack> init)
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<Stack> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create stack while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlStack> stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction));
push_child(stack);
push_child(std::static_pointer_cast<uui::GlComponent>(stack));
stack->init();
if(stack != nullptr && init != nullptr)
init(*stack);
}

2
umake

@ -1 +1 @@
Subproject commit f5965cc91e7eb27d1def78113301e7ec28fad54a
Subproject commit 797d6accd2fd4fd1faa742a6ec8039b6aad8d861