ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
input.cpp
Go to the documentation of this file.
2
4
6
7std::array<asw::input::ControllerState, asw::input::MAX_CONTROLLERS>
9
11 auto& k_state = asw::input::keyboard;
12 auto& m_state = asw::input::mouse;
13 auto& c_state = asw::input::controller;
14
15 // Clear key state
16 k_state.anyPressed = false;
17 k_state.lastPressed = -1;
18
19 for (auto& pressed : k_state.pressed) {
20 pressed = false;
21 }
22
23 for (auto& released : k_state.released) {
24 released = false;
25 }
26
27 // Clear mouse state
28 m_state.anyPressed = false;
29 m_state.xChange = 0;
30 m_state.yChange = 0;
31 m_state.z = 0;
32
33 for (auto& pressed : m_state.pressed) {
34 pressed = false;
35 }
36
37 for (auto& released : m_state.released) {
38 released = false;
39 }
40
41 // Clear controller state
42 for (auto& cont : c_state) {
43 cont.anyPressed = false;
44 cont.lastPressed = -1;
45
46 for (auto& button : cont.pressed) {
47 button = false;
48 }
49
50 for (auto& button : cont.released) {
51 button = false;
52 }
53 }
54}
55
57 return mouse.down[static_cast<int>(button)];
58}
59
61 return mouse.pressed[static_cast<int>(button)];
62}
63
65 return mouse.released[static_cast<int>(button)];
66}
67
69 return keyboard.down[static_cast<int>(key)];
70}
71
73 return keyboard.pressed[static_cast<int>(key)];
74}
75
77 return keyboard.released[static_cast<int>(key)];
78}
79
81 auto cursor_int = static_cast<unsigned int>(cursor);
82
83 if (cursor_int < 0 || cursor_int >= cursors.size()) {
84 return;
85 }
86
87 if (cursors[cursor_int] == nullptr) {
88 cursors[cursor_int] =
89 SDL_CreateSystemCursor(static_cast<SDL_SystemCursor>(cursor_int));
90 }
91
92 SDL_SetCursor(cursors[cursor_int]);
93}
94
97 return controller[index].down[static_cast<int>(button)];
98}
99
102 return controller[index].pressed[static_cast<int>(button)];
103}
104
107 return controller[index].released[static_cast<int>(button)];
108}
109
112 return controller[index].axis[static_cast<int>(axis)];
113}
114
115void asw::input::setControllerDeadZone(int index, float deadZone) {
116 controller[index].deadZone = deadZone;
117}
118
120 int* count = nullptr;
121 SDL_GetJoysticks(count);
122 if (count == nullptr) {
123 return 0;
124 }
125
126 return *count;
127}
128
129std::string asw::input::getControllerName(int index) {
130 return SDL_GetJoystickNameForID(index);
131}
Input module for the ASW library.
void setControllerDeadZone(int index, float deadZone)
Definition input.cpp:115
CursorId
System cursor Ids.
Definition input.h:279
void setCursor(asw::input::CursorId cursor)
Change cursor.
Definition input.cpp:80
std::string getControllerName(int index)
Definition input.cpp:129
float getControllerAxis(int index, asw::input::ControllerAxis axis)
Get the value of a controller axis.
Definition input.cpp:110
bool getMouseButton(asw::input::MouseButton button)
Check if a mouse button is down.
Definition input.cpp:56
int getControllerCount()
Definition input.cpp:119
bool getMouseButtonUp(asw::input::MouseButton button)
Check if a mouse button was released since the last update.
Definition input.cpp:64
struct KeyState { std::array< bool, NUM_KEYS > pressed{false}; std::array< bool, NUM_KEYS > released{false}; std::array< bool, NUM_KEYS > down{false}; bool anyPressed{false}; int lastPressed{-1}; } KeyState
Keyboard state stores the current state of the keyboard. It is updated by the core.
Definition input.h:356
bool getControllerButtonDown(int index, asw::input::ControllerButton button)
Check if a controller button was pressed since the last update.
Definition input.cpp:100
bool getMouseButtonDown(asw::input::MouseButton button)
Check if a mouse button was pressed since the last update.
Definition input.cpp:60
bool getControllerButtonUp(int index, asw::input::ControllerButton button)
Check if a controller button was released since the last update.
Definition input.cpp:105
void reset()
Reset all input states. Called by the core.
Definition input.cpp:10
std::array< ControllerState, MAX_CONTROLLERS > controller
Global controller state.
Definition input.cpp:8
Key
Enumeration of keys in the ASW library.
Definition input.h:37
bool getKeyUp(asw::input::Key key)
Check if a key was released since the last update.
Definition input.cpp:76
struct MouseState { bool anyPressed{false}; int lastPressed{-1}; float xChange{0.0F}; float yChange{0.0F}; Vec2< float > position{0.0F, 0.0F}; float z{0.0F}; std::array< bool, NUM_MOUSE_BUTTONS > pressed{false}; std::array< bool, NUM_MOUSE_BUTTONS > released{false}; std::array< bool, NUM_MOUSE_BUTTONS > down{false}; } MouseState
Mouse state stores the current state of the mouse. It is updated by the core.
Definition input.h:311
ControllerButton
Mappings from SDL game controller buttons to ASW buttons.
Definition input.h:410
MouseState mouse
Global mouse state.
Definition input.cpp:5
KeyState keyboard
Global keyboard state.
Definition input.cpp:3
MouseButton
Enumeration of mouse buttons in the ASW library.
Definition input.h:27
bool getKeyDown(asw::input::Key key)
Check if a key was pressed since the last update.
Definition input.cpp:72
bool getKey(asw::input::Key key)
Check if a key is down.
Definition input.cpp:68
bool getControllerButton(int index, asw::input::ControllerButton button)
Check if a controller button is down.
Definition input.cpp:95
ControllerAxis
Mappings from SDL game controller axes to ASW axes.
Definition input.h:443