-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cpp
195 lines (132 loc) · 4.71 KB
/
InputManager.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//---------------------------------------------------------------------//
// InputManager.cpp //
// Handles all Input related functionality for keyboard and mouse. //
//(To be expanded later to handle touch and contoller input) //
// //
// By: Ather Omar //
//---------------------------------------------------------------------//
#include <string.h>
//---------------------------------------------------------------
#include "InputManager.h"
//---------------------------------------------------------------
// QuickSDL
//---------------------------------------------------------------
namespace QuickSDL {
//Initializing to NULL
InputManager* InputManager::sInstance = NULL;
InputManager* InputManager::Instance() {
//Create a new instance if no instance was created before
if(sInstance == NULL)
sInstance = new InputManager();
return sInstance;
}
void InputManager::Release() {
delete sInstance;
sInstance = NULL;
}
InputManager::InputManager() {
mKeyboardState = SDL_GetKeyboardState(&mKeyLength);
//Setting mPrevKeyboardState to be the same length as mKeyboardState
mPrevKeyboardState = new Uint8[mKeyLength];
//Copying the contents of mKeyboardState into mPrevKeyboardState so that it contains data on the first frame
memcpy(mPrevKeyboardState, mKeyboardState, mKeyLength);
}
InputManager::~InputManager() {
//Clearing the previous keyboard state array
delete[] mPrevKeyboardState;
mPrevKeyboardState = NULL;
}
bool InputManager::KeyDown(SDL_Scancode scanCode) {
return (mKeyboardState[scanCode] != 0);
}
bool InputManager::KeyPressed(SDL_Scancode scanCode) {
//returning true of the key was not pressed in the previous keyboard state but is pressed in the current one
return (mPrevKeyboardState[scanCode]) == 0 && (mKeyboardState[scanCode] != 0);
}
bool InputManager::KeyReleased(SDL_Scancode scanCode) {
//returning true of the key was pressed in the previous keyboard state but is not pressed in the current one
return (mPrevKeyboardState[scanCode] != 0) && (mKeyboardState[scanCode] == 0);
}
Vector2 InputManager::MousePos() {
//Converting the mouse position into a Vector2 for ease of use since all entities use Vector2
return Vector2((float)mMouseXPos, (float)mMouseYPos);
}
bool InputManager::MouseButtonDown(MOUSE_BUTTON button) {
//mask to be using for bit wise operations
Uint32 mask = 0;
switch(button) {
case left:
mask = SDL_BUTTON_LMASK;
break;
case right:
mask = SDL_BUTTON_RMASK;
break;
case middle:
mask = SDL_BUTTON_MMASK;
break;
case back:
mask = SDL_BUTTON_X1MASK;
break;
case forward:
mask = SDL_BUTTON_X2MASK;
break;
}
//return true if the mask exists in the current mouse state
return ((mMouseState & mask) != 0);
}
bool InputManager::MouseButtonPressed(MOUSE_BUTTON button) {
//mask to be using for bit wise operations
Uint32 mask = 0;
switch(button) {
case left:
mask = SDL_BUTTON_LMASK;
break;
case right:
mask = SDL_BUTTON_RMASK;
break;
case middle:
mask = SDL_BUTTON_MMASK;
break;
case back:
mask = SDL_BUTTON_X1MASK;
break;
case forward:
mask = SDL_BUTTON_X2MASK;
break;
}
//return true if the mask does not exist in the previous mouse state, but exists in the current one
return ((mPrevMouseState & mask) == 0) && ((mMouseState & mask) != 0);
}
bool InputManager::MouseButtonReleased(MOUSE_BUTTON button) {
Uint32 mask = 0;
switch(button) {
case left:
mask = SDL_BUTTON_LMASK;
break;
case right:
mask = SDL_BUTTON_RMASK;
break;
case middle:
mask = SDL_BUTTON_MMASK;
break;
case back:
mask = SDL_BUTTON_X1MASK;
break;
case forward:
mask = SDL_BUTTON_X2MASK;
break;
}
//return true if the mask exists in the previous mouse state, but does not exist in the current one
return ((mPrevMouseState & mask) != 0) && ((mMouseState & mask) == 0);
}
void InputManager::Update() {
//Updating the mouse state to get the key states of the current frame
mMouseState = SDL_GetMouseState(&mMouseXPos, &mMouseYPos);
}
void InputManager::UpdatePrevInput() {
//Copying the keyboard state into the previous keyboard state array at the end of the frame
memcpy(mPrevKeyboardState, mKeyboardState, mKeyLength);
//Setting the previous mouse state as the current mouse state at the end of the frame
mPrevMouseState = mMouseState;
}
}