From d15b25dba60efba0cb09ab2a84557ea88116a86d Mon Sep 17 00:00:00 2001 From: Corentin Date: Sat, 1 Oct 2022 05:03:16 +0900 Subject: [PATCH] Fix code format and add Style object (unused) --- .ccls | 2 +- .clang-format | 14 ++-- ROADMAP.md | 4 +- include/uui/component.hpp | 2 +- include/uui/config.hpp | 1 + include/uui/opengl/application.hpp | 8 +- include/uui/opengl/button.hpp | 29 +++++++ include/uui/opengl/component.hpp | 23 ++--- include/uui/opengl/container.hpp | 7 +- include/uui/opengl/flex.hpp | 10 ++- include/uui/opengl/font_manager.hpp | 6 +- include/uui/opengl/label.hpp | 13 +-- include/uui/opengl/stack.hpp | 13 +-- include/uui/style.hpp | 46 ++++++++++ makefile | 3 + src/test/benchmark_text.cpp | 3 +- src/test/example_gl.cpp | 126 ++++++++++++++-------------- src/uui/opengl/application.cpp | 2 +- src/uui/opengl/component.cpp | 69 ++++++++------- src/uui/opengl/flex.cpp | 16 ++-- src/uui/opengl/font_manager.cpp | 34 ++++---- src/uui/opengl/label.cpp | 23 ++--- src/uui/opengl/stack.cpp | 29 ++++--- src/uui/opengl/window.cpp | 32 ++++--- src/uui/shader.cpp | 6 +- 25 files changed, 320 insertions(+), 201 deletions(-) create mode 100644 include/uui/opengl/button.hpp create mode 100644 include/uui/style.hpp diff --git a/.ccls b/.ccls index 04436f7..a91784d 100644 --- a/.ccls +++ b/.ccls @@ -1,4 +1,4 @@ -clang++ +clangd -std=c++17 -DDEBUG -Wall diff --git a/.clang-format b/.clang-format index f54912e..3efd2bd 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ BasedOnStyle: Microsoft -AccessModifierOffset: -3 +AccessModifierOffset: -4 AlignAfterOpenBracket: AlwaysBreak AlignConsecutiveAssignments: None AlignConsecutiveBitFields: None @@ -21,7 +21,7 @@ AllowShortLoopsOnASingleLine: true AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: No -AttributeMacros: ['__ununsed'] +AttributeMacros: ['__unused'] BinPackArguments: true BinPackParameters: true BitFieldColonSpacing: Both @@ -50,12 +50,13 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: AfterColon BreakInheritanceList: AfterColon BreakStringLiterals: true -ColumnLimit: 140 +ColumnLimit: 120 CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false +EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock FixNamespaceComments: true IncludeBlocks: Regroup @@ -78,13 +79,14 @@ IncludeCategories: - Regex: '^"' Priority: 4 SortPriority: 4 +IndentAccessModifiers: false IndentCaseBlocks: true IndentCaseLabels: true IndentExternBlock: NoIndent IndentGotoLabels: false IndentPPDirectives: BeforeHash IndentRequires: true -IndentWidth: 3 +IndentWidth: 4 IndentWrappedFunctionNames: false InsertTrailingCommas: None KeepEmptyLinesAtTheStartOfBlocks: false @@ -116,6 +118,6 @@ SpacesInContainerLiterals: false SpacesInParentheses: false SpacesInSquareBrackets: false Standard: c++17 -TabWidth: 3 +TabWidth: 4 UseCRLF: false -UseTab: Always \ No newline at end of file +UseTab: Always diff --git a/ROADMAP.md b/ROADMAP.md index 9c5580d..6125280 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,6 +1,8 @@ +* layout + * button -* layout (constraint) +* lambda constructor * scroll diff --git a/include/uui/component.hpp b/include/uui/component.hpp index 1313eba..53a4c50 100644 --- a/include/uui/component.hpp +++ b/include/uui/component.hpp @@ -11,7 +11,7 @@ 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) + position_x(x), position_y(y), size_width(width), size_height(height) { background_color = {0.0f, 0.0f, 0.0f, 0.0f}; } diff --git a/include/uui/config.hpp b/include/uui/config.hpp index 03c4f94..a45f341 100644 --- a/include/uui/config.hpp +++ b/include/uui/config.hpp @@ -12,6 +12,7 @@ constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; namespace uui { typedef std::variant position_t; +typedef std::variant size_t; enum Direction { HORIZONTAL, diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index 2146237..30c1c9f 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -30,7 +30,7 @@ public: void run(); std::shared_ptr create_window(int width, int height, const std::string& title); - std::shared_ptr get_window(size_t index) const; + std::shared_ptr get_window(std::size_t index) const; private: static GlApplication* application_pointer; @@ -55,9 +55,11 @@ 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_component( + const position_t x, const position_t y, const position_t width, const position_t height); std::shared_ptr create_label( - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + 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); diff --git a/include/uui/opengl/button.hpp b/include/uui/opengl/button.hpp new file mode 100644 index 0000000..dfe5854 --- /dev/null +++ b/include/uui/opengl/button.hpp @@ -0,0 +1,29 @@ + +#pragma once + +#include +#include +#include + +#include "uui/opengl/component.hpp" + +namespace uui +{ +class GlButton: public GlComponent +{ + friend class GlWindow; + +protected: + GlButton( + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlButton( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const Direction direction): + GlButton(window, window_index, window, window_index, x, y, direction) + {} + + virtual std::tuple get_child_position(const std::size_t child_index) const; +}; + +} // namespace uui diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index ab9a554..135bbc5 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -6,6 +6,7 @@ #include "graphic_context.hpp" #include "uui/config.hpp" #include "uui/opengl/container.hpp" +#include "uui/style.hpp" #include "window.hpp" namespace uui @@ -19,21 +20,22 @@ class GlComponent: public std::enable_shared_from_this friend class GlStack; public: - ~GlComponent(); + virtual ~GlComponent(); void set_background_color(float r, float g, float b, float a = 1.0f); protected: std::shared_ptr window; - size_t window_index; + std::size_t window_index; std::shared_ptr parent; - size_t parent_index; + std::size_t parent_index; position_t position_x; position_t position_y; - position_t size_width; - position_t size_height; + size_t size_width; + size_t size_height; bool coord_all_relative; + Style style; std::array background_color; @@ -44,12 +46,13 @@ protected: GLuint gl_ebo; GlComponent( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const position_t width, const position_t height); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const position_t width, + const position_t height); GlComponent( - std::shared_ptr 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) + std::shared_ptr 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(const GlComponent&) = delete; GlComponent(GlComponent&&) = default; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index f690fb1..4673972 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -39,7 +40,11 @@ public: std::vector> components; Type type; - ~GlContainer() { components.clear(); } + virtual ~GlContainer() + { + std::cout << "Destroying Container" << std::endl; + components.clear(); + } }; } // namespace uui diff --git a/include/uui/opengl/flex.hpp b/include/uui/opengl/flex.hpp index 8448c7a..918792a 100644 --- a/include/uui/opengl/flex.hpp +++ b/include/uui/opengl/flex.hpp @@ -17,10 +17,12 @@ class GlFlex: public GlStack protected: GlFlex( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction); - GlFlex(std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const Direction direction): - GlFlex(window, window_index, window, window_index, x, y, direction) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlFlex( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const Direction direction): + GlFlex(window, window_index, window, window_index, x, y, direction) {} std::tuple get_child_position(const std::size_t child_index) const override; diff --git a/include/uui/opengl/font_manager.hpp b/include/uui/opengl/font_manager.hpp index 07184a5..926f84c 100644 --- a/include/uui/opengl/font_manager.hpp +++ b/include/uui/opengl/font_manager.hpp @@ -46,8 +46,8 @@ public: ~GlFontManager(); void render_text( - const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, - const std::tuple& text_color); + const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, + const std::tuple& text_color); std::tuple get_text_size(const std::string& text, Font& font, float scale_x, float scale_y); private: @@ -63,6 +63,6 @@ private: void init(); void deinit(); - const Font& init_font(const std::string& font_path, size_t font_size); + const Font& init_font(const std::string& font_path, std::size_t font_size); }; } // namespace uui diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index be3eee1..99f094d 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -16,7 +16,7 @@ class GlLabel: public GlComponent friend class GlFlex; friend class GlStack; - size_t font_index; + std::size_t font_index; float get_width() const; float get_height() const; @@ -29,12 +29,13 @@ protected: int text_height; GlLabel( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color); GlLabel( - std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color): - GlLabel(window, window_index, window, window_index, x, y, text, text_color) + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const std::string& text, const std::tuple& text_color): + GlLabel(window, window_index, window, window_index, x, y, text, text_color) {} void render(); diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index 6a52e38..9204a64 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -16,7 +16,8 @@ class GlStack: public GlComponent, public GlContainer 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); + std::shared_ptr create_label( + const std::string& text, const std::tuple& text_color); protected: Direction direction; @@ -24,10 +25,12 @@ protected: int content_height; GlStack( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction); - GlStack(std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const Direction direction): - GlStack(window, window_index, window, window_index, x, y, direction) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlStack( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const Direction direction): + GlStack(window, window_index, window, window_index, x, y, direction) {} float get_width() const; diff --git a/include/uui/style.hpp b/include/uui/style.hpp new file mode 100644 index 0000000..22d7401 --- /dev/null +++ b/include/uui/style.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +#include "config.hpp" + +namespace uui +{ +struct Style +{ + enum Position + { + RELATIVE, + ABSOLUTE + }; + enum Alignment + { + START, + CENTER, + END + }; + struct AbsolutePostion + { + std::optional left; + std::optional right; + std::optional top; + std::optional bottom; + }; + + // Layout + Position position; + Alignment horizontal_alignment; + Alignment vertical_alignment; + std::optional width; + std::optional height; + std::optional margin; + std::optional padding; + // For absolute positioning + std::unique_ptr absolute_position; + + // Color + std::optional> background_color; +}; +} // namespace uui diff --git a/makefile b/makefile index db2db6a..5c9f40d 100644 --- a/makefile +++ b/makefile @@ -11,3 +11,6 @@ debug: clean: @PYTHONPATH=$(UMAKE_PATH) python $(MAKE_PATH)/make.py --clean + +lint: + @clang-format --dry-run --Werror include/uui/*.hpp include/uui/**/*.hpp src/uui/*.cpp src/uui/**/*.cpp src/test/*.cpp diff --git a/src/test/benchmark_text.cpp b/src/test/benchmark_text.cpp index d51a978..1855d8c 100644 --- a/src/test/benchmark_text.cpp +++ b/src/test/benchmark_text.cpp @@ -13,8 +13,7 @@ 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}); + 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 8315822..a94fe74 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -10,81 +10,81 @@ using namespace std; int main() { - // try - // { - cout << "Starting example 1" << endl; + try { - uui::GlApplication app; - // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); + cout << "Starting example 1" << endl; + { + uui::GlApplication app; + // uui::GlApplication app2; // exception : application already created + auto window = app.create_window(800, 600, "OpenGl!"); - auto comp = window->create_component(0, 100, 0.5f, 50); - comp->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); - comp = window->create_component(0.25f, 50, 0.4f, 150); - comp->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); - 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); + 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); - 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}); + 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}); - 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); + 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); - 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); + 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); - window = app.create_window(800, 600, "OpenGl 2"); + 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); + 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); + 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); - app.run(); + 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); + + 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.run(); + } + } + catch(const std::exception& error) + { + std::cerr << error.what() << std::endl; + return EXIT_FAILURE; } - // 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); - - // 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.run(); - // } - // } - // catch(const std::exception& error) - // { - // std::cerr << error.what() << std::endl; - // return EXIT_FAILURE; - // } cout << "Example done" << endl; return 0; diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index ee36f85..71a747a 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -111,7 +111,7 @@ std::shared_ptr uui::GlApplication::create_window(int width, int return window; } -std::shared_ptr uui::GlApplication::get_window(size_t index) const +std::shared_ptr 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"); diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index f4e031e..07fb49d 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -6,11 +6,11 @@ #include "uui/opengl/gl_utils.hpp" uui::GlComponent::GlComponent( - std::shared_ptr window, std::size_t window_index, std::shared_ptr 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 window, std::size_t window_index, std::shared_ptr 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) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlBox" << std::endl; @@ -67,8 +67,8 @@ void uui::GlComponent::init() set_vbo_data(); unsigned int indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle }; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_ebo); if constexpr(uui::GlApplication::debug_mode) @@ -86,45 +86,50 @@ void uui::GlComponent::init() void uui::GlComponent::set_vbo_data() { - float vert_x = - position_x.index() == 1 ? std::get<1>(position_x) : static_cast(std::get<0>(position_x)) / static_cast(window->width); - float vert_y = - position_y.index() == 1 ? std::get<1>(position_y) : static_cast(std::get<0>(position_y)) / static_cast(window->height); - float vert_w = - size_width.index() == 1 ? std::get<1>(size_width) : static_cast(std::get<0>(size_width)) / static_cast(window->width); - float vert_h = size_height.index() == 1 ? std::get<1>(size_height) - : static_cast(std::get<0>(size_height)) / static_cast(window->height); + float vert_x = position_x.index() == 1 + ? std::get<1>(position_x) + : static_cast(std::get<0>(position_x)) / static_cast(window->width); + float vert_y = position_y.index() == 1 + ? std::get<1>(position_y) + : static_cast(std::get<0>(position_y)) / static_cast(window->height); + float vert_w = size_width.index() == 1 + ? std::get<1>(size_width) + : static_cast(std::get<0>(size_width)) / static_cast(window->width); + float vert_h = size_height.index() == 1 + ? std::get<1>(size_height) + : static_cast(std::get<0>(size_height)) / static_cast(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; float y_max = y_min - (2.0f * vert_h); // clang-format off - float vertices[] = { - x_max, y_min, background_color[0] , background_color[1], background_color[2], background_color[3], // top right - x_max, y_max, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom right - x_min, y_max, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom left - x_min, y_min, background_color[0] , background_color[1], background_color[2], background_color[3] // top left + float vertices[] = + { + x_max, y_min, background_color[0], background_color[1], background_color[2], background_color[3], // top right + x_max, y_max, background_color[0], background_color[1], background_color[2], background_color[3], // bottom right + x_min, y_max, background_color[0], background_color[1], background_color[2], background_color[3], // bottom left + x_min, y_min, background_color[0], background_color[1], background_color[2], background_color[3] // top left }; // clang-format on glBindBuffer(GL_ARRAY_BUFFER, gl_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer( - 0, // index - 2, // size - GL_FLOAT, // type - GL_FALSE, // enable normalization, - 6 * sizeof(float), // stride - (void*)0 // offset + 0, // index + 2, // size + GL_FLOAT, // type + GL_FALSE, // enable normalization, + 6 * sizeof(float), // stride + (void*)0 // offset ); glEnableVertexAttribArray(0); glVertexAttribPointer( - 1, // index - 4, // size - GL_FLOAT, // type - GL_FALSE, // enable normalization, - 6 * sizeof(float), // stride - (void*)(2 * sizeof(float)) // offset + 1, // index + 4, // size + GL_FLOAT, // type + GL_FALSE, // enable normalization, + 6 * sizeof(float), // stride + (void*)(2 * sizeof(float)) // offset ); glEnableVertexAttribArray(1); } diff --git a/src/uui/opengl/flex.cpp b/src/uui/opengl/flex.cpp index 5cd73db..bd259a2 100644 --- a/src/uui/opengl/flex.cpp +++ b/src/uui/opengl/flex.cpp @@ -5,9 +5,9 @@ #include "uui/opengl/gl_utils.hpp" uui::GlFlex::GlFlex( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction): - GlStack(window, window_index, parent, parent_index, x, y, direction) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction): + GlStack(window, window_index, parent, parent_index, x, y, direction) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlStack" << std::endl; @@ -18,10 +18,12 @@ uui::GlFlex::GlFlex( std::tuple uui::GlFlex::get_child_position(const std::size_t child_index) const { - const int initial_x = - position_x.index() == 0 ? std::get<0>(position_x) : static_cast(std::get<1>(position_x) * static_cast(window->width)); - const int initial_y = - position_y.index() == 0 ? std::get<0>(position_y) : static_cast(std::get<1>(position_y) * static_cast(window->height)); + const int initial_x = position_x.index() == 0 + ? std::get<0>(position_x) + : static_cast(std::get<1>(position_x) * static_cast(window->width)); + const int initial_y = position_y.index() == 0 + ? std::get<0>(position_y) + : static_cast(std::get<1>(position_y) * static_cast(window->height)); if(child_index == 0) return {initial_x, initial_y}; diff --git a/src/uui/opengl/font_manager.cpp b/src/uui/opengl/font_manager.cpp index 546ae6e..554c83d 100644 --- a/src/uui/opengl/font_manager.cpp +++ b/src/uui/opengl/font_manager.cpp @@ -125,7 +125,7 @@ void uui::GlFontManager::deinit() initialized = false; } -const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, size_t font_size) +const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, std::size_t font_size) { fonts.emplace_back(); auto& font = fonts.back(); @@ -206,7 +206,8 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& } glTexSubImage2D( - GL_TEXTURE_2D, 0, offset_x, offset_y, glyph->bitmap.width, glyph->bitmap.rows, GL_RED, GL_UNSIGNED_BYTE, glyph->bitmap.buffer); + GL_TEXTURE_2D, 0, offset_x, offset_y, glyph->bitmap.width, glyph->bitmap.rows, GL_RED, GL_UNSIGNED_BYTE, + glyph->bitmap.buffer); font.char_infos[i].advance_x = glyph->advance.x >> 6; font.char_infos[i].advance_y = glyph->advance.y >> 6; @@ -228,13 +229,13 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& } std::tuple uui::GlFontManager::get_text_size( - const std::string& text, uui::GlFontManager::Font& font, float scale_x, float scale_y) + const std::string& text, uui::GlFontManager::Font& font, float scale_x, float scale_y) { float width = 0.0f; float height = 0.0f; for(auto current_char: text) { - const auto& char_info = font.char_infos[static_cast(current_char)]; + const auto& char_info = font.char_infos[static_cast(current_char)]; width += char_info.advance_x * scale_x; // height = std::max(height, char_info.bitmap_top + char_info.bitmap_height * scale_y); height = std::max(height, roundf(char_info.bitmap_height * scale_y)); @@ -243,8 +244,8 @@ std::tuple uui::GlFontManager::get_text_size( } void uui::GlFontManager::render_text( - const std::string& text, uui::GlFontManager::Font& font, float x, float y, float scale_x, float scale_y, - const std::tuple& text_color) + const std::string& text, uui::GlFontManager::Font& font, float x, float y, float scale_x, float scale_y, + const std::tuple& text_color) { GLuint gl_prev_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)(&gl_prev_program)); @@ -268,7 +269,7 @@ void uui::GlFontManager::render_text( float out_base_y = y; for(auto current_char: text) { - const auto& char_info = font.char_infos[static_cast(current_char)]; + const auto& char_info = font.char_infos[static_cast(current_char)]; // Calculate the vertex and texture coordinates float char_x = out_base_x + char_info.bitmap_left * scale_x; float char_y = out_base_y - char_info.bitmap_top * scale_y; @@ -284,15 +285,16 @@ void uui::GlFontManager::render_text( continue; // clang-format off - float current_vertex[24] = { - char_x, char_y, char_info.texture_x, char_info.texture_y, // top_left - char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right - char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-right - char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right - char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-left - char_x + w, char_y + h, char_info.texture_x + char_info.bitmap_width / font.atlas_width, - char_info.texture_y + char_info.bitmap_height / font.atlas_height // bottom-right - }; + float current_vertex[24] = + { + char_x, char_y, char_info.texture_x, char_info.texture_y, // top_left + char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right + char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-right + char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right + char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-left + char_x + w, char_y + h, char_info.texture_x + char_info.bitmap_width / font.atlas_width, + char_info.texture_y + char_info.bitmap_height / font.atlas_height // bottom-right + }; std::memcpy(vertices.data() + (char_count * 24), current_vertex, 24 * sizeof(float)); // clang-format on char_count += 1; diff --git a/src/uui/opengl/label.cpp b/src/uui/opengl/label.cpp index 7e39604..c776be1 100644 --- a/src/uui/opengl/label.cpp +++ b/src/uui/opengl/label.cpp @@ -5,16 +5,18 @@ #include "uui/opengl/gl_utils.hpp" uui::GlLabel::GlLabel( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color): - GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f), - text(text), text_color(text_color) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color): + 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; 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); + std::tie(text_width, text_height) = + 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; @@ -22,14 +24,15 @@ uui::GlLabel::GlLabel( void uui::GlLabel::render() { - float text_x = - position_x.index() == 1 ? std::get<1>(position_x) * static_cast(window->width) : static_cast(std::get<0>(position_x)); - float text_y = - position_y.index() == 1 ? std::get<1>(position_y) * static_cast(window->height) : static_cast(std::get<0>(position_y)); + float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast(window->width) + : static_cast(std::get<0>(position_x)); + float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast(window->height) + : static_cast(std::get<0>(position_y)); text_y += static_cast(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); } diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp index 1b2c9f1..73a24ea 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/stack.cpp @@ -5,16 +5,14 @@ #include "uui/opengl/gl_utils.hpp" uui::GlStack::GlStack( - std::shared_ptr window, std::size_t window_index, std::shared_ptr 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) + std::shared_ptr window, std::size_t window_index, std::shared_ptr 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) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlStack" << std::endl; - std::cout << "Stack:" << direction << std::endl; - type = Type::STACK; coord_all_relative = false; } @@ -26,8 +24,8 @@ std::shared_ptr uui::GlStack::create_component(const position_ glfwMakeContextCurrent(window->glfw_window); std::shared_ptr component(new uui::GlComponent( - window, window->components.size(), std::dynamic_pointer_cast(shared_from_this()), components.size(), 0, 0, width, - height)); + window, window->components.size(), std::dynamic_pointer_cast(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); @@ -36,15 +34,16 @@ std::shared_ptr uui::GlStack::create_component(const position_ return component; } -std::shared_ptr uui::GlStack::create_label(const std::string& text, const std::tuple& text_color) +std::shared_ptr uui::GlStack::create_label( + const std::string& text, const std::tuple& text_color) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); glfwMakeContextCurrent(window->glfw_window); std::shared_ptr label(new uui::GlLabel( - window, window->components.size(), std::dynamic_pointer_cast(shared_from_this()), components.size(), 0, 0, text, - text_color)); + window, window->components.size(), std::dynamic_pointer_cast(shared_from_this()), + components.size(), 0, 0, text, text_color)); push_child(label); label->init(); std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1); @@ -55,10 +54,10 @@ std::shared_ptr uui::GlStack::create_label(const std::string& text std::tuple 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(std::get<1>(position_x) * static_cast(window->width)); - int y = - position_y.index() == 0 ? std::get<0>(position_y) : static_cast(std::get<1>(position_y) * static_cast(window->height)); + int x = position_x.index() == 0 ? std::get<0>(position_x) + : static_cast(std::get<1>(position_x) * static_cast(window->width)); + int y = position_y.index() == 0 ? std::get<0>(position_y) + : static_cast(std::get<1>(position_y) * static_cast(window->height)); if(child_index == 0) return {x, y}; diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index 5063be7..a06aa58 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -8,8 +8,9 @@ #include "uui/opengl/label.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) +static void APIENTRY glDebugOutput( + GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char* message, + const void* userParam) { // ignore non-significant error/warning codes if(id == 131169 || id == 131185 || id == 131218 || id == 131204) @@ -66,7 +67,9 @@ void uui::GlWindow::glfw_resize_callback(GLFWwindow* window, int width, int heig glm::mat4 projection = glm::ortho(0.0f, static_cast(width), static_cast(height), 0.0f); glfwMakeContextCurrent(window); glUseProgram(gl_window->font_manager.gl_program); - glUniformMatrix4fv(glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv( + glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE, + glm::value_ptr(projection)); } void uui::GlWindow::glfw_iconify_callback(GLFWwindow* window, int iconified) @@ -82,7 +85,8 @@ void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode, } 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) + app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false), + iconified(false) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlWindow" << std::endl; @@ -206,7 +210,8 @@ void uui::GlWindow::init() font_manager.init(); glUseProgram(font_manager.gl_program); glm::mat4 projection = glm::ortho(0.0f, static_cast(width), static_cast(height), 0.0f); - glUniformMatrix4fv(glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv( + glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUseProgram(gl_program); initialized = true; @@ -249,32 +254,36 @@ void uui::GlWindow::render() } std::shared_ptr uui::GlWindow::create_component( - const position_t x, const position_t y, const position_t width, const position_t height) + const position_t x, const position_t y, const position_t width, const position_t height) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr component(new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); + std::shared_ptr component( + new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); push_child(component); component->init(); return 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) + const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create label while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr label(new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); + std::shared_ptr label( + new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); push_child(label); label->init(); return label; } -std::shared_ptr uui::GlWindow::create_flex(const position_t x, const position_t y, const Direction direction) +std::shared_ptr uui::GlWindow::create_flex( + const position_t x, const position_t y, const Direction direction) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create flex while not initialized"); @@ -286,7 +295,8 @@ std::shared_ptr uui::GlWindow::create_flex(const position_t x, cons return flex; } -std::shared_ptr uui::GlWindow::create_stack(const position_t x, const position_t y, const Direction direction) +std::shared_ptr uui::GlWindow::create_stack( + const position_t x, const position_t y, const Direction direction) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create stack while not initialized"); diff --git a/src/uui/shader.cpp b/src/uui/shader.cpp index 2ce9266..ea8e207 100644 --- a/src/uui/shader.cpp +++ b/src/uui/shader.cpp @@ -1,7 +1,7 @@ #include "uui/shader.hpp" #include - +#include std::vector uui::readFile(const std::string& filename) { @@ -10,10 +10,10 @@ std::vector uui::readFile(const std::string& filename) if(!file.is_open()) throw std::runtime_error("failed to open file!"); - size_t fileSize = (size_t)file.tellg(); + std::size_t fileSize = static_cast(file.tellg()); std::vector buffer(fileSize); file.seekg(0); - file.read(buffer.data(), fileSize); + file.read(buffer.data(), static_cast(fileSize)); file.close(); return buffer;