-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
120 lines (106 loc) · 2.88 KB
/
Copy pathinput.cpp
File metadata and controls
120 lines (106 loc) · 2.88 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
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "input.hpp"
Input* Input::_input = nullptr;
Input::Input() {
for (int i = 0; i < 3; i++) {
_mouseButtonStates.push_back(false);
}
_prevMouseButtonStates = _mouseButtonStates;
_mPos.x = 0;
_mPos.y = 0;
_keyStates = nullptr;
for (int i = 0; i < SDL_NUM_SCANCODES; i++) {
_prevKeyStates[i] = 0;
}
_numkeys = 0;
}
Input* Input::getInputHandler() {
if (_input == nullptr) _input = new Input();
return _input;
}
bool Input::update() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
_onMouseButtonDown(event);
break;
case SDL_MOUSEBUTTONUP:
_onMouseButtonUp(event);
break;
case SDL_MOUSEMOTION:
_onMouseMotion(event);
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
_keyStates = SDL_GetKeyboardState(&_numkeys);
break;
case SDL_WINDOWEVENT:
Window::getWindow()->handleWindowEvent(event);
if (Window::getWindow()->getQuit() == true) {
return true;
}
break;
case SDL_QUIT:
return true;
break;
default:
break;
}
}
return false;
}
void Input::_onMouseButtonDown(const SDL_Event& event) {
if (event.button.button == SDL_BUTTON_LEFT) {
_mouseButtonStates[LEFT] = true;
}
if (event.button.button == SDL_BUTTON_MIDDLE) {
_mouseButtonStates[MIDDLE] = true;
}
if (event.button.button == SDL_BUTTON_RIGHT) {
_mouseButtonStates[RIGHT] = true;
}
}
void Input::_onMouseButtonUp(const SDL_Event& event)
{
if (event.button.button == SDL_BUTTON_LEFT) {
_mouseButtonStates[LEFT] = false;
}
if (event.button.button == SDL_BUTTON_MIDDLE) {
_mouseButtonStates[MIDDLE] = false;
}
if (event.button.button == SDL_BUTTON_RIGHT) {
_mouseButtonStates[RIGHT] = false;
}
}
void Input::_onMouseMotion(const SDL_Event& event) {
_mPos.x = event.motion.x;
_mPos.y = event.motion.y;
}
bool Input::isKeyDown(const SDL_Scancode& key) const {
if (_keyStates != 0 && _keyStates[key] == 1) {
return true;
}
return false;
}
/* registers a 'release' when prev key state is 0, then current key state is 1 (rather than the other way around, as to register when the key is hit)
*/
bool Input::isKeyReleased(const SDL_Scancode& key) const {
if (_keyStates != 0 && _prevKeyStates != 0 && _prevKeyStates[key] == 0 && _keyStates[key] == 1) return true;
return false;
}
bool Input::isMouseKeyReleased(const int& buttonNumber) const {
if(_prevMouseButtonStates[buttonNumber] == 1 && _mouseButtonStates[buttonNumber] == 0) return true;
return false;
}
const bool& Input::getMouseButtonState(const int& buttonNumber) const {
return _mouseButtonStates[buttonNumber];
}
const SDL_Point& Input::getMousePosition() const {
return _mPos;
}
void Input::updatePrevKeyStates() {
if (_keyStates != 0) {
memcpy(_prevKeyStates, _keyStates, _numkeys);
}
_prevMouseButtonStates = _mouseButtonStates;
}