diff --git a/CMakeLists.txt b/CMakeLists.txt index 45bf937..afd9c6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.13) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -project(Onyx VERSION 1.4.1) +project(Onyx VERSION 1.5.0) add_compile_options(-Wno-volatile -Wno-delete-incomplete) diff --git a/README.md b/README.md index da40a22..4cad587 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ Onyx is a high-level, cross-platform C++ rendering engine using OpenGL 4.1, with The intuitive and well-structured codebase makes it simpler than ever to code games, apps, or any graphical interface using a language as complex as C++. ## Download -There is a pre-compiled binary compiled on my system (WSL x64), but it is recommended to compile yourself using CMake. See [Build](https://github.com/jopo86/onyx/wiki/Build) for detailed download/build instructions. +There is a pre-compiled binary compiled on my system (Windows x64), but it is recommended to compile yourself using CMake. See [Build](https://github.com/jopo86/onyx/wiki/Build) for detailed download/build instructions. -[Latest Release (1.4.1)](https://github.com/jopo86/onyx/releases/tag/v1.4.1) +[Latest Release (1.5.0)](https://github.com/jopo86/onyx/releases/tag/v1.5.0) [All Releases](https://github.com/jopo86/onyx/releases) ## Get Started @@ -28,3 +28,4 @@ There is also a [Guides](https://github.com/jopo86/onyx/wiki/guides) page, with - Presets for Meshes, Shaders, and Renderables - Monitor info - Extensive Matrix & Vector Math +- Various system functions (clipboard access, key states, and more) diff --git a/build/CMakeFiles/onyx.dir/src/Core.cpp.obj b/build/CMakeFiles/onyx.dir/src/Core.cpp.obj index fc5055e..515e8fc 100644 Binary files a/build/CMakeFiles/onyx.dir/src/Core.cpp.obj and b/build/CMakeFiles/onyx.dir/src/Core.cpp.obj differ diff --git a/build/CMakeFiles/onyx.dir/src/InputHandler.cpp.obj b/build/CMakeFiles/onyx.dir/src/InputHandler.cpp.obj index e3debc8..db7ca24 100644 Binary files a/build/CMakeFiles/onyx.dir/src/InputHandler.cpp.obj and b/build/CMakeFiles/onyx.dir/src/InputHandler.cpp.obj differ diff --git a/build/CMakeFiles/onyx.dir/src/Window.cpp.obj b/build/CMakeFiles/onyx.dir/src/Window.cpp.obj index bb921ea..0ec7562 100644 Binary files a/build/CMakeFiles/onyx.dir/src/Window.cpp.obj and b/build/CMakeFiles/onyx.dir/src/Window.cpp.obj differ diff --git a/build/CMakeFiles/test.dir/objects.a b/build/CMakeFiles/test.dir/objects.a index 7a4a6e4..66ea270 100644 Binary files a/build/CMakeFiles/test.dir/objects.a and b/build/CMakeFiles/test.dir/objects.a differ diff --git a/build/CMakeFiles/test.dir/tests/BouncingBallTest.cpp.obj b/build/CMakeFiles/test.dir/tests/BouncingBallTest.cpp.obj index 3733387..cdd05ee 100644 Binary files a/build/CMakeFiles/test.dir/tests/BouncingBallTest.cpp.obj and b/build/CMakeFiles/test.dir/tests/BouncingBallTest.cpp.obj differ diff --git a/build/CMakeFiles/test.dir/tests/PresetTests.cpp.obj b/build/CMakeFiles/test.dir/tests/PresetTests.cpp.obj index a82e76f..bcfae2b 100644 Binary files a/build/CMakeFiles/test.dir/tests/PresetTests.cpp.obj and b/build/CMakeFiles/test.dir/tests/PresetTests.cpp.obj differ diff --git a/build/CMakeFiles/test.dir/tests/WindowTest.cpp.obj b/build/CMakeFiles/test.dir/tests/WindowTest.cpp.obj index ad0a33e..3095851 100644 Binary files a/build/CMakeFiles/test.dir/tests/WindowTest.cpp.obj and b/build/CMakeFiles/test.dir/tests/WindowTest.cpp.obj differ diff --git a/build/libonyx.a b/build/libonyx.a index 169a276..c23e997 100644 Binary files a/build/libonyx.a and b/build/libonyx.a differ diff --git a/build/test.exe b/build/test.exe index 3ee2779..bbb5802 100644 Binary files a/build/test.exe and b/build/test.exe differ diff --git a/resources/shaders/bin/PN_Color.bin b/resources/shaders/bin/PN_Color.bin index 9bc642f..1a4c0ee 100644 Binary files a/resources/shaders/bin/PN_Color.bin and b/resources/shaders/bin/PN_Color.bin differ diff --git a/resources/shaders/bin/PT_UI.bin b/resources/shaders/bin/PT_UI.bin index 8bb19f4..338bada 100644 Binary files a/resources/shaders/bin/PT_UI.bin and b/resources/shaders/bin/PT_UI.bin differ diff --git a/resources/shaders/bin/P_UI_Color.bin b/resources/shaders/bin/P_UI_Color.bin index f578ca4..b521c8c 100644 Binary files a/resources/shaders/bin/P_UI_Color.bin and b/resources/shaders/bin/P_UI_Color.bin differ diff --git a/resources/shaders/bin/UI_Text.bin b/resources/shaders/bin/UI_Text.bin index 3e83a87..12edb87 100644 Binary files a/resources/shaders/bin/UI_Text.bin and b/resources/shaders/bin/UI_Text.bin differ diff --git a/src/Core.h b/src/Core.h index debf5bf..5c3e2bc 100644 --- a/src/Core.h +++ b/src/Core.h @@ -9,8 +9,8 @@ */ #define ONYX_VERSION_MAJOR 1 -#define ONYX_VERSION_MINOR 4 -#define ONYX_VERSION_PATCH 1 +#define ONYX_VERSION_MINOR 5 +#define ONYX_VERSION_PATCH 0 #define ONYX_PRE_RELEASE_NUM 0 diff --git a/src/InputHandler.cpp b/src/InputHandler.cpp index 6aae5ba..22c52ec 100644 --- a/src/InputHandler.cpp +++ b/src/InputHandler.cpp @@ -21,6 +21,11 @@ void onyx_warn(const Onyx::Warning&); Onyx::InputHandler::InputHandler() { m_pWin = nullptr; + m_pKeyCallback = nullptr; + m_pMouseButtonCallback = nullptr; + m_pMousePosCallback = nullptr; + m_pScrollCallback = nullptr; + m_pJoystickCallback = nullptr; for (int i = 0; i < (int)Onyx::Key::MaxKey; i++) { @@ -200,6 +205,31 @@ void Onyx::InputHandler::toggleCursorLock() setCursorLock(!m_cursorLock); } +void Onyx::InputHandler::setKeyCallback(KeyCallbackFn callback) +{ + m_pKeyCallback = callback; +} + +void Onyx::InputHandler::setMouseButtonCallback(MouseButtonCallbackFn callback) +{ + m_pMouseButtonCallback = callback; +} + +void Onyx::InputHandler::setMousePosCallback(MousePosCallbackFn callback) +{ + m_pMousePosCallback = callback; +} + +void Onyx::InputHandler::setScrollCallback(ScrollCallbackFn callback) +{ + m_pScrollCallback = callback; +} + +void Onyx::InputHandler::setJoystickCallback(JoystickCallbackFn callback) +{ + m_pJoystickCallback = callback; +} + bool Onyx::InputHandler::isCursorLocked() const { return m_cursorLock; @@ -331,29 +361,39 @@ void Onyx::InputHandler::keyCallback(int key, int scancode, int action, int mods { m_keys[key] = (Onyx::KeyState)action; if (action == GLFW_PRESS) m_keysTapped[key] = true; + + if (m_pKeyCallback) m_pKeyCallback(key, scancode, action, mods); } void Onyx::InputHandler::mouseButtonCallback(int button, int action, int mods) { m_buttons[button] = (Onyx::KeyState)action; if (action == GLFW_PRESS) m_buttonsTapped[button] = true; + + if (m_pMouseButtonCallback) m_pMouseButtonCallback(button, action, mods); } -void Onyx::InputHandler::cursorPosCallback(double x, double y) +void Onyx::InputHandler::mousePosCallback(double x, double y) { m_mousePos.set(x, y); + + if (m_pMousePosCallback) m_pMousePosCallback(x, y); } void Onyx::InputHandler::scrollCallback(double dx, double dy) { m_scrollDeltas.set(dx, dy); m_scrollThisFrame = true; + + if (m_pScrollCallback) m_pScrollCallback(dx, dy); } void Onyx::InputHandler::joystickCallback(int jid, int event) { if (!glfwJoystickIsGamepad(jid)) return; + if (m_pJoystickCallback != nullptr) m_pJoystickCallback(jid, event); + bool found = false; for (Gamepad& gamepad : m_gamepads) { diff --git a/src/InputHandler.h b/src/InputHandler.h index aa6d06e..7bbe029 100644 --- a/src/InputHandler.h +++ b/src/InputHandler.h @@ -15,6 +15,44 @@ namespace Onyx { friend class Window; + /* + @brief The key callback function signature. + @param key The GLFW key. + TODO: scancode param + @param action The GLFW action. + TODO: mods param + */ + typedef void(*KeyCallbackFn)(int key, int scancode, int action, int mods); + + /* + @brief The mouse button callback function signature. + @param key The GLFW button. + @param action The GLFW action. + TODO: mods param + */ + typedef void(*MouseButtonCallbackFn)(int button, int action, int mods); + + /* + @brief The mouse position callback function signature. + @param x The X position of the mouse as a window coordinate. + @param y The Y position of the mouse as a window coordinate. + */ + typedef void(*MousePosCallbackFn)(double x, double y); + + /* + @brief The scroll callback function signature. + @param dx The change in scroll on the X axis. + @param dy The change in scroll on the Y axis (the only axis for mice). + */ + typedef void(*ScrollCallbackFn)(double dx, double dy); + + /* + @brief The key callback function signature. + @param jid The GLFW joystick ID. + @param event The GLFW event. + */ + typedef void(*JoystickCallbackFn)(int jid, int event); + public: /* @brief Creates an InputHandler. @@ -147,6 +185,41 @@ namespace Onyx */ void toggleCursorLock(); + /* + @brief Sets the key callback function. + @param callback The callback function. + See definition of `KeyCallbackFn` for parameter info. + */ + void setKeyCallback(KeyCallbackFn callback); + + /* + @brief Sets the mouse button callback function. + @param callback The callback function. + See definition of `MouseButtonCallbackFn` for parameter info. + */ + void setMouseButtonCallback(MouseButtonCallbackFn callback); + + /* + @brief Sets the mouse position callback function. + @param callback The callback function. + See definition of `MousePosCallbackFn` for parameter info. + */ + void setMousePosCallback(MousePosCallbackFn callback); + + /* + @brief Sets the scroll callback function. + @param callback The callback function. + See definition of `ScrollCallbackFn` for parameter info. + */ + void setScrollCallback(ScrollCallbackFn callback); + + /* + @brief Sets the joystick callback function. + @param callback The callback function. + See definition of `JoystickCallbackFn` for parameter info. + */ + void setJoystickCallback(JoystickCallbackFn callback); + /* @brief Gets whether the cursor is locked. @return Whether the cursor is locked. @@ -231,9 +304,15 @@ namespace Onyx bool m_cursorLock; + KeyCallbackFn m_pKeyCallback; + MouseButtonCallbackFn m_pMouseButtonCallback; + MousePosCallbackFn m_pMousePosCallback; + ScrollCallbackFn m_pScrollCallback; + JoystickCallbackFn m_pJoystickCallback; + void keyCallback(int key, int scancode, int action, int mods); void mouseButtonCallback(int button, int action, int mods); - void cursorPosCallback(double x, double y); + void mousePosCallback(double x, double y); void scrollCallback(double dx, double dy); void joystickCallback(int jid, int event); }; diff --git a/src/TextRenderable.cpp b/src/TextRenderable.cpp index 7df5c3a..909d67a 100644 --- a/src/TextRenderable.cpp +++ b/src/TextRenderable.cpp @@ -17,7 +17,7 @@ Onyx::TextRenderable::TextRenderable() m_scale = Vec2(1.0f); } -Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 color, bool* result) +Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, const Vec3& color, bool* result) { m_pFont = &font; m_hidden = false; @@ -65,7 +65,7 @@ Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 c if (result != nullptr) *result = true; } -Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec4 color, bool* result) +Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, const Vec4& color, bool* result) { m_pFont = &font; m_hidden = false; @@ -122,6 +122,111 @@ Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec4 c if (result != nullptr) *result = true; } +Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, const Vec3& color, Shader shaderOverride, bool* result) +{ + m_pFont = &font; + m_hidden = false; + m_text = text; + m_color = Vec4(color, 1.0f); + m_model = Mat4::Identity(); + m_z = 0.0f; + m_rotation = 0.0f; + m_scale = Vec2(1.0f); + m_dimensions = font.getStringDimensions(text); + + if (font.getGlyphs().size() == 0) + { + onyx_err(Error{ + .sourceFunction = "Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 color)", + .message = "Font has no glyphs loaded.", + .howToFix = "Load a font from a TTF file before creating a TextRenderable with it." + } + ); + if (result != nullptr) *result = false; + return; + } + if (text == "") + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 color)", + .message = "Text is empty.", + .howToFix = "Provide a non-empty string to render.", + .severity = Warning::Severity::Low + } + ); + } + + uint advance = 0; + for (int i = 0; i < text.size(); i++) + { + m_chars.push_back(CharRenderable(text[i], font, advance)); + advance += font[text[i]].advance >> 6; + } + + m_shader = shaderOverride; + m_shader.use(); + m_shader.setVec4("u_color", m_color); + + if (result != nullptr) *result = true; +} + +Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, const Vec4& color, Shader shaderOverride, bool* result) +{ + m_pFont = &font; + m_hidden = false; + m_text = text; + m_color = color; + m_model = Mat4::Identity(); + m_rotation = 0.0f; + m_scale = Vec2(1.0f); + m_dimensions = font.getStringDimensions(text); + + if (font.getGlyphs().size() == 0) + { + onyx_err(Error{ + .sourceFunction = "Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 color)", + .message = "Font has no glyphs loaded.", + .howToFix = "Load a font from a TTF file before creating a TextRenderable with it." + } + ); + if (result != nullptr) *result = false; + return; + } + if (text == "") + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec3 color)", + .message = "Text is empty.", + .howToFix = "Provide a non-empty string to render.", + .severity = Warning::Severity::Low + } + ); + } + if (color.getW() == 0.0f) + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable::TextRenderable(const std::string& text, Font& font, Vec4 color)", + .message = "Alpha value of text color is 0, text will not be visible.", + .howToFix = "Change the alpha value to be between 0 and 1, reflecting the text's opacity.", + .severity = Warning::Severity::Low + } + ); + } + + uint advance = 0; + for (int i = 0; i < text.size(); i++) + { + m_chars.push_back(CharRenderable(text[i], font, advance)); + advance += font[text[i]].advance >> 6; + } + + m_shader = shaderOverride; + m_shader.use(); + m_shader.setVec4("u_color", color); + + if (result != nullptr) *result = true; +} + void Onyx::TextRenderable::render() { if (m_hidden) return; diff --git a/src/TextRenderable.h b/src/TextRenderable.h index b8679a8..2fe0d90 100644 --- a/src/TextRenderable.h +++ b/src/TextRenderable.h @@ -27,7 +27,7 @@ namespace Onyx @param color The color of the text. @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. */ - TextRenderable(const std::string& text, Font& font, Math::Vec3 color, bool* result = nullptr); + TextRenderable(const std::string& text, Font& font, const Math::Vec3& color, bool* result = nullptr); /* @brief Creates a TextRenderable from the specified text, font, and color. @@ -36,7 +36,27 @@ namespace Onyx @param color The color of the text. @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. */ - TextRenderable(const std::string& text, Font& font, Math::Vec4 color, bool* result = nullptr); + TextRenderable(const std::string& text, Font& font, const Math::Vec4& color, bool* result = nullptr); + + /* + @brief Creates a TextRenderable from the specified text, font, and color. + @param text The text to render. + @param font The font to use. + @param color The color of the text. + @param shaderOverride The shader to use instead of the defaut text shader. + @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. + */ + TextRenderable(const std::string& text, Font& font, const Math::Vec3& color, Shader shaderOverride, bool* result = nullptr); + + /* + @brief Creates a TextRenderable from the specified text, font, and color. + @param text The text to render. + @param font The font to use. + @param color The color of the text. + @param shaderOverride The shader to use instead of the defaut text shader. + @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. + */ + TextRenderable(const std::string& text, Font& font, const Math::Vec4& color, Shader shaderOverride, bool* result = nullptr); /* @brief Renders the object. diff --git a/src/TextRenderable3D.cpp b/src/TextRenderable3D.cpp index d02360a..6e9b0cd 100644 --- a/src/TextRenderable3D.cpp +++ b/src/TextRenderable3D.cpp @@ -15,7 +15,7 @@ Onyx::TextRenderable3D::TextRenderable3D() m_scale = Vec3(1.0f); } -Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec3 color, bool* result) +Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, const Vec3& color, bool* result) { m_pFont = &font; m_hidden = false; @@ -61,7 +61,7 @@ Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Ve if (result != nullptr) *result = true; } -Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec4 color, bool* result) +Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, const Vec4& color, bool* result) { m_pFont = &font; m_hidden = false; @@ -116,6 +116,107 @@ Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Ve if (result != nullptr) *result = true; } +Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, const Vec3& color, Shader shaderOverride, bool* result) +{ + m_pFont = &font; + m_hidden = false; + m_text = text; + m_color = Vec4(color, 1.0f); + m_model = Mat4::Identity(); + m_scale = Vec3(1.0f); + m_dimensions = font.getStringDimensions(text); + + if (font.getGlyphs().size() == 0) + { + onyx_err(Error{ + .sourceFunction = "Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec3 color)", + .message = "Font has no glyphs loaded.", + .howToFix = "Load a font from a TTF file before creating a TextRenderable3D with it." + } + ); + if (result != nullptr) *result = false; + return; + } + if (text == "") + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec3 color)", + .message = "Text is empty.", + .howToFix = "Provide a non-empty string to render.", + .severity = Warning::Severity::Low + } + ); + } + + uint advance = 0; + for (int i = 0; i < text.size(); i++) + { + m_chars.push_back(CharRenderable(text[i], font, advance)); + advance += font[text[i]].advance >> 6; + } + + m_shader = shaderOverride; + m_shader.use(); + m_shader.setVec4("u_color", m_color); + + if (result != nullptr) *result = true; +} + +Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, const Vec4& color, Shader shaderOverride, bool* result) +{ + m_pFont = &font; + m_hidden = false; + m_text = text; + m_color = color; + m_model = Mat4::Identity(); + m_scale = Vec3(1.0f); + m_dimensions = font.getStringDimensions(text); + + if (font.getGlyphs().size() == 0) + { + onyx_err(Error{ + .sourceFunction = "Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec3 color)", + .message = "Font has no glyphs loaded.", + .howToFix = "Load a font from a TTF file before creating a TextRenderable3D with it." + } + ); + if (result != nullptr) *result = false; + return; + } + if (text == "") + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec3 color)", + .message = "Text is empty.", + .howToFix = "Provide a non-empty string to render.", + .severity = Warning::Severity::Low + } + ); + } + if (color.getW() == 0.0f) + { + onyx_warn(Warning{ + .sourceFunction = "Onyx::TextRenderable3D::TextRenderable3D(const std::string& text, Font& font, Vec4 color)", + .message = "Alpha value of text color is 0, text will not be visible.", + .howToFix = "Change the alpha value to be between 0 and 1, reflecting the text's opacity.", + .severity = Warning::Severity::Low + } + ); + } + + uint advance = 0; + for (int i = 0; i < text.size(); i++) + { + m_chars.push_back(CharRenderable(text[i], font, advance)); + advance += font[text[i]].advance >> 6; + } + + m_shader = shaderOverride; + m_shader.use(); + m_shader.setVec4("u_color", color); + if (result != nullptr) *result = true; +} + void Onyx::TextRenderable3D::render() { if (m_hidden) return; diff --git a/src/TextRenderable3D.h b/src/TextRenderable3D.h index 451f2fd..d4ae2d2 100644 --- a/src/TextRenderable3D.h +++ b/src/TextRenderable3D.h @@ -27,7 +27,7 @@ namespace Onyx @param color The color of the text. @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. */ - TextRenderable3D(const std::string& text, Font& font, Math::Vec3 color, bool* result = nullptr); + TextRenderable3D(const std::string& text, Font& font, const Math::Vec3& color, bool* result = nullptr); /* @brief Creates a TextRenderable3D from the specified text, font, and color. @@ -36,7 +36,27 @@ namespace Onyx @param color The color of the text. @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. */ - TextRenderable3D(const std::string& text, Font& font, Math::Vec4 color, bool* result = nullptr); + TextRenderable3D(const std::string& text, Font& font, const Math::Vec4& color, bool* result = nullptr); + + /* + @brief Creates a TextRenderable3D from the specified text, font, and color. + @param text The text to render. + @param font The font to use. + @param color The color of the text. + @param shaderOverride The shader to use instead of the default 3D text shader. + @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. + */ + TextRenderable3D(const std::string& text, Font& font, const Math::Vec3& color, Shader shaderOverride, bool* result = nullptr); + + /* + @brief Creates a TextRenderable3D from the specified text, font, and color. + @param text The text to render. + @param font The font to use. + @param color The color of the text. + @param shaderOverride The shader to use instead of the default 3D text shader. + @param result A pointer to a boolean that will be set to true if the object was created successfully, and false otherwise. + */ + TextRenderable3D(const std::string& text, Font& font, const Math::Vec4& color, Shader shaderOverride, bool* result = nullptr); /* @brief Renders the object. diff --git a/src/Window.cpp b/src/Window.cpp index da6b26c..a794a76 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -140,7 +140,10 @@ Onyx::Window::Window() m_frame = 0L; m_fps = 0; m_lastFrameTime = m_deltaTime = 0; - m_fileDropCallback = nullptr; + m_pFramebufferSizeCallback = nullptr; + m_pWindowSizeCallback = nullptr; + m_pWindowPosCallback = nullptr; + m_pFileDropCallback = nullptr; m_numFramesCamNotUpdated = m_numFramesInputNotUpdated = 0; } @@ -153,7 +156,10 @@ Onyx::Window::Window(WindowProperties properties) m_frame = 0L; m_fps = 0; m_lastFrameTime = m_deltaTime = 0; - m_fileDropCallback = nullptr; + m_pFramebufferSizeCallback = nullptr; + m_pWindowSizeCallback = nullptr; + m_pWindowPosCallback = nullptr; + m_pFileDropCallback = nullptr; m_numFramesCamNotUpdated = m_numFramesInputNotUpdated = 0; } @@ -604,9 +610,24 @@ void Onyx::Window::linkRenderer(Renderer& renderer) renderer.m_ortho = Projection::Orthographic(m_bufferWidth, m_bufferHeight).getMatrix(); } -void Onyx::Window::setFileDropCallback(void (*callback)(const char**, int)) +void Onyx::Window::setFramebufferSizeCallback(FramebufferSizeCallbackFn callback) { - m_fileDropCallback = callback; + m_pFramebufferSizeCallback = callback; +} + +void Onyx::Window::setWindowSizeCallback(WindowSizeCallbackFn callback) +{ + m_pWindowSizeCallback = callback; +} + +void Onyx::Window::setWindowPosCallback(WindowPosCallbackFn callback) +{ + m_pWindowPosCallback = callback; +} + +void Onyx::Window::setFileDropCallback(FileDropCallbackFn callback) +{ + m_pFileDropCallback = callback; } void Onyx::Window::dispose() @@ -650,6 +671,8 @@ void Onyx::Window::framebufferSizeCallback(GLFWwindow* pGlfwWin, int width, int pRenderer->m_ortho = Projection::Orthographic(width, height).getMatrix(); } + if (pWin->m_pFramebufferSizeCallback) pWin->m_pFramebufferSizeCallback(width, height); + #if defined(ONYX_GL_DEBUG_HIGH) glCheckError(); #endif @@ -660,12 +683,16 @@ void Onyx::Window::windowSizeCallback(GLFWwindow* pGlfwWin, int width, int heigh Window* pWin = (Window*)glfwGetWindowUserPointer(pGlfwWin); pWin->m_properties.width = width; pWin->m_properties.height = height; + + if (pWin->m_pWindowSizeCallback) pWin->m_pWindowSizeCallback(width, height); } void Onyx::Window::windowPosCallback(GLFWwindow* pGlfwWin, int x, int y) { Window* pWin = (Window*)glfwGetWindowUserPointer(pGlfwWin); pWin->m_properties.position = Math::IVec2(x, y); + + if (pWin->m_pWindowPosCallback) pWin->m_pWindowPosCallback(x, y); } void Onyx::Window::keyCallback(GLFWwindow* pGlfwWin, int key, int scancode, int action, int mods) @@ -691,7 +718,7 @@ void Onyx::Window::cursorPosCallback(GLFWwindow* pGlfwWin, double x, double y) Window* pWin = (Window*)glfwGetWindowUserPointer(pGlfwWin); for (InputHandler* pInputHandler : pWin->m_pInputHandlers) { - pInputHandler->cursorPosCallback(x, ((Window*)glfwGetWindowUserPointer(pGlfwWin))->m_properties.height - y); + pInputHandler->mousePosCallback(x, ((Window*)glfwGetWindowUserPointer(pGlfwWin))->m_properties.height - y); } } @@ -716,5 +743,5 @@ void Onyx::Window::joystickCallback(int jid, int event) void Onyx::Window::fileDropCallback(GLFWwindow* pGlfwWin, int count, const char** paths) { Window* pWin = (Window*)glfwGetWindowUserPointer(pGlfwWin); - if (pWin->m_fileDropCallback != nullptr) pWin->m_fileDropCallback(paths, count); + if (pWin->m_pFileDropCallback) pWin->m_pFileDropCallback(paths, count); } diff --git a/src/Window.h b/src/Window.h index 2a8072d..899c8a7 100644 --- a/src/Window.h +++ b/src/Window.h @@ -209,6 +209,34 @@ namespace Onyx friend class Camera; friend class Renderer; + /* + @brief The framebuffer resize callback function signature. + @param width The new framebuffer width. + @param height The new framebuffer height. + */ + typedef void(*FramebufferSizeCallbackFn)(int width, int height); + + /* + @brief The window resize callback function signature. + @param width The new window width. + @param height The new window height. + */ + typedef void(*WindowSizeCallbackFn)(int width, int height); + + /* + @brief The window position callback function signature. + @param x The new X position of the window as a monitor coordinate. + @param y The new Y position of the window as a monitor coordinate. + */ + typedef void(*WindowPosCallbackFn)(int x, int y); + + /* + @brief The file drop callback function signature. + @param paths An array of string literals representing each file path. + @param count The number of file paths in the array. + */ + typedef void(*FileDropCallbackFn)(const char** paths, int count); + public: /* @brief Default constructor, initializes member variables. @@ -600,12 +628,32 @@ namespace Onyx void linkRenderer(Renderer& renderer); /* - @brief Sets the file drop callback. - This function receives an array of string literals representing filepaths, and the count of the filepaths. - You can do whatever you want with this. - @param callback The function to call when files are dropped onto the window. + @brief Sets the framebuffer resize callback function. + @param callback The callback function. + See the definition of `FramebufferSizeCallbackFn` for parameter info. + */ + void setFramebufferSizeCallback(FramebufferSizeCallbackFn callback); + + /* + @brief Sets the window resize callback function. + @param callback The callback function. + See the definition of `WindowSizeCallbackFn` for parameter info. + */ + void setWindowSizeCallback(WindowSizeCallbackFn callback); + + /* + @brief Sets the window position callback function. + @param callback The callback function. + See the definition of `WindowPosCallbackFn` for parameter info. + */ + void setWindowPosCallback(WindowPosCallbackFn callback); + + /* + @brief Sets the file drop callback function. + @param callback The callback function. + See the definition of `FileDropCallbackFn` for parameter info. */ - void setFileDropCallback(void (*callback)(const char**, int)); + void setFileDropCallback(FileDropCallbackFn callback); void dispose() override; @@ -632,7 +680,10 @@ namespace Onyx ulong m_numFramesCamNotUpdated; ulong m_numFramesInputNotUpdated; - void (*m_fileDropCallback)(const char**, int); + FramebufferSizeCallbackFn m_pFramebufferSizeCallback; + WindowSizeCallbackFn m_pWindowSizeCallback; + WindowPosCallbackFn m_pWindowPosCallback; + FileDropCallbackFn m_pFileDropCallback; static void framebufferSizeCallback(GLFWwindow* pGlfwWin, int width, int height); static void windowSizeCallback(GLFWwindow* pGlfwWin, int width, int height);