-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
109 lines (79 loc) · 2.7 KB
/
Input.cpp
File metadata and controls
109 lines (79 loc) · 2.7 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Input.h"
#include <iostream>
namespace Engine
{
namespace Input
{
bool m_anyKeyPressed = false;
bool m_anyMouseKeyPressed = false;
//Mouse
double m_mousePosX = 0, m_mousePosY = 0;
double m_lastMousePosX = 0, m_lastMousePosY = 0;
bool m_invertMouseX, m_invertMouseY;
//Update mouse info
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
m_anyMouseKeyPressed = true;
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
//log_printf(log_level_e::LOG_INFO, "Pressed key %i\n",key);
m_anyKeyPressed = true;
}
void Input::Init(GLFWwindow** rendererWindow) {
m_rendererWindow = rendererWindow;
m_window = *m_rendererWindow;
UpdateWindow();
//glfwSetInputMode(m_window, GLFW_STICKY_KEYS, GLFW_TRUE);//the key will maintain its state untill polled with glfwGetKey
log_message(log_level_e::LOG_INFO, " Initialized Input");
}
void Input::UpdateWindow() {
//Unbind callbacks
glfwSetCursorPosCallback(m_window, NULL);
glfwSetScrollCallback(m_window, NULL);
glfwSetMouseButtonCallback(m_window, NULL);
//Binding callbacks to the new window
m_window = *m_rendererWindow;
glfwSetCursorPosCallback(m_window, mouse_callback);
glfwSetScrollCallback(m_window, scroll_callback);
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
glfwSetKeyCallback(m_window, key_callback);
}
void Input::processInput() {
//UpdateMouse movement
double xpos, ypos;
glfwGetCursorPos(m_window, &xpos, &ypos);
//Horizontal axis
m_lastMousePosX = m_mousePosX;
m_mousePosX = xpos;
//Vertical Axis
m_lastMousePosY = m_mousePosY;
m_mousePosY = ypos;
//Reset anykey pressed
m_anyKeyPressed = false;
m_anyMouseKeyPressed = false;
}
bool Input::anyKey() { return m_anyKeyPressed; }
bool Input::anyMouseKey() { return m_anyMouseKeyPressed; }
double Input::getMousePositionX() { return m_mousePosX; }
double Input::getMousePositionY() { return m_mousePosY; }
float Input::getMouseDeltaX() {
return (float)((m_invertMouseX) ? -1 : 1) * (m_mousePosX - m_lastMousePosX);
}
float Input::getMouseDeltaY() {
return (float)((m_invertMouseY) ? -1 : 1) * (m_lastMousePosY - m_mousePosY);
}
void Input::invertMouse(bool xAxis, bool yAxis) {
m_invertMouseX = xAxis;
m_invertMouseY = yAxis;
}
KeyCode Input::getKey(KeyCode keyCode) {
return glfwGetKey(m_window, keyCode);
}
KeyCode Input::getMouseButton(KeyCode keyCode) {
return glfwGetMouseButton(m_window, keyCode);
}
}
}