From 29627a93c68b9f3927ee9d1097ed3266777b6a0b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 7 Nov 2021 06:33:07 +0100 Subject: Fix exit being called when closing window, respond to wm ping, add is_open function to window --- depends/mgl | 2 +- include/mglpp/graphics/Image.hpp | 1 + include/mglpp/graphics/Shader.hpp | 1 + include/mglpp/graphics/Text.hpp | 2 +- include/mglpp/system/FloatRect.hpp | 6 +++--- include/mglpp/system/vec.hpp | 3 ++- include/mglpp/window/Event.hpp | 9 ++++++++- src/graphics/Image.cpp | 4 ++++ src/graphics/Shader.cpp | 6 ++++++ src/graphics/Text.cpp | 13 +++++++------ src/window/Window.cpp | 4 +++- tests/main.cpp | 2 +- 12 files changed, 38 insertions(+), 15 deletions(-) diff --git a/depends/mgl b/depends/mgl index ccb3e58..2143364 160000 --- a/depends/mgl +++ b/depends/mgl @@ -1 +1 @@ -Subproject commit ccb3e58071b3e807109918184727b305df8b96a0 +Subproject commit 214336492da0d184d5ad4ac64c31920954c5f7e7 diff --git a/include/mglpp/graphics/Image.hpp b/include/mglpp/graphics/Image.hpp index 1ddeb88..a868ad6 100644 --- a/include/mglpp/graphics/Image.hpp +++ b/include/mglpp/graphics/Image.hpp @@ -19,6 +19,7 @@ namespace mgl { unsigned char* data(); size_t get_byte_size(); vec2i get_size() const; + int get_num_channels() const; mgl_image* internal_image(); private: diff --git a/include/mglpp/graphics/Shader.hpp b/include/mglpp/graphics/Shader.hpp index 47e9689..0aedc47 100644 --- a/include/mglpp/graphics/Shader.hpp +++ b/include/mglpp/graphics/Shader.hpp @@ -20,6 +20,7 @@ namespace mgl { ~Shader(); bool load_from_file(const char *filepath, Type type); + bool set_uniform(const char *name, float value); bool set_uniform(const char *name, vec2f value); bool is_valid() const; diff --git a/include/mglpp/graphics/Text.hpp b/include/mglpp/graphics/Text.hpp index 23f847c..6166087 100644 --- a/include/mglpp/graphics/Text.hpp +++ b/include/mglpp/graphics/Text.hpp @@ -23,7 +23,7 @@ namespace mgl { void set_color(Color color) override; vec2f get_position() const override; - FloatRect get_local_bounds(); + FloatRect get_bounds() const; void set_string(std::string str); const std::string& get_string() const; diff --git a/include/mglpp/system/FloatRect.hpp b/include/mglpp/system/FloatRect.hpp index e187b50..50941dd 100644 --- a/include/mglpp/system/FloatRect.hpp +++ b/include/mglpp/system/FloatRect.hpp @@ -7,10 +7,10 @@ namespace mgl { template class Rect { public: - Rect() : position(0.0f, 0.0f), size(0.0f, 0.0f) {} - Rect(const vec2& position, const vec2& size) : position(position), size(size) {} + Rect() : position(0, 0), size(0, 0) {} + Rect(vec2 position, vec2 size) : position(position), size(size) {} - bool contains(const vec2& point) const { + bool contains(vec2 point) const { return point.x >= position.x && point.x <= position.x + size.x && point.y >= position.y && point.y <= position.y + size.y; } diff --git a/include/mglpp/system/vec.hpp b/include/mglpp/system/vec.hpp index ea6eba5..c81cdfa 100644 --- a/include/mglpp/system/vec.hpp +++ b/include/mglpp/system/vec.hpp @@ -4,7 +4,8 @@ namespace mgl { template struct vec2 { - vec2(T x = 0, T y = 0) : x(x), y(y) {} + vec2() : x(0), y(0) {} + vec2(T x, T y) : x(x), y(y) {} vec2 operator + (vec2 other) const { return { x + other.x, y + other.y }; diff --git a/include/mglpp/window/Event.hpp b/include/mglpp/window/Event.hpp index c073251..5b7da45 100644 --- a/include/mglpp/window/Event.hpp +++ b/include/mglpp/window/Event.hpp @@ -15,6 +15,10 @@ namespace mgl { int height; }; + struct TextEvent { + uint32_t codepoint; + }; + struct KeyEvent { Keyboard::Key code; bool alt; @@ -36,7 +40,9 @@ namespace mgl { enum Type : int { Unknown, - Resized, + Closed, /* Window closed */ + Resized, /* Window resized */ + TextEntered, KeyPressed, KeyReleased, MouseButtonPressed, @@ -48,6 +54,7 @@ namespace mgl { union { SizeEvent size; + TextEvent text; KeyEvent key; MouseButtonEvent mouse_button; MouseMoveEvent mouse_move; diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp index 2d9b58d..74a9866 100644 --- a/src/graphics/Image.cpp +++ b/src/graphics/Image.cpp @@ -34,6 +34,10 @@ namespace mgl { return { image.width, image.height }; } + int Image::get_num_channels() const { + return mgl_image_get_num_channels(&image); + } + mgl_image* Image::internal_image() { return ℑ } diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index c23d32b..7775b78 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -24,6 +24,12 @@ namespace mgl { return mgl_shader_program_finalize(&shader_program) == 0; } + bool Shader::set_uniform(const char *name, float value) { + if(!shader_program.id) + return false; + return mgl_shader_program_set_uniform_float(&shader_program, name, value) == 0; + } + bool Shader::set_uniform(const char *name, vec2f value) { if(!shader_program.id) return false; diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp index b3a32da..49829e6 100644 --- a/src/graphics/Text.cpp +++ b/src/graphics/Text.cpp @@ -7,19 +7,19 @@ extern "C" { namespace mgl { Text::Text() : font(nullptr) { - mgl_text_init(&text, nullptr, nullptr, 0, nullptr); + mgl_text_init(&text, nullptr, nullptr, 0); } Text::Text(std::string str, Font &font) : Text(std::move(str), vec2f(0.0f, 0.0f), font) {} Text::Text(std::string str, vec2f position, Font &font) : font(&font), str(std::move(str)) { - mgl_text_init(&text, font.internal_font(), this->str.c_str(), this->str.size(), nullptr); + mgl_text_init(&text, font.internal_font(), this->str.c_str(), this->str.size()); mgl_text_set_position(&text, { position.x, position.y }); } Text::Text(const Text &other) { font = other.font; - mgl_text_init(&text, font ? font->internal_font() : nullptr, other.str.c_str(), other.str.size(), nullptr); + mgl_text_init(&text, font ? font->internal_font() : nullptr, other.str.c_str(), other.str.size()); mgl_text_set_position(&text, { other.text.position.x, other.text.position.y }); } @@ -39,9 +39,10 @@ namespace mgl { return { text.position.x, text.position.y }; } - // TODO: Implement - FloatRect Text::get_local_bounds() { - return FloatRect(); + FloatRect Text::get_bounds() const { + mgl_vec2f bounds = mgl_text_get_bounds(&text); + FloatRect rect(get_position(), { bounds.x, bounds.y }); + return rect; } void Text::set_string(std::string str) { diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 7f1fe38..5dbbe5a 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -71,7 +71,9 @@ namespace mgl { // TODO: Implement bool Window::is_open() const { - return true; + if(!window.window) + return false; + return mgl_window_is_open(&window); } // TODO: Implement diff --git a/tests/main.cpp b/tests/main.cpp index 98fa34c..25ef012 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -98,7 +98,7 @@ int main(int argc, char **argv) { delegate.shader_program = &shader; mgl::Event event; - while(true) { + while(window.is_open()) { if(window.poll_event(event)) { } -- cgit v1.2.3