diff --git a/engine/NBGraphics/include/NBGraphics/Window.hpp b/engine/NBGraphics/include/NBGraphics/Window.hpp index ff96529..881b0d8 100644 --- a/engine/NBGraphics/include/NBGraphics/Window.hpp +++ b/engine/NBGraphics/include/NBGraphics/Window.hpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -38,6 +39,12 @@ 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; @@ -95,6 +102,7 @@ class OGLWindow { const std::vector>& integerHints={}, const std::vector>& stringHints={} ); + OGLWindow(OGLWindow&&) = delete; OGLWindow(const OGLWindow&) = delete; ~OGLWindow(); @@ -106,14 +114,15 @@ class OGLWindow { inline std::string getTitle() const { return _title; } std::array getSize() const; bool shouldClose() const; - inline void processEvents() const { CheckForMainThread(); glfwPollEvents(); } - inline void refresh() const { glfwSwapBuffers(_window_ptr); } + inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); } + inline void swapBuffers() const { glfwSwapBuffers(_window_ptr); } void setSize(unsigned int x, unsigned int y); void setTitle(const std::string&); void shouldClose(bool); -/* template - void setCallBack(Func); */ + + template + void setCallback(std::function callback); }; }; diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index 383f749..d45460e 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -1,5 +1,7 @@ #include +#include + namespace nb { using GLFWCodes = GLFWError::Codes; @@ -60,14 +62,14 @@ void OGLWindow::SetHintString(const std::tuple& hint) { GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr& window) { auto tmp = _currWindow; - _currWindow = window->getWindowPtr(); if (window) { - glfwMakeContextCurrent(_currWindow); + _currWindow = window->getWindowPtr(); _currArena = window->_arena; } else { - glfwMakeContextCurrent(nullptr); + _currWindow = nullptr; _currArena = nullptr; } + glfwMakeContextCurrent(_currWindow); return tmp; } @@ -200,14 +202,37 @@ void OGLWindow::setTitle(const std::string& name) { glfwSetWindowTitle(_window_ptr, _title.c_str()); } -/* template<> void OGLWindow::setCallBack( CallbackFunc func) { +template<> +void OGLWindow::setCallback(std::function callback) { CheckForMainThread(); auto tmp = makeCurrent(); - auto tmp_func = - [=](GLFWwindow* window, int a, int b, int c, int d){ - func(this, a, b, c, d); - }; - glfwSetKeyCallback(_window_ptr, tmp_func); -} */ + 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 72f12e1..fe9cc24 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -69,13 +69,22 @@ int main() { }}, indxs); LOG(prog.log()); - GLFWwindow* window_ptr = window->getWindowPtr(); - while(!glfwWindowShouldClose(window_ptr)) { + window->setCallback( + (std::function)[](nb::OGLWindow* w, int key, int x, int actiom, int mods)->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)); + }); + while(!window->shouldClose()) { OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT)); tri.draw(); - glfwPollEvents(); - glfwSwapBuffers(window_ptr); + window->pollEvents(); + window->swapBuffers(); } return 0;