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
101 int index,
103 return controller[index].pressed[static_cast<int>(button)];
104}
105
107 int index,
109 return controller[index].released[static_cast<int>(button)];
110}
111
114 return controller[index].axis[static_cast<int>(axis)];
115}
116
117void asw::input::setControllerDeadZone(int index, float deadZone) {
118 controller[index].deadZone = deadZone;
119}
120
122 int* count = nullptr;
123 SDL_GetJoysticks(count);
124 if (count == nullptr) {
125 return 0;
126 }
127
128 return *count;
129}
130
131std::string asw::input::getControllerName(int index) {
132 return SDL_GetJoystickNameForID(index);
133}
Input module for the ASW library.
bool wasButtonReleased(asw::input::MouseButton button)
Check if a button was released since the last update.
Definition input.cpp:64
void setControllerDeadZone(int index, float deadZone)
Definition input.cpp:117
CursorId
System cursor Ids.
Definition input.h:277
bool wasKeyReleased(asw::input::Key key)
Check if a key was released since the last update.
Definition input.cpp:76
bool isControllerButtonDown(int index, asw::input::ControllerButton button)
Check if a controller button is down.
Definition input.cpp:95
void setCursor(asw::input::CursorId cursor)
Change cursor.
Definition input.cpp:80
std::string getControllerName(int index)
Definition input.cpp:131
bool isKeyDown(asw::input::Key key)
Check if a key is down.
Definition input.cpp:68
float getControllerAxis(int index, asw::input::ControllerAxis axis)
Get the value of a controller axis.
Definition input.cpp:112
bool wasControllerButtonReleased(int index, asw::input::ControllerButton button)
Check if a controller button was released since the last update.
Definition input.cpp:106
int getControllerCount()
Definition input.cpp:121
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:355
struct MouseState { bool anyPressed{false}; int lastPressed{-1}; float xChange{0}; float yChange{0}; float x{0}; float y{0}; float z{0}; 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:309
void reset()
Reset all input states. Called by the core.
Definition input.cpp:10
bool isButtonDown(asw::input::MouseButton button)
Check if a button is down.
Definition input.cpp:56
std::array< ControllerState, MAX_CONTROLLERS > controller
Global controller state.
Definition input.cpp:8
bool wasKeyPressed(asw::input::Key key)
Check if a key was pressed since the last update.
Definition input.cpp:72
Key
Enumeration of keys in the ASW library.
Definition input.h:35
bool wasButtonPressed(asw::input::MouseButton button)
Check if a button was pressed since the last update.
Definition input.cpp:60
bool wasControllerButtonPressed(int index, asw::input::ControllerButton button)
Check if a controller button was pressed since the last update.
Definition input.cpp:100
ControllerButton
Mappings from SDL game controller buttons to ASW buttons.
Definition input.h:409
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:25
ControllerAxis
Mappings from SDL game controller axes to ASW axes.
Definition input.h:442