76 lines
2.1 KiB
C++
76 lines
2.1 KiB
C++
#pragma once
|
|
|
|
// Must be the first include
|
|
#include <glad/glad.h>
|
|
|
|
// Other includes
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "RenderBackend.h"
|
|
#include <glm/fwd.hpp>
|
|
#include <glm/glm.hpp>
|
|
#include <map>
|
|
|
|
namespace Gedeng {
|
|
|
|
class Input {
|
|
public:
|
|
static void initialize(GLFWwindow *window) {
|
|
glfwSetKeyCallback(window, key_callback);
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
glfwSetMouseButtonCallback(window, mouse_button_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);
|
|
|
|
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 mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
|
|
set_mouse_down(button, action == GLFW_RELEASE ? false : true);
|
|
}
|
|
|
|
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 void set_mouse_down(int button, bool down) {
|
|
mouse_down[button] = down;
|
|
}
|
|
|
|
static bool is_mouse_down(int button) {
|
|
return mouse_down[button];
|
|
}
|
|
|
|
static glm::vec2 get_mouse_position() {
|
|
return is_mouse_active ? mouse_position : glm::vec2(0.0, 0.0);
|
|
}
|
|
|
|
private:
|
|
inline static std::map<int, bool> keys_down;
|
|
inline static std::map<int, bool> mouse_down;
|
|
|
|
inline static glm::vec2 mouse_position;
|
|
inline static bool is_mouse_active;
|
|
};
|
|
|
|
} // namespace Gedeng
|