-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyInput.hpp
More file actions
37 lines (34 loc) · 1.41 KB
/
keyInput.hpp
File metadata and controls
37 lines (34 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "GLFW/glfw3.h"
#include <map>
#include <vector>
//shoutout to this guy https://stackoverflow.com/questions/55573238/how-do-i-do-a-proper-input-class-in-glfw-for-a-game-engine
class KeyInput {
// Main KeyInput functionality
public:
// Takes a list of which keys to keep state for
KeyInput(std::vector<int> keysToMonitor);
~KeyInput();
// If this KeyInput is enabled and the given key is monitored,
// returns pressed state. Else returns false.
bool getIsKeyDown(int key);
// See _isEnabled for details
bool getIsEnabled() { return _isEnabled; }
void setIsEnabled(bool value) { _isEnabled = value; }
private:
// Used internally to update key states. Called by the GLFW callback.
void setIsKeyDown(int key, bool isDown);
// Map from monitored keyes to their pressed states
std::map<int, bool> _keys;
// If disabled, KeyInput.getIsKeyDown always returns false
bool _isEnabled;
// Workaround for C++ class using a c-style-callback
public:
// Must be called before any KeyInput instances will work
static void setupKeyInputs(GLFWwindow* window);
private:
// The GLFW callback for key events. Sends events to all KeyInput instances
static void callback(
GLFWwindow* window, int key, int scancode, int action, int mods);
// Keep a list of all KeyInput instances and notify them all of key events
static std::vector<KeyInput*> _instances;
};