tweak event handlers

This commit is contained in:
2018-12-11 00:09:36 -05:00
parent c3c1c80c6d
commit abf32feb0d
2 changed files with 21 additions and 8 deletions

View File

@@ -16,6 +16,8 @@ private:
static void onSize(GLFWwindow *window, int width, int height); static void onSize(GLFWwindow *window, int width, int height);
static void onCursorPos(GLFWwindow *window, double x, double y);
protected: protected:
App(int gl_major, int gl_minor); App(int gl_major, int gl_minor);
@@ -33,16 +35,18 @@ protected:
void setSize(int width, int height); void setSize(int width, int height);
void swapBuffers();
virtual void onKey(int key, int scan_code, int action, int mods) {} virtual void onKey(int key, int scan_code, int action, int mods) {}
virtual void onSize(int width, int height) {} virtual void onSize(int width, int height) {}
virtual void onCursorPos(double x, double y) {}
virtual void init() {} virtual void init() {}
virtual void display() {} virtual void display() {}
void swapBuffers();
public: public:
GLFWwindow *getWindow(); GLFWwindow *getWindow();

View File

@@ -102,18 +102,26 @@ void App::close() {
std::unordered_map<GLFWwindow *, App *> manager; std::unordered_map<GLFWwindow *, App *> manager;
void App::onKey(GLFWwindow *window, int key, int scan_code, int action, int mods) { App *get(GLFWwindow *window) {
auto m = manager.find(window); auto m = manager.find(window);
if (m == manager.end())
return nullptr;
return m->second;
}
if (m != manager.end()) void App::onKey(GLFWwindow *window, int key, int scan_code, int action, int mods) {
m->second->onKey(key, scan_code, action, mods); auto *app = get(window);
if (app) app->onKey(key, scan_code, action, mods);
} }
void App::onSize(GLFWwindow *window, int width, int height) { void App::onSize(GLFWwindow *window, int width, int height) {
auto m = manager.find(window); auto *app = get(window);
if (app) app->onSize(width, height);
}
if (m != manager.end()) void App::onCursorPos(GLFWwindow *window, double x, double y) {
m->second->onSize(width, height); auto *app = get(window);
if (app) app->onCursorPos(x, y);
} }
int App::run() { int App::run() {
@@ -128,6 +136,7 @@ int App::run() {
glfwSetWindowSizeCallback(getWindow(), App::onSize); glfwSetWindowSizeCallback(getWindow(), App::onSize);
glfwSetKeyCallback(getWindow(), App::onKey); glfwSetKeyCallback(getWindow(), App::onKey);
glfwSetCursorPosCallback(getWindow(), App::onCursorPos);
glfwMakeContextCurrent(_window); glfwMakeContextCurrent(_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);