-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
83 lines (70 loc) · 2.44 KB
/
Input.cpp
File metadata and controls
83 lines (70 loc) · 2.44 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
#include "Input.h"
#include <cstdio>
void InputManager::Init(Window* window)
{
this->_window = window;
// Setting the key event callback
glfwSetWindowUserPointer(this->_window->GetWindow(), this);
glfwSetKeyCallback(this->_window->GetWindow(), sKeyCallback);
glfwSetCursorPosCallback(this->_window->GetWindow(), sCursorPosCallback);
// Setting a default value for the key map
for (int i = 0; i <= 348; i++)
this->_keyMap[i] = KeyState::UP;
}
void InputManager::Destroy()
{
// Unsetting the key event callback
glfwSetKeyCallback(this->_window->GetWindow(), nullptr);
}
bool InputManager::IsKeyDown(int key)
{
// If the key is in either a down or press state, return true
if (this->_keyMap.at(key) == KeyState::DOWN || this->_keyMap.at(key) == KeyState::PRESS)
{
return true;
}
return false;
}
bool InputManager::IsKeyPressed(int key)
{
// If the key is in a press state, return true
if (this->_keyMap.at(key) == KeyState::PRESS)
{
this->_keyMap.at(key) = KeyState::DOWN;
return true;
}
return false;
}
void InputManager::LockMouse()
{
glfwSetInputMode(this->_window->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glfwRawMouseMotionSupported())
glfwSetInputMode(this->_window->GetWindow(), GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
}
void InputManager::UnlockMouse()
{
glfwSetInputMode(this->_window->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
if (glfwRawMouseMotionSupported())
glfwSetInputMode(this->_window->GetWindow(), GLFW_RAW_MOUSE_MOTION, GLFW_FALSE);
}
void InputManager::sKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// Calling the non-static key callback from this static method
InputManager* inputManager = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
inputManager->KeyCallback(window, key, scancode, action, mods);
}
void InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
this->_keyMap[key] = action;
}
void InputManager::sCursorPosCallback(GLFWwindow* window, double x, double y)
{
// Calling the non-static cursor pos callback from this static method
InputManager* inputManager = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
inputManager->CursorPosCallback(window, x, y);
}
void InputManager::CursorPosCallback(GLFWwindow* window, double x, double y)
{
this->_x = x;
this->_y = y;
}