From abf32feb0dd389d4530cada7bad314b2e19d6630 Mon Sep 17 00:00:00 2001 From: allem Date: Tue, 11 Dec 2018 00:09:36 -0500 Subject: [PATCH] tweak event handlers --- framework/include/framework.h | 8 ++++++-- framework/src/framework.cpp | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/framework/include/framework.h b/framework/include/framework.h index ca2a265..8937cef 100644 --- a/framework/include/framework.h +++ b/framework/include/framework.h @@ -16,6 +16,8 @@ private: static void onSize(GLFWwindow *window, int width, int height); + static void onCursorPos(GLFWwindow *window, double x, double y); + protected: App(int gl_major, int gl_minor); @@ -33,16 +35,18 @@ protected: void setSize(int width, int height); + void swapBuffers(); + virtual void onKey(int key, int scan_code, int action, int mods) {} virtual void onSize(int width, int height) {} + virtual void onCursorPos(double x, double y) {} + virtual void init() {} virtual void display() {} - void swapBuffers(); - public: GLFWwindow *getWindow(); diff --git a/framework/src/framework.cpp b/framework/src/framework.cpp index f0991b9..e4598b1 100644 --- a/framework/src/framework.cpp +++ b/framework/src/framework.cpp @@ -102,18 +102,26 @@ void App::close() { std::unordered_map manager; -void App::onKey(GLFWwindow *window, int key, int scan_code, int action, int mods) { +App *get(GLFWwindow *window) { auto m = manager.find(window); + if (m == manager.end()) + return nullptr; + return m->second; +} - if (m != manager.end()) - m->second->onKey(key, scan_code, action, mods); +void App::onKey(GLFWwindow *window, int key, int scan_code, int action, int mods) { + auto *app = get(window); + if (app) app->onKey(key, scan_code, action, mods); } 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()) - m->second->onSize(width, height); +void App::onCursorPos(GLFWwindow *window, double x, double y) { + auto *app = get(window); + if (app) app->onCursorPos(x, y); } int App::run() { @@ -128,6 +136,7 @@ int App::run() { glfwSetWindowSizeCallback(getWindow(), App::onSize); glfwSetKeyCallback(getWindow(), App::onKey); + glfwSetCursorPosCallback(getWindow(), App::onCursorPos); glfwMakeContextCurrent(_window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);