#pragma once // Must be the first include #include // Other includes #include #include "RenderBackend.h" #include #include #include namespace Gedeng { class Input { public: static void initialize(GLFWwindow *window) { glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); is_mouse_active = true; } // FIXME: Ignore warnings produced by these unused variables -- they're required for the callback to work: // https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); Input::set_key_down(key, action == GLFW_RELEASE ? false : true); } static void mouse_callback(GLFWwindow *window, double xpos, double ypos) { mouse_position.x = xpos; mouse_position.y = ypos; } static void poll_input() { glfwPollEvents(); } static void set_key_down(int key, bool down) { keys_down[key] = down; } static bool is_key_down(int key) { return keys_down[key]; } static glm::vec2 get_mouse_position() { return is_mouse_active ? mouse_position : glm::vec2(0.0, 0.0); } private: inline static std::map keys_down; inline static glm::vec2 mouse_position; inline static bool is_mouse_active; }; } // namespace Gedeng