-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
97 lines (76 loc) · 2.3 KB
/
Controller.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
#include "Controller.h"
#include "Log.h"
Controller::Controller() {
keystates = SDL_GetKeyboardState( 0 );
}
void Controller::update() {
// We can poll for the change to the mouse position here.
SDL_GetMouseState( &xMouse, &yMouse ); //Abs pos of mouse
SDL_GetRelativeMouseState( &dxMouse, &dyMouse ); //rel changes
// keep a copy of the prev keystates to be able to tell if a key was just pressed/just released
//memcpy( prevKeystates, keystates, SDL_NUM_SCANCODES ); // this does NOT work since it's a pointer.
keystates = SDL_GetKeyboardState( 0 );
}
void Controller::setKeyJustPressed( uint16_t key ) {
if( key > SDL_NUM_SCANCODES ) {
error( "Invalid key index %hu", key );
return;
}
keysJustPressed[ key ] = 1;
}
void Controller::setMouseJustClicked( uint16_t mouseButton ) {
mouseButtonsJustPressed[ mouseButton ] = 1;
}
void Controller::setKeyJustReleased( uint16_t key ) {
if( key > SDL_NUM_SCANCODES ) {
error( "Invalid key index %hu", key );
return;
}
keysJustReleased[ key ] = 1;
}
bool Controller::mouseJustPressed( uint16_t mouseButton ) {
auto it = mouseButtonsJustPressed.find( mouseButton );
if( it == mouseButtonsJustPressed.end() ) {
// hasn't been pushed before or doesn't exist
return false;
}
return it->second;
}
bool Controller::isPressed( uint16_t key ) {
// The scancode has to be between 0 and SDL_NUM_SCANCODES to be a valid index
if( key > SDL_NUM_SCANCODES ) {
error( "Invalid key index %hu", key );
return false;
}
return keystates[ key ];
}
bool Controller::justPressed( uint16_t key ) {
if( key > SDL_NUM_SCANCODES ) {
error( "Invalid key index %hu", key );
return false;
}
return keysJustPressed[ key ];
}
bool Controller::justPressedAny( const vector<uint16_t>& keys ) {
for( uint16_t key : keys ) {
if( justPressed( key ) ) {
return true;
}
}
return false;
}
bool Controller::justReleased( uint16_t key ) {
if( key > SDL_NUM_SCANCODES ) {
error( "Invalid key index %hu", key );
return false;
}
return keysJustReleased[ key ];
}
void Controller::clearEventKeys() {
memset( keysJustPressed, 0, SDL_NUM_SCANCODES );
memset( keysJustReleased, 0, SDL_NUM_SCANCODES );
// Clear these to off
for( auto& p : mouseButtonsJustPressed ) {
p.second = 0;
}
}