From af734cda739b19997763f26746d4ebcaadee4415 Mon Sep 17 00:00:00 2001 From: Corentin Date: Sat, 1 Oct 2022 08:06:38 +0900 Subject: [PATCH] Add init function for cleaner usage --- README.md | 3 + include/uui/opengl/application.hpp | 17 ++-- include/uui/opengl/container.hpp | 8 +- include/uui/opengl/stack.hpp | 6 +- include/uui/opengl/uui.hpp | 6 ++ make.py | 6 +- src/test/benchmark_text.cpp | 9 +-- src/test/example_gl.cpp | 122 +++++++++++++++++------------ src/test/sample.cpp | 14 ++++ src/uui/opengl/application.cpp | 7 +- src/uui/opengl/stack.cpp | 14 ++-- src/uui/opengl/window.cpp | 35 ++++++--- 12 files changed, 156 insertions(+), 91 deletions(-) create mode 100644 README.md create mode 100644 include/uui/opengl/uui.hpp create mode 100644 src/test/sample.cpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3cd877 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# UUI + +Simple and efficient C++ GUI toolkit diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index 30c1c9f..bae5211 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,6 +13,7 @@ namespace uui { +template using InitFunction = const std::function&; class GlWindow; class GlApplication { @@ -29,7 +31,7 @@ public: ~GlApplication(); void run(); - std::shared_ptr create_window(int width, int height, const std::string& title); + void create_window(int width, int height, const std::string& title, InitFunction init); std::shared_ptr get_window(std::size_t index) const; private: @@ -55,13 +57,14 @@ public: GlWindow(GlWindow&&) = delete; ~GlWindow(); - std::shared_ptr create_component( - const position_t x, const position_t y, const position_t width, const position_t height); - std::shared_ptr create_label( + void create_component( + const position_t x, const position_t y, const position_t width, const position_t height, + InitFunction init); + void create_label( const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color); - std::shared_ptr create_flex(const position_t x, const position_t y, const Direction direction); - std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); + const std::tuple& text_color, InitFunction init); + void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction init); + void create_stack(const position_t x, const position_t y, const Direction direction, InitFunction init); protected: GlApplication* app; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index 4673972..0772e6d 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -31,6 +31,11 @@ protected: virtual std::tuple get_child_position(const std::size_t child_index) const { return {0, 0}; } public: + #ifdef DEBUG + constexpr static bool debug_mode = true; + #else + constexpr static bool debug_mode = false; + #endif enum Type { WINDOW, @@ -42,7 +47,8 @@ public: virtual ~GlContainer() { - std::cout << "Destroying Container" << std::endl; + if constexpr(debug_mode) + std::cout << "Destroying Container" << std::endl; components.clear(); } }; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index 9204a64..b50dd3d 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -15,9 +15,9 @@ class GlStack: public GlComponent, public GlContainer friend class GlWindow; public: - std::shared_ptr create_component(const position_t width, const position_t height); - std::shared_ptr create_label( - const std::string& text, const std::tuple& text_color); + void create_component(const position_t width, const position_t height, InitFunction init); + void create_label( + const std::string& text, const std::tuple& text_color, InitFunction init); protected: Direction direction; diff --git a/include/uui/opengl/uui.hpp b/include/uui/opengl/uui.hpp new file mode 100644 index 0000000..f579f95 --- /dev/null +++ b/include/uui/opengl/uui.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "uui/opengl/application.hpp" +#include "uui/opengl/component.hpp" +#include "uui/opengl/flex.hpp" +#include "uui/opengl/label.hpp" diff --git a/make.py b/make.py index 86b943e..2a627af 100644 --- a/make.py +++ b/make.py @@ -9,7 +9,7 @@ from umake import get_hash, make class Config: CC = 'g++' - APPS = ['test/example_gl', 'test/benchmark_text'] + APPS = ['test/example_gl', 'test/benchmark_text', 'test/sample'] IGNORE_APPS = [] JOB_COUNT = int(os.cpu_count() * 0.8) @@ -19,9 +19,9 @@ class Config: SOURCE_DIR = Path('src') COMMON_FLAGS = '-std=c++17 -fno-semantic-interposition' - COMMON_DEBUG_FLAGS = '-g -DDEBUG' + COMMON_DEBUG_FLAGS = '-g -Og -DDEBUG' COMMON_RELEASE_FLAGS = '-O2 -flto' - COMPILE_FLAGS = f'-Wall -Wconversion -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`' + 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`' PRE_COMPILE_FUNCTION = None diff --git a/src/test/benchmark_text.cpp b/src/test/benchmark_text.cpp index 1855d8c..dccd7a5 100644 --- a/src/test/benchmark_text.cpp +++ b/src/test/benchmark_text.cpp @@ -1,8 +1,6 @@ #include -#include "uui/opengl/application.hpp" -#include "uui/opengl/component.hpp" -#include "uui/opengl/label.hpp" +#include "uui/opengl/uui.hpp" using namespace std; @@ -12,8 +10,9 @@ int main() { uui::GlApplication app; // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); - 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.create_window(800, 600, "OpenGl!", [](uui::GlWindow& 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(); } catch(const std::exception& error) diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index a94fe74..169d5dd 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -1,10 +1,6 @@ #include -#include "uui/config.hpp" -#include "uui/opengl/application.hpp" -#include "uui/opengl/component.hpp" -#include "uui/opengl/flex.hpp" -#include "uui/opengl/label.hpp" +#include "uui/opengl/uui.hpp" using namespace std; @@ -16,67 +12,89 @@ int main() { uui::GlApplication app; // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); + app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) { + window.create_component(0, 100, 0.5f, 50, [](uui::GlComponent& component) { + component.set_background_color(0.8f, 0.1f, 0.5f); + }); - auto comp = window->create_component(0, 100, 0.5f, 50); - comp->set_background_color(0.8f, 0.1f, 0.5f); + window.create_component(0.25f, 50, 0.4f, 150, [](uui::GlComponent& component) { + component.set_background_color(0.3f, 0.8f, 0.4f, 0.5f); + }); - comp = window->create_component(0.25f, 50, 0.4f, 150); - comp->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) { + label.set_background_color(0.1f, 0.2f, 0.5f, 0.8f); + }); - auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - 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) { + 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}, {}); - label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); - label->set_background_color(0.5f, 0.5f, 0.1f); - label = 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) { + 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) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); - auto flex = window->create_flex(200, 0.6f, uui::Direction::HORIZONTAL); - comp = flex->create_component(100, 50); - comp->set_background_color(0.2f, 0.75f, 0.25f); - flex->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); - comp = flex->create_component(100, 20); - comp->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) { + 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) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); + }); - flex = window->create_flex(50, 400, uui::Direction::VERTICAL); - comp = flex->create_component(50, 100); - comp->set_background_color(0.2f, 0.75f, 0.25f); - flex->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); - comp = flex->create_component(100, 20); - comp->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) { + 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) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); - window = app.create_window(800, 600, "OpenGl 2"); - - auto stack = window->create_stack(200, 0.6f, uui::Direction::HORIZONTAL); - comp = stack->create_component(100, 50); - comp->set_background_color(0.2f, 0.75f, 0.25f); - stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); - comp = stack->create_component(100, 20); - comp->set_background_color(0.2f, 0.25f, 0.75f); - - stack = window->create_stack(50, 400, uui::Direction::VERTICAL); - comp = stack->create_component(50, 100); - comp->set_background_color(0.2f, 0.75f, 0.25f); - stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); - comp = stack->create_component(100, 20); - comp->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) { + 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) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); + }); app.run(); } cout << "\nStarting example 2" << endl; { uui::GlApplication app; - auto window_1 = app.create_window(800, 600, "OpenGl!"); - auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); - comp->set_background_color(0.8f, 0.1f, 0.5f); - comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); - comp->set_background_color(0.2f, 0.1f, 0.5f); + app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) { + window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::GlComponent& 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) { + component.set_background_color(0.2f, 0.1f, 0.5f); + }); + }); - auto window_2 = app.create_window(600, 600, "OpenGL 2!"); - comp = window_2->create_component(0, 0.5f, 100, 0.5f); - comp->set_background_color(0.2f, 0.5f, 0.3f); - auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - label->set_background_color(0.1f, 0.2f, 0.5f); + app.create_window(600, 600, "OpenGL 2!", [](uui::GlWindow& window) { + window.create_component(0, 0.5f, 100, 0.5f, [](uui::GlComponent& 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) { + label.set_background_color(0.1f, 0.2f, 0.5f); + }); + }); app.run(); } } diff --git a/src/test/sample.cpp b/src/test/sample.cpp new file mode 100644 index 0000000..345a3e6 --- /dev/null +++ b/src/test/sample.cpp @@ -0,0 +1,14 @@ +#include "uui/opengl/uui.hpp" + +int main() +{ + uui::GlApplication app; + app.create_window(800, 600, "UUI sample", [](uui::GlWindow& window) { + window.create_component( + 0, 0, 200, 100, [](uui::GlComponent& 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(); + + return 0; +} diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index 71a747a..b3b3178 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -97,7 +97,8 @@ void uui::GlApplication::run() std::cout << "Ending the GlApplication" << std::endl; } -std::shared_ptr uui::GlApplication::create_window(int width, int height, const std::string& title) +void uui::GlApplication::create_window( + int width, int height, const std::string& title, uui::InitFunction init) { std::shared_ptr window(new uui::GlWindow(this, width, height, title)); window->init(); @@ -108,7 +109,9 @@ std::shared_ptr uui::GlApplication::create_window(int width, int glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window); glfwSwapInterval(0); } - return window; + + if(window != nullptr && init != nullptr) + init(*window); } std::shared_ptr uui::GlApplication::get_window(std::size_t index) const diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp index 73a24ea..8ee1a83 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/stack.cpp @@ -17,7 +17,8 @@ uui::GlStack::GlStack( coord_all_relative = false; } -std::shared_ptr uui::GlStack::create_component(const position_t width, const position_t height) +void uui::GlStack::create_component( + const uui::position_t width, const uui::position_t height, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -31,11 +32,13 @@ std::shared_ptr uui::GlStack::create_component(const position_ std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1); window->components.push_back(component); - return component; + if(component != nullptr) + init(*component); } -std::shared_ptr uui::GlStack::create_label( - const std::string& text, const std::tuple& text_color) +void uui::GlStack::create_label( + const std::string& text, const std::tuple& text_color, + uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -49,7 +52,8 @@ std::shared_ptr uui::GlStack::create_label( std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1); window->components.push_back(label); - return label; + if(label != nullptr && init != nullptr) + init(*label); } std::tuple uui::GlStack::get_child_position(const std::size_t child_index) const diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index a06aa58..89c5e31 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -253,8 +253,9 @@ void uui::GlWindow::render() render_needed = false; } -std::shared_ptr uui::GlWindow::create_component( - const position_t x, const position_t y, const position_t width, const position_t height) +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 init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -264,12 +265,14 @@ std::shared_ptr uui::GlWindow::create_component( new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); push_child(component); component->init(); - return component; + + if(component != nullptr && init != nullptr) + init(*component); } -std::shared_ptr uui::GlWindow::create_label( - const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color) +void uui::GlWindow::create_label( + const uui::position_t x, const uui::position_t y, const std::string& text, + const std::tuple& text_color, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create label while not initialized"); @@ -279,11 +282,13 @@ std::shared_ptr uui::GlWindow::create_label( new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); push_child(label); label->init(); - return label; + + if(label != nullptr && init != nullptr) + init(*label); } -std::shared_ptr uui::GlWindow::create_flex( - const position_t x, const position_t y, const Direction direction) +void uui::GlWindow::create_flex( + const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create flex while not initialized"); @@ -292,11 +297,13 @@ std::shared_ptr uui::GlWindow::create_flex( std::shared_ptr flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction)); push_child(flex); flex->init(); - return flex; + + if(flex != nullptr && init != nullptr) + init(*flex); } -std::shared_ptr uui::GlWindow::create_stack( - const position_t x, const position_t y, const Direction direction) +void uui::GlWindow::create_stack( + const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create stack while not initialized"); @@ -305,5 +312,7 @@ std::shared_ptr uui::GlWindow::create_stack( std::shared_ptr stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction)); push_child(stack); stack->init(); - return stack; + + if(stack != nullptr && init != nullptr) + init(*stack); }