-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputsystem.h
94 lines (69 loc) · 2.2 KB
/
inputsystem.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef INPUTSYSTEM_H
#define INPUTSYSTEM_H
#include "glm/glm.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <unordered_map>
#include <string>
struct KeyPress{
int key;
int scancode;
int action;
int mods;
};
class InputSystem{
public:
std::unordered_map<int, KeyPress> pressedKeys;
glm::vec2 lastMouseAxis;
glm::vec2 mouseAxis;
float mouseX;
float mouseY;
float lastX;
float lastY;
bool firstMouse = true;
static InputSystem& GetInstance(){
static InputSystem instance;
return instance;
}
void SetKey(KeyPress kp){
pressedKeys[kp.key] = kp;
}
bool GetKey(int keyval){
int action = pressedKeys[keyval].action;
if (action == GLFW_PRESS || action == GLFW_REPEAT)
return true;
return false;
}
glm::vec2 GetMouseAxis(){
if (firstMouse)
{
lastX = mouseAxis.x;
lastY = mouseAxis.y;
firstMouse = false;
}
mouseX = mouseAxis.x - lastX;
mouseY = lastY - mouseAxis.y;
lastX = mouseAxis.x;
lastY = mouseAxis.y;
return glm::vec2(mouseX, mouseY);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
GetInstance().key_callback_impl(window, key, scancode, action, mods);
}
void key_callback_impl(GLFWwindow* window, int key, int scancode, int action, int mods){
SetKey({key, scancode, action, mods});
}
static void mouse_callback(GLFWwindow* window, double xposIn, double yposIn){
GetInstance().mouse_callback_impl(window, xposIn, yposIn);
}
void mouse_callback_impl(GLFWwindow* window, double xposIn, double yposIn){
mouseAxis = glm::vec2(xposIn, yposIn);
}
private:
InputSystem(void) // private constructor necessary to allow only 1 instance
{
}
InputSystem(InputSystem const&); // prevent copies
void operator=(InputSystem const&);
};
#endif