From 1d0869794b82de33fa2e7c95c5be9076697090e6 Mon Sep 17 00:00:00 2001 From: MrLetsplay2003 Date: Mon, 31 Oct 2022 21:52:22 +0100 Subject: [PATCH] Improve input --- src/kekengine/cpp/engine.cpp | 16 ++++++++++++---- src/kekengine/cpp/input.cpp | 23 +++++++++++++++++------ src/kekengine/include/constants.h | 2 +- src/kekengine/include/input.h | 16 +++++++--------- src/kekengine/include/internal.h | 1 + src/kekgame/cpp/kekgame.cpp | 23 +++++++++++++++++++++++ 6 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/kekengine/cpp/engine.cpp b/src/kekengine/cpp/engine.cpp index a5a3043..237e8f2 100644 --- a/src/kekengine/cpp/engine.cpp +++ b/src/kekengine/cpp/engine.cpp @@ -43,7 +43,7 @@ static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum se std::cout << "OpenGL Debug (" << id << "): " << message << std::endl; } -void onCursorPosCallback(GLFWwindow *window, double x, double y) { +static void onCursorPosCallback(GLFWwindow *window, double x, double y) { static bool firstMouse = true; static double lastX = 0, lastY = 0; if(firstMouse) { @@ -63,17 +63,23 @@ void onCursorPosCallback(GLFWwindow *window, double x, double y) { kekData.activeCamera->rotateYaw(xoff); kekData.activeCamera->rotatePitch(yoff); - for(std::pair cb : kekData.mouseCallbacks) { + for(auto cb : kekData.mouseCallbacks) { cb.second(window, x, y); } } -void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - for(std::pair cb : kekData.keyCallbacks) { +static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { + for(auto cb : kekData.keyCallbacks) { cb.second(window, key, scancode, action, mods); } } +void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { + for(auto cb : kekData.mouseButtonCallbacks) { + cb.second(window, button, action, mods); + } +} + int init() { // Init GLFW if (glfwInit() != GL_TRUE) { @@ -155,6 +161,8 @@ int init() { glfwSetCursorPosCallback(kekData.window, onCursorPosCallback); glfwSetInputMode(kekData.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetKeyCallback(kekData.window, onKeyCallback); + glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback); glClearColor(0.1f, 0.3f, 0.1f, 0.0f); glfwSwapInterval(0); diff --git a/src/kekengine/cpp/input.cpp b/src/kekengine/cpp/input.cpp index ce3f191..161643b 100644 --- a/src/kekengine/cpp/input.cpp +++ b/src/kekengine/cpp/input.cpp @@ -38,6 +38,16 @@ void removeMouseListener(InputListener listener) { kekData.mouseCallbacks.erase(listener); } +InputListener addMouseButtonListener(MouseButtonCallback callback) { + InputListener id = nextID++; + kekData.mouseButtonCallbacks.emplace(id, callback); + return id; +} + +void removeMouseButtonListener(InputListener listener) { + kekData.mouseButtonCallbacks.erase(listener); +} + KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) { if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING; KeyBinding id = nextID++; @@ -48,22 +58,23 @@ KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) { return id; } -void reassignKeyBinding(KeyBinding mapping, GLFWKey key) { - auto it = kekData.keyBindings.find(mapping); +void reassignKeyBinding(KeyBinding binding, GLFWKey key) { + if(binding == KEK_INVALID_KEY_BINDING) return; + auto it = kekData.keyBindings.find(binding); if(it == kekData.keyBindings.end()) return; KeyBindingData d = it->second; d.key = key; it->second = d; } -KeyBindingData getKeyBinding(KeyBinding mapping) { - auto it = kekData.keyBindings.find(mapping); +KeyBindingData getKeyBinding(KeyBinding binding) { + auto it = kekData.keyBindings.find(binding); if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME}; return it->second; } -GLFWKeyState getKeyState(KeyBinding mapping) { - auto it = kekData.keyBindings.find(mapping); +GLFWKeyState getKeyState(KeyBinding binding) { + auto it = kekData.keyBindings.find(binding); if(it == kekData.keyBindings.end()) return -1; return glfwGetKey(kekData.window, it->second.key); } diff --git a/src/kekengine/include/constants.h b/src/kekengine/include/constants.h index 795dba9..8d2265f 100644 --- a/src/kekengine/include/constants.h +++ b/src/kekengine/include/constants.h @@ -25,7 +25,7 @@ #define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE) #define KEK_INVALID_KEY_BINDING_NAME "INVALID" -#define KEK_INVALID_KEY_BINDING -1 +#define KEK_INVALID_KEY_BINDING -1u #define KEK_FONT_RESOLUTION 64 #define KEK_FONT_BITMAP_WIDTH_BLOCKS 16 diff --git a/src/kekengine/include/input.h b/src/kekengine/include/input.h index de54eea..c8c7697 100644 --- a/src/kekengine/include/input.h +++ b/src/kekengine/include/input.h @@ -10,12 +10,13 @@ namespace kek { typedef generic_callable_t PeriodicCallback; // periodicCallback(GLFWwindow *window) typedef generic_callable_t KeyCallback; // keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) typedef generic_callable_t MouseCallback; // mouseCallback(GLFWwindow *window, double x, double y) +typedef generic_callable_t MouseButtonCallback; // void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) typedef unsigned int InputListener; -typedef unsigned int GLFWKey; -typedef unsigned int GLFWKeyState; +typedef int GLFWKey; +typedef int GLFWKeyState; typedef unsigned int KeyBinding; -typedef unsigned int GLFWMouseButton; +typedef int GLFWMouseButton; struct KeyBindingData { std::string name; @@ -27,23 +28,20 @@ struct KeyBindingData { namespace kek::Input { InputListener addPeriodicCallback(PeriodicCallback callback); - void removePeriodicCallback(InputListener listener); InputListener addKeyListener(KeyCallback callback); - void removeKeyListener(InputListener listener); InputListener addMouseListener(MouseCallback callback); - void removeMouseListener(InputListener listener); +InputListener addMouseButtonListener(MouseButtonCallback callback); +void removeMouseButtonListener(InputListener listener); + KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey); - void reassignKeyBinding(KeyBinding mapping, GLFWKey key); - KeyBindingData getKeyBinding(KeyBinding mapping); - GLFWKeyState getKeyState(KeyBinding mapping); } diff --git a/src/kekengine/include/internal.h b/src/kekengine/include/internal.h index 96ebc7f..5d938c6 100644 --- a/src/kekengine/include/internal.h +++ b/src/kekengine/include/internal.h @@ -17,6 +17,7 @@ struct KekData { std::map periodicCallbacks; std::map keyCallbacks; std::map mouseCallbacks; + std::map mouseButtonCallbacks; std::map keyBindings; GLFWwindow *window; diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index bd6c6d3..f524927 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -6,6 +6,24 @@ using namespace kek; +static KeyBinding keyWow; + +void periodicCallback(GLFWwindow *window, void *data){ + if(Input::getKeyState(keyWow) == GLFW_PRESS) { + //std::cout << "WOW" << std::endl; + } +} + +void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data){ + if(key == Input::getKeyBinding(keyWow).key && action == GLFW_PRESS) { + std::cout << "WOW" << std::endl; + } +} + +void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data){ + std::cout << "HELLO" << std::endl; +} + int main(int argc, char **argv) { Engine::init(); @@ -38,5 +56,10 @@ int main(int argc, char **argv) { Engine::setActiveScene(scene); + keyWow = Input::createKeyBinding("wow", GLFW_KEY_O); + Input::addPeriodicCallback(PeriodicCallback(periodicCallback, nullptr)); + Input::addKeyListener(KeyCallback(keyCallback, nullptr)); + Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr)); + Engine::start(); }