Added callbacks
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -38,6 +39,12 @@ class OGLWindow {
|
|||||||
THROW(GLFWError(GLFWError::Codes::NOT_MAIN));
|
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:
|
protected:
|
||||||
static std::atomic<unsigned int> _count;
|
static std::atomic<unsigned int> _count;
|
||||||
@@ -95,6 +102,7 @@ class OGLWindow {
|
|||||||
const std::vector<std::array<int, 2>>& integerHints={},
|
const std::vector<std::array<int, 2>>& integerHints={},
|
||||||
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||||
);
|
);
|
||||||
|
|
||||||
OGLWindow(OGLWindow&&) = delete;
|
OGLWindow(OGLWindow&&) = delete;
|
||||||
OGLWindow(const OGLWindow&) = delete;
|
OGLWindow(const OGLWindow&) = delete;
|
||||||
~OGLWindow();
|
~OGLWindow();
|
||||||
@@ -106,14 +114,15 @@ class OGLWindow {
|
|||||||
inline std::string getTitle() const { return _title; }
|
inline std::string getTitle() const { return _title; }
|
||||||
std::array<int, 2> getSize() const;
|
std::array<int, 2> getSize() const;
|
||||||
bool shouldClose() const;
|
bool shouldClose() const;
|
||||||
inline void processEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||||
inline void refresh() const { glfwSwapBuffers(_window_ptr); }
|
inline void swapBuffers() const { glfwSwapBuffers(_window_ptr); }
|
||||||
|
|
||||||
void setSize(unsigned int x, unsigned int y);
|
void setSize(unsigned int x, unsigned int y);
|
||||||
void setTitle(const std::string&);
|
void setTitle(const std::string&);
|
||||||
void shouldClose(bool);
|
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 <NBGraphics/Window.hpp>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
using GLFWCodes = GLFWError::Codes;
|
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) {
|
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||||
auto tmp = _currWindow;
|
auto tmp = _currWindow;
|
||||||
_currWindow = window->getWindowPtr();
|
|
||||||
if (window) {
|
if (window) {
|
||||||
glfwMakeContextCurrent(_currWindow);
|
_currWindow = window->getWindowPtr();
|
||||||
_currArena = window->_arena;
|
_currArena = window->_arena;
|
||||||
} else {
|
} else {
|
||||||
glfwMakeContextCurrent(nullptr);
|
_currWindow = nullptr;
|
||||||
_currArena = nullptr;
|
_currArena = nullptr;
|
||||||
}
|
}
|
||||||
|
glfwMakeContextCurrent(_currWindow);
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,14 +202,37 @@ void OGLWindow::setTitle(const std::string& name) {
|
|||||||
glfwSetWindowTitle(_window_ptr, _title.c_str());
|
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();
|
CheckForMainThread();
|
||||||
auto tmp = makeCurrent();
|
auto tmp = makeCurrent();
|
||||||
auto tmp_func =
|
static auto this_ptr = this;
|
||||||
[=](GLFWwindow* window, int a, int b, int c, int d){
|
static auto tmp_callback = callback;
|
||||||
func(this, a, b, c, d);
|
glfwSetKeyCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c, int d){
|
||||||
};
|
tmp_callback(this_ptr, a, b, c, d);
|
||||||
glfwSetKeyCallback(_window_ptr, tmp_func);
|
});
|
||||||
} */
|
}
|
||||||
|
|
||||||
|
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);
|
}}, indxs);
|
||||||
LOG(prog.log());
|
LOG(prog.log());
|
||||||
|
|
||||||
GLFWwindow* window_ptr = window->getWindowPtr();
|
window->setCallback<nb::OGLWindow::KEY>(
|
||||||
while(!glfwWindowShouldClose(window_ptr)) {
|
(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(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
|
||||||
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
||||||
tri.draw();
|
tri.draw();
|
||||||
glfwPollEvents();
|
window->pollEvents();
|
||||||
glfwSwapBuffers(window_ptr);
|
window->swapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user