Added callbacks
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <NBGraphics/OGLObjects.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -38,6 +39,12 @@ class OGLWindow {
|
||||
THROW(GLFWError(GLFWError::Codes::NOT_MAIN));
|
||||
}
|
||||
}
|
||||
template<typename... Args>
|
||||
using CallbackFuncType = void (*)(GLFWwindow*, Args...);
|
||||
template<typename... Args>
|
||||
struct CallbackSetterFuncType {
|
||||
typedef CallbackFuncType<Args...> (*type)(GLFWwindow*, CallbackFuncType<Args...>);
|
||||
};
|
||||
|
||||
protected:
|
||||
static std::atomic<unsigned int> _count;
|
||||
@@ -95,6 +102,7 @@ class OGLWindow {
|
||||
const std::vector<std::array<int, 2>>& integerHints={},
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||
);
|
||||
|
||||
OGLWindow(OGLWindow&&) = delete;
|
||||
OGLWindow(const OGLWindow&) = delete;
|
||||
~OGLWindow();
|
||||
@@ -106,14 +114,15 @@ class OGLWindow {
|
||||
inline std::string getTitle() const { return _title; }
|
||||
std::array<int, 2> 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<enum Callbacks, typename Func>
|
||||
void setCallBack(Func); */
|
||||
|
||||
template<enum Callbacks, typename Func>
|
||||
void setCallback(std::function<Func> callback);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <NBGraphics/Window.hpp>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace nb {
|
||||
|
||||
using GLFWCodes = GLFWError::Codes;
|
||||
@@ -60,14 +62,14 @@ void OGLWindow::SetHintString(const std::tuple<int, std::string>& hint) {
|
||||
|
||||
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& 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<OGLWindow::KEY>( CallbackFunc<int,int,int,int> func) {
|
||||
template<>
|
||||
void OGLWindow::setCallback<OGLWindow::KEY>(std::function<void(OGLWindow*, int, int, int, int)> 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<OGLWindow::MOUSE_BUTTON>(std::function<void(OGLWindow*, int, int, int)> 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<OGLWindow::CURSOR_POS>(std::function<void(OGLWindow*, double, double)> 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);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
@@ -69,13 +69,22 @@ int main() {
|
||||
}}, indxs);
|
||||
LOG(prog.log());
|
||||
|
||||
GLFWwindow* window_ptr = window->getWindowPtr();
|
||||
while(!glfwWindowShouldClose(window_ptr)) {
|
||||
window->setCallback<nb::OGLWindow::KEY>(
|
||||
(std::function<void(nb::OGLWindow*, int, int, int, int)>)[](nb::OGLWindow* w, int key, int x, int actiom, int mods)->void{
|
||||
if (key == GLFW_KEY_Q) {
|
||||
w->shouldClose(true);
|
||||
}
|
||||
});
|
||||
window->setCallback<nb::OGLWindow::CURSOR_POS>(
|
||||
(std::function<void(nb::OGLWindow*, double, double)>)[](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;
|
||||
|
||||
Reference in New Issue
Block a user