Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
InputState.cpp
Go to the documentation of this file.
1
2/*
3 ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
4 ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
5 █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
6 ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
7 ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
8 ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
9 /vvvvvvvvvvvvvvvvvvv \=========================================,
10 `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
11 Katana Engine \/ © 2012 - Shuriken Studios LLC
12 http://www.shurikenstudios.com
13*/
14
15#include "KatanaEngine.h"
16
17namespace KatanaEngine
18{
20 {
21 al_install_keyboard();
22 al_install_mouse();
23
24
25 al_set_config_value(al_get_system_config(), "joystick", "driver", "xinput");
26 al_install_joystick();
27
28
29 for (int i = 0; i < MAX_NUM_GAMEPADSTATES; i++)
30 {
31 m_currentGamePadStates[i].ID = nullptr;
32 m_previousGamePadStates[i].ID = nullptr;
33 m_currentGamePadStates[i].IsConnected = false;
34 m_previousGamePadStates[i].IsConnected = false;
35 }
36
37 if (al_is_joystick_installed())
38 {
39 InitializeGamePads();
40 }
41 }
42
43 void InputState::Update()
44 {
45 if (al_is_keyboard_installed())
46 {
47 m_previousKeyboardState = m_currentKeyboardState;
48 al_get_keyboard_state(&m_currentKeyboardState);
49 }
50
51 if (al_is_mouse_installed())
52 {
53 m_previousMouseState = m_currentMouseState;
54 al_get_mouse_state(&m_currentMouseState);
55 }
56
57 for (int i = 0; i < MAX_NUM_GAMEPADSTATES; i++)
58 {
59 m_previousGamePadStates[i] = m_currentGamePadStates[i];
60 }
61 }
62
63 void InputState::InitializeGamePads()
64 {
65 for (int i = 0; i < al_get_num_joysticks(); i++)
66 {
67 ALLEGRO_JOYSTICK *joystick = al_get_joystick(i);
68
69 //if (strcmp(al_get_joystick_name(joystick), "Controller (XBOX 360 For Windows)") == 0)
70 //if (strcmp(al_get_joystick_name(joystick), "XInput Joystick 0") == 0)
71 {
72 for (int i = 0; i < MAX_NUM_GAMEPADSTATES; i++)
73 {
74 if (m_currentGamePadStates[i].ID == nullptr)
75 {
76 m_currentGamePadStates[i].ID = joystick;
77 m_currentGamePadStates[i].IsConnected = true;
78 m_currentGamePadStates[i].Reset();
79
80 m_previousGamePadStates[i] = m_currentGamePadStates[i];
81
82 m_map.emplace(std::pair<ALLEGRO_JOYSTICK *, int>(joystick, i));
83 break;
84 }
85 }
86 }
87 }
88 }
89
90 void InputState::UpdateConfigurationEvent()
91 {
92 if (al_is_joystick_installed())
93 {
94 if (al_reconfigure_joysticks())
95 {
96 InitializeGamePads();
97 }
98 }
99 }
100
101 void InputState::UpdateAxisEvent(ALLEGRO_EVENT alEvent)
102 {
103 int index = GetGamePadIndex(alEvent.joystick.id);
104
105 if (index > -1)
106 {
107 //std::cout << "Stick: " << alEvent.joystick.stick << std::endl;
108 //std::cout << "Pos: " << alEvent.joystick.pos << std::endl;
109 //std::cout << "Axis: " << alEvent.joystick.axis << std::endl;
110 //std::cout << "-------------------" << std::endl;
111
112 if (alEvent.joystick.stick == 0 && alEvent.joystick.axis == 0)
113 {
114 m_currentGamePadStates[index].Thumbsticks.Left.X = alEvent.joystick.pos;
115 }
116 else if (alEvent.joystick.stick == 0 && alEvent.joystick.axis == 1)
117 {
118 m_currentGamePadStates[index].Thumbsticks.Left.Y = alEvent.joystick.pos;
119 }
120 else if (alEvent.joystick.stick == 1 && alEvent.joystick.axis == 0)
121 {
122 m_currentGamePadStates[index].Thumbsticks.Right.X = alEvent.joystick.pos;
123 }
124 else if (alEvent.joystick.stick == 1 && alEvent.joystick.axis == 1)
125 {
126 m_currentGamePadStates[index].Thumbsticks.Right.Y = alEvent.joystick.pos;
127 }
128 else if (alEvent.joystick.stick == 2)
129 {
130 m_currentGamePadStates[index].Triggers.Left = alEvent.joystick.pos;
131 }
132 else if (alEvent.joystick.stick == 3)
133 {
134 m_currentGamePadStates[index].Triggers.Right = alEvent.joystick.pos;
135 }
136 }
137 }
138
139 void InputState::UpdateButtonEvent(ALLEGRO_EVENT alEvent, ButtonState state)
140 {
141 int index = GetGamePadIndex(alEvent.joystick.id);
142
143 if (index > -1)
144 {
145 GamePadButtons *pButtons = &m_currentGamePadStates[index].Buttons;
146 GamePadDPad *pDPad = &m_currentGamePadStates[index].DPad;
147
148 //std::cout << "Button Index " << alEvent.joystick.button;
149 //if (state == ButtonState::PRESSED) std::cout << " was pressed." << std::endl;
150 //else std::cout << " was released." << std::endl;
151
152 switch (alEvent.joystick.button)
153 {
154 case 0: pButtons->A = state; break;
155 case 1: pButtons->B = state; break;
156 case 2: pButtons->X = state; break;
157 case 3: pButtons->Y = state; break;
158
159 case 4: pButtons->RightShoulder = state; break;
160 case 5: pButtons->LeftShoulder = state; break;
161
162 case 6: pButtons->RightStick = state; break;
163 case 7: pButtons->LeftStick = state; break;
164
165 case 8: pButtons->Back = state; break;
166 case 9: pButtons->Start = state; break;
167
168 case 10: pDPad->Right = state; break;
169 case 11: pDPad->Left = state; break;
170 case 12: pDPad->Down = state; break;
171 case 13: pDPad->Up = state; break;
172 }
173 }
174 }
175
177 {
178 return al_key_down(&m_currentKeyboardState, (int)key);
179 }
180
181 bool InputState::IsKeyUp(Key key) const
182 {
183 return !al_key_down(&m_currentKeyboardState, (int)key);
184 }
185
187 {
188 return IsKeyDown(key) && !al_key_down(&m_previousKeyboardState, (int)key);
189 }
190
192 {
193 return IsKeyUp(key) && al_key_down(&m_previousKeyboardState, (int)key);
194 }
195
196 bool InputState::IsButtonUp(Button button, int8_t &indexOut, int8_t controllingIndex) const
197 {
198 if (controllingIndex > -1 && controllingIndex < MAX_NUM_GAMEPADSTATES)
199 {
200 if (m_currentGamePadStates[controllingIndex].IsButtonUp(button))
201 {
202 indexOut = controllingIndex;
203 return true;
204 }
205
206 return false;
207 }
208 else
209 {
210 return (IsButtonUp(button, indexOut, 0) || IsButtonUp(button, indexOut, 1) ||
211 IsButtonUp(button, indexOut, 2) || IsButtonUp(button, indexOut, 3));
212 }
213 }
214
215 bool InputState::IsButtonDown(Button button, int8_t &indexOut, int8_t controllingIndex) const
216 {
217 if (controllingIndex > -1 && controllingIndex < MAX_NUM_GAMEPADSTATES)
218 {
219 if (m_currentGamePadStates[controllingIndex].IsButtonDown(button))
220 {
221 indexOut = controllingIndex;
222 return true;
223 }
224
225 return false;
226 }
227 else
228 {
229 return (IsButtonDown(button, indexOut, 0) || IsButtonDown(button, indexOut, 1) ||
230 IsButtonDown(button, indexOut, 2) || IsButtonDown(button, indexOut, 3));
231 }
232 }
233
234 bool InputState::IsNewButtonPress(Button button, int8_t &indexOut, int8_t controllingIndex) const
235 {
236 if (controllingIndex > -1 && controllingIndex < MAX_NUM_GAMEPADSTATES)
237 {
238 if (m_currentGamePadStates[controllingIndex].IsButtonDown(button) &&
239 m_previousGamePadStates[controllingIndex].IsButtonUp(button))
240 {
241 indexOut = controllingIndex;
242 return true;
243 }
244
245 return false;
246 }
247 else
248 {
249 return (IsNewButtonPress(button, indexOut, 0) || IsNewButtonPress(button, indexOut, 1) ||
250 IsNewButtonPress(button, indexOut, 2) || IsNewButtonPress(button, indexOut, 3));
251 }
252 }
253
254 bool InputState::IsNewButtonRelease(Button button, int8_t &indexOut, int8_t controllingIndex) const
255 {
256 if (controllingIndex > -1 && controllingIndex < MAX_NUM_GAMEPADSTATES)
257 {
258 if (m_currentGamePadStates[controllingIndex].IsButtonUp(button) &&
259 m_previousGamePadStates[controllingIndex].IsButtonDown(button))
260 {
261 indexOut = controllingIndex;
262 return true;
263 }
264
265 return false;
266 }
267 else
268 {
269 return (IsNewButtonRelease(button, indexOut, 0) || IsNewButtonRelease(button, indexOut, 1) ||
270 IsNewButtonRelease(button, indexOut, 2) || IsNewButtonRelease(button, indexOut, 3));
271 }
272 }
273
274 GamePadState InputState::GetGamePadState(const int8_t gamePadIndex) const
275 {
276 return m_currentGamePadStates[gamePadIndex];
277 }
278
279 int8_t InputState::GetGamePadIndex(ALLEGRO_JOYSTICK *pId)
280 {
281 return m_map[pId];
282 }
283}
bool IsNewButtonRelease(Button button, int8_t &indexOut, int8_t controllingIndex=-1) const
Determines if a the button on an Xbox controller just released this frame.
static const uint8_t MAX_NUM_GAMEPADSTATES
The maximum number of Xbox controllers that the system can manage.
Definition InputState.h:28
bool IsKeyDown(Key key) const
Determines if a keyboard key is currently being pressed down.
bool IsButtonUp(Button button, int8_t &indexOut, int8_t controllingIndex=-1) const
Determines if a the button on an Xbox controller is up.
bool IsNewButtonPress(Button button, int8_t &indexOut, int8_t controllingIndex=-1) const
Determines if a the button on an Xbox controller was just pressed this frame.
GamePadState GetGamePadState(const int8_t gamePadIndex) const
Get the current state of an Xbox controller.
bool IsKeyUp(Key key) const
Determines if a keyboard key is currently not being pressed down.
bool IsNewKeyPress(Key key) const
Determines if a keyboard key was just pressed this frame.
bool IsButtonDown(Button button, int8_t &indexOut, int8_t controllingIndex=-1) const
Determines if a the button on an Xbox controller is down.
bool IsNewKeyRelease(Key key) const
Determines if a keyboard key was just released this frame.
float Y
The y-coordinate of the vector.
Definition Vector2.h:187
float X
The x-coordinate of the vector.
Definition Vector2.h:186
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18
Button
Defines the buttons for an Xbox Controller.
ButtonState
Defines the possible states of a Button.
Key
Defines the keys on a keyboard.
Definition KeyState.h:22
Represents specific information about the state of an Xbox Controller, including the current state of...
GamePadButtons Buttons
The current state of the GamePad's Buttons.
GamePadDPad DPad
The current state of the GamePad's DPad.
ALLEGRO_JOYSTICK * ID
The underlaying allegro joystick id.
bool IsConnected
True if the GamePad is connected, false otherwise.
GamePadThumbSticks Thumbsticks
The current state of the GamePad's Thumbsticks.
GamePadTriggers Triggers
The current state of the GamePad's Triggers.