diff --git a/engine/NBGraphics/include/NBGraphics/Window.hpp b/engine/NBGraphics/include/NBGraphics/Window.hpp index 881b0d8..7bb5956 100644 --- a/engine/NBGraphics/include/NBGraphics/Window.hpp +++ b/engine/NBGraphics/include/NBGraphics/Window.hpp @@ -31,6 +31,86 @@ class GLFWError : public Error { static const ErrorCodeMap ErrorMessages; }; +class OGLWindow; + +enum WindowCallbacks { + KEY, CHAR, CHAR_MODS, MOUSE_BUTTON, CURSOR_POS, CURSOR_ENTER, + SCROLL, DROP, WINDOW_POS, WINDOW_SIZE, WINDOW_CLOSE, WINDOW_REFRESH, + WINDOW_FOCUS, WINDOW_ICONIFY, WINDOW_MAX, FRAMEBUFFER_SIZE, WINDOW_SCALE, +}; + +template +struct OGLWindowCallbacks; + +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; +template<> +struct OGLWindowCallbacks { + using type = typename std::function; +}; + class OGLWindow { using GLFWHint = std::array; private: @@ -39,12 +119,6 @@ class OGLWindow { THROW(GLFWError(GLFWError::Codes::NOT_MAIN)); } } - template - using CallbackFuncType = void (*)(GLFWwindow*, Args...); - template - struct CallbackSetterFuncType { - typedef CallbackFuncType (*type)(GLFWwindow*, CallbackFuncType); - }; protected: static std::atomic _count; @@ -63,21 +137,14 @@ class OGLWindow { unsigned int, unsigned int, const std::string&, - const std::vector>&, - const std::vector>&, + const std::vector>& integerWindowHints, + const std::vector>& stringWindowHints={}, GLFWwindow* sharingContext = NULL ); GLFWwindow* makeCurrent() const; void detachWindow(); public: - enum Callbacks { - KEY, CHAR, CHAR_MODS, MOUSE_BUTTON, CURSOR_POS, CURSOR_ENTER, - SCROLL, DROP, WINDOW_POS, WINDOW_SIZE, WINDOW_CLOSE, WINDOW_REFRESH, - WINDOW_FOCUS, WINDOW_ICONIFY, WINDOW_MAX, FRAMEBUFFER_SIZE, WINDOW_SCALE, - }; - template - using CallbackFunc = void(*)(OGLWindow*, T...); static inline bool IsGLFWInitialized() { return _glfwInitialized; } static bool InitGLFW(const std::vector& hints={}); @@ -121,8 +188,84 @@ class OGLWindow { void setTitle(const std::string&); void shouldClose(bool); - template - void setCallback(std::function callback); + template + void setCallback(CallbackFunc callback) { + CheckForMainThread(); + auto tmp = makeCurrent(); + static auto this_ptr = this; + static typename OGLWindowCallbacks::type tmp_callback = callback; + if constexpr (CallbackType == KEY) { + glfwSetKeyCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c, int d){ + tmp_callback(this_ptr, a,b,c,d); + }); + } else if constexpr (CallbackType == CHAR) { + glfwSetCharCallback(_window_ptr, [](GLFWwindow*, int a) { + tmp_callback(this_ptr, a); + }); + } else if constexpr (CallbackType == CHAR_MODS) { + glfwSetCharModsCallback(_window_ptr, [](GLFWwindow*, unsigned int a, int b) { + tmp_callback(this_ptr, a, b); + }); + } else if constexpr (CallbackType == CURSOR_ENTER) { + glfwSetCursorEnterCallback(_window_ptr, [](GLFWwindow*, int a) { + tmp_callback(this_ptr, a); + }); + } else if constexpr (CallbackType == CURSOR_POS) { + glfwSetCursorPosCallback(_window_ptr, [](GLFWwindow*, double a, double b){ + tmp_callback(this_ptr, a,b); + }); + } else if constexpr (CallbackType == MOUSE_BUTTON) { + glfwSetMouseButtonCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c){ + tmp_callback(this_ptr, a,b,c); + }); + } else if constexpr (CallbackType == SCROLL) { + glfwSetScrollCallback(_window_ptr, [](GLFWwindow*, double a, double b) { + tmp_callback(this_ptr, a,b); + }); + } else if constexpr (CallbackType == DROP) { + glfwSetDropCallback(_window_ptr, [](GLFWwindow*, int a, const char** b) { + tmp_callback(this_ptr, a, b); + }); + } else if constexpr (CallbackType == WINDOW_POS) { + glfwSetWindowPosCallback(_window_ptr, [](GLFWwindow*, int a, int b){ + tmp_callback(this_ptr, a,b); + }); + } else if constexpr (CallbackType == WINDOW_SIZE) { + glfwSetWindowSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b){ + tmp_callback(this_ptr, a,b); + }); + } else if constexpr (CallbackType == WINDOW_CLOSE) { + glfwSetWindowCloseCallback(_window_ptr, [](GLFWwindow*){ + tmp_callback(this_ptr); + }); + } else if constexpr (CallbackType == WINDOW_REFRESH) { + glfwSetWindowRefreshCallback(_window_ptr, [](GLFWwindow*){ + tmp_callback(this_ptr); + }); + } else if constexpr (CallbackType == WINDOW_FOCUS) { + glfwSetWindowFocusCallback(_window_ptr, [](GLFWwindow*, int a) { + tmp_callback(this_ptr, a); + }); + } else if constexpr (CallbackType == WINDOW_ICONIFY) { + glfwSetWindowIconifyCallback(_window_ptr, [](GLFWwindow*, int a) { + tmp_callback(this_ptr, a); + }); + } else if constexpr (CallbackType == WINDOW_MAX) { + glfwSetWindowMaximizeCallback(_window_ptr, [](GLFWwindow*, int a) { + tmp_callback(this_ptr, a); + }); + } else if constexpr (CallbackType == FRAMEBUFFER_SIZE) { + glfwSetFramebufferSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b) { + tmp_callback(this_ptr, a, b); + }); + } else if constexpr (CallbackType == WINDOW_SCALE) { + glfwSetWindowContentScaleCallback(_window_ptr, [](GLFWwindow*, int a, int b) { + tmp_callback(this_ptr, a, b); + }); + } else { + static_assert(false, "No matching callback."); + } + } }; }; diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index d45460e..830ada6 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -202,37 +202,4 @@ void OGLWindow::setTitle(const std::string& name) { glfwSetWindowTitle(_window_ptr, _title.c_str()); } -template<> -void OGLWindow::setCallback(std::function callback) { - CheckForMainThread(); - auto tmp = makeCurrent(); - static auto this_ptr = this; - static auto tmp_callback = callback; - glfwSetKeyCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c, int d){ - tmp_callback(this_ptr, a, b, c, d); - }); -} - -template<> -void OGLWindow::setCallback(std::function callback) { - CheckForMainThread(); - auto tmp = makeCurrent(); - static auto this_ptr = this; - static auto tmp_callback = callback; - glfwSetMouseButtonCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c){ - tmp_callback(this_ptr, a, b, c); - }); -} - -template<> -void OGLWindow::setCallback(std::function callback) { - CheckForMainThread(); - auto tmp = makeCurrent(); - static auto this_ptr = this; - static auto tmp_callback = callback; - glfwSetCursorPosCallback(_window_ptr, [](GLFWwindow*, double a, double b){ - tmp_callback(this_ptr, a, b); - }); -} - }; \ No newline at end of file diff --git a/engine/NBGraphics/tests/TestWindow.cpp b/engine/NBGraphics/tests/TestWindow.cpp index fe9cc24..a3c815e 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -69,16 +69,19 @@ int main() { }}, indxs); LOG(prog.log()); - window->setCallback( - (std::function)[](nb::OGLWindow* w, int key, int x, int actiom, int mods)->void{ + using nb::WindowCallbacks; + + window->setCallback( + [](nb::OGLWindow* w, int key, int, int, int)->void{ if (key == GLFW_KEY_Q) { w->shouldClose(true); } }); - window->setCallback( - (std::function)[](nb::OGLWindow* w, double x, double y)->void{ - nb::logger.msg("Pos in x is " + std::to_string(x)); - }); + window->setCallback( + [](nb::OGLWindow* w, double x, double y)->void{ + nb::logger.msg("Pos in x is " + std::to_string(x)); + } + ); while(!window->shouldClose()) { OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));