replaced input defines with statics

This commit is contained in:
Noel Berry 2020-08-27 19:43:23 -07:00
parent 3caa4a0ee1
commit 71043af92a
9 changed files with 136 additions and 91 deletions

View File

@ -30,8 +30,8 @@ using namespace Internal;
namespace
{
SDL_Window* window = nullptr;
SDL_Joystick* joysticks[BLAH_MAX_CONTROLLERS];
SDL_GameController* gamepads[BLAH_MAX_CONTROLLERS];
SDL_Joystick* joysticks[Blah::Input::max_controllers];
SDL_GameController* gamepads[Blah::Input::max_controllers];
char* basePath = nullptr;
char* userPath = nullptr;
bool displayed = false;

View File

@ -20,7 +20,7 @@ namespace
void Internal::Input::init()
{
g_empty_controller.name = "Disconnected";
for (int i = 0; i < BLAH_MAX_CONTROLLERS; i++)
for (int i = 0; i < Blah::Input::max_controllers; i++)
g_empty_state.controllers[i].name = g_empty_controller.name;
g_last_state = g_empty_state;
@ -36,30 +36,30 @@ void Internal::Input::frame()
// copy state, clear pressed / released values
{
for (int i = 0; i < BLAH_MAX_KEYBOARD_KEYS; i++)
for (int i = 0; i < Blah::Input::max_keyboard_keys; i++)
{
g_next_state.keyboard.pressed[i] = false;
g_next_state.keyboard.released[i] = false;
}
for (int i = 0; i < BLAH_MAX_MOUSE_BUTTONS; i++)
for (int i = 0; i < Blah::Input::max_mouse_buttons; i++)
{
g_next_state.mouse.pressed[i] = false;
g_next_state.mouse.released[i] = false;
}
g_next_state.mouse.wheel = Point::zero;
for (int i = 0; i < BLAH_MAX_TEXT; i ++)
for (int i = 0; i < Blah::Input::max_text_input; i++)
g_next_state.keyboard.text[i] = 0;
for (int i = 0; i < BLAH_MAX_CONTROLLERS; i++)
for (int i = 0; i < Blah::Input::max_controllers; i++)
{
ControllerState* controller = &(g_next_state.controllers[i]);
if (!controller->is_connected)
controller->name = nullptr;
for (int j = 0; j < BLAH_MAX_CONTROLLER_BUTTONS; j++)
for (int j = 0; j < Blah::Input::max_controller_buttons; j++)
{
controller->pressed[j] = false;
controller->released[j] = false;
@ -89,7 +89,7 @@ void Internal::Input::on_mouse_screen_move(float x, float y)
void Internal::Input::on_mouse_down(MouseButton button)
{
int i = (int)button;
if (i >= 0 && i < BLAH_MAX_MOUSE_BUTTONS)
if (i >= 0 && i < Blah::Input::max_mouse_buttons)
{
g_next_state.mouse.down[i] = true;
g_next_state.mouse.pressed[i] = true;
@ -100,7 +100,7 @@ void Internal::Input::on_mouse_down(MouseButton button)
void Internal::Input::on_mouse_up(MouseButton button)
{
int i = (int)button;
if (i >= 0 && i < BLAH_MAX_MOUSE_BUTTONS)
if (i >= 0 && i < Blah::Input::max_mouse_buttons)
{
g_next_state.mouse.down[i] = false;
g_next_state.mouse.released[i] = true;
@ -110,7 +110,7 @@ void Internal::Input::on_mouse_up(MouseButton button)
void Internal::Input::on_key_down(Key key)
{
int i = (int)key;
if (i >= 0 && i < BLAH_MAX_KEYBOARD_KEYS)
if (i >= 0 && i < Blah::Input::max_keyboard_keys)
{
g_next_state.keyboard.down[i] = true;
g_next_state.keyboard.pressed[i] = true;
@ -126,7 +126,7 @@ void Internal::Input::on_mouse_wheel(Point wheel)
void Internal::Input::on_key_up(Key key)
{
int i = (int)key;
if (i >= 0 && i < BLAH_MAX_KEYBOARD_KEYS)
if (i >= 0 && i < Blah::Input::max_keyboard_keys)
{
g_next_state.keyboard.down[i] = false;
g_next_state.keyboard.released[i] = true;
@ -135,12 +135,12 @@ void Internal::Input::on_key_up(Key key)
void Internal::Input::on_text_utf8(const char* text)
{
strncat(g_next_state.keyboard.text, text, BLAH_MAX_TEXT);
strncat(g_next_state.keyboard.text, text, Blah::Input::max_text_input);
}
void Internal::Input::on_controller_connect(int index, const char* name, int is_gamepad, int button_count, int axis_count)
{
if (index < BLAH_MAX_CONTROLLERS)
if (index < Blah::Input::max_controllers)
{
ControllerState* controller = &(g_next_state.controllers[index]);
*controller = g_empty_controller;
@ -154,14 +154,14 @@ void Internal::Input::on_controller_connect(int index, const char* name, int is_
void Internal::Input::on_controller_disconnect(int index)
{
if (index < BLAH_MAX_CONTROLLERS)
if (index < Blah::Input::max_controllers)
g_next_state.controllers[index] = g_empty_controller;
}
void Internal::Input::on_button_down(int index, int button)
{
if (index < BLAH_MAX_CONTROLLERS &&
button < BLAH_MAX_CONTROLLER_BUTTONS &&
if (index < Blah::Input::max_controllers &&
button < Blah::Input::max_controller_buttons &&
g_next_state.controllers[index].is_connected &&
button < g_next_state.controllers[index].button_count)
{
@ -173,8 +173,8 @@ void Internal::Input::on_button_down(int index, int button)
void Internal::Input::on_button_up(int index, int button)
{
if (index < BLAH_MAX_CONTROLLERS &&
button < BLAH_MAX_CONTROLLER_BUTTONS &&
if (index < Blah::Input::max_controllers &&
button < Blah::Input::max_controller_buttons &&
g_next_state.controllers[index].is_connected &&
button < g_next_state.controllers[index].button_count)
{
@ -185,8 +185,8 @@ void Internal::Input::on_button_up(int index, int button)
void Internal::Input::on_axis_move(int index, int axis, float value)
{
if (index < BLAH_MAX_CONTROLLERS &&
axis < BLAH_MAX_CONTROLLER_AXIS &&
if (index < Blah::Input::max_controllers &&
axis < Blah::Input::max_controller_axis &&
g_next_state.controllers[index].is_connected &&
axis < g_next_state.controllers[index].axis_count)
{
@ -205,28 +205,38 @@ const InputState* Input::last_state()
return &g_last_state;
}
Vec2 Input::mouse()
{
return g_curr_state.mouse.position;
}
Vec2 Input::mouse_draw()
{
return Vec2(g_curr_state.mouse.draw_position);
}
Vec2 Input::mouse_screen()
{
return Vec2(g_curr_state.mouse.screen_position);
}
bool Input::pressed(MouseButton button)
{
int i = (int)button;
return i >= 0 && i < BLAH_MAX_MOUSE_BUTTONS && g_curr_state.mouse.pressed[i];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.pressed[i];
}
bool Input::down(MouseButton button)
{
int i = (int)button;
return i >= 0 && i < BLAH_MAX_MOUSE_BUTTONS && g_curr_state.mouse.down[i];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.down[i];
}
bool Input::released(MouseButton button)
{
int i = (int)button;
return i >= 0 && i < BLAH_MAX_MOUSE_BUTTONS && g_curr_state.mouse.released[i];
return i >= 0 && i < Blah::Input::max_mouse_buttons&& g_curr_state.mouse.released[i];
}
Point Input::mouse_wheel()
@ -237,19 +247,19 @@ Point Input::mouse_wheel()
bool Input::pressed(Key key)
{
int i = (int)key;
return i > 0 && i < BLAH_MAX_KEYBOARD_KEYS && g_curr_state.keyboard.pressed[i];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.pressed[i];
}
bool Input::down(Key key)
{
int i = (int)key;
return i > 0 && i < BLAH_MAX_KEYBOARD_KEYS && g_curr_state.keyboard.down[i];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.down[i];
}
bool Input::released(Key key)
{
int i = (int)key;
return i > 0 && i < BLAH_MAX_KEYBOARD_KEYS && g_curr_state.keyboard.released[i];
return i > 0 && i < Blah::Input::max_keyboard_keys&& g_curr_state.keyboard.released[i];
}
bool Input::ctrl()
@ -274,7 +284,7 @@ const char* Input::text()
const ControllerState* Input::controller(int controllerIndex)
{
if (controllerIndex >= BLAH_MAX_CONTROLLERS)
if (controllerIndex >= Blah::Input::max_controllers)
{
Log::warn("Trying to access a controller at %i, outside of EX_MAX_CONTROLLERS", controllerIndex);
return &g_empty_controller;
@ -292,7 +302,7 @@ const ControllerState* Input::controller(int controllerIndex)
bool Input::pressed(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < BLAH_MAX_CONTROLLERS && i >= 0 && i < BLAH_MAX_CONTROLLER_BUTTONS)
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].pressed[i];
return false;
}
@ -300,7 +310,7 @@ bool Input::pressed(int controllerIndex, Button button)
bool Input::down(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < BLAH_MAX_CONTROLLERS && i >= 0 && i < BLAH_MAX_CONTROLLER_BUTTONS)
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].down[i];
return false;
}
@ -308,7 +318,7 @@ bool Input::down(int controllerIndex, Button button)
bool Input::released(int controllerIndex, Button button)
{
int i = (int)button;
if (controllerIndex < BLAH_MAX_CONTROLLERS && i >= 0 && i < BLAH_MAX_CONTROLLER_BUTTONS)
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_buttons)
return g_curr_state.controllers[controllerIndex].released[i];
return false;
}
@ -316,7 +326,7 @@ bool Input::released(int controllerIndex, Button button)
float Input::axis_check(int controllerIndex, Axis axis)
{
int i = (int)axis;
if (controllerIndex < BLAH_MAX_CONTROLLERS && i >= 0 && i < BLAH_MAX_CONTROLLER_AXIS)
if (controllerIndex < Blah::Input::max_controllers && i >= 0 && i < Blah::Input::max_controller_axis)
return g_curr_state.controllers[controllerIndex].axis[i];
return 0;
}
@ -374,5 +384,17 @@ const char* Input::name_of(Key key)
#undef DEFINE_KEY
}
return "Unknown";
}
const char* Input::name_of(Button button)
{
switch (button)
{
#define DEFINE_BTN(name, value) case Button::name: return #name;
BLAH_BUTTON_DEFINITIONS
#undef DEFINE_BTN
}
return "Unknown";
}

View File

@ -3,14 +3,7 @@
#include <inttypes.h>
#include <blah/math/vec2.h>
#define BLAH_MAX_CONTROLLERS 4
#define BLAH_MAX_CONTROLLER_BUTTONS 64
#define BLAH_MAX_CONTROLLER_AXIS 16
#define BLAH_MAX_MOUSE_BUTTONS 16
#define BLAH_MAX_KEYBOARD_KEYS 512
#define BLAH_MAX_TEXT 256
#define BLAH_MAX_VIRTUAL_NODES 32
// These are generally copied from the SDL2 Scancode Keys
#define BLAH_KEY_DEFINITIONS \
DEFINE_KEY(Unknown, 0) \
DEFINE_KEY(A, 4) \
@ -226,8 +219,49 @@
DEFINE_KEY(RightAlt, 230) \
DEFINE_KEY(RightGui, 231)
#define BLAH_BUTTON_DEFINITIONS \
DEFINE_BTN(None, -1) \
DEFINE_BTN(A, 0) \
DEFINE_BTN(B, 1) \
DEFINE_BTN(X, 2) \
DEFINE_BTN(Y, 3) \
DEFINE_BTN(Back, 4) \
DEFINE_BTN(Select, 5) \
DEFINE_BTN(Start, 6) \
DEFINE_BTN(LeftStick, 7) \
DEFINE_BTN(RightStick, 8) \
DEFINE_BTN(LeftShoulder, 9) \
DEFINE_BTN(RightShoulder, 10) \
DEFINE_BTN(Up, 11) \
DEFINE_BTN(Down, 12) \
DEFINE_BTN(Left, 13) \
DEFINE_BTN(Right, 14)
namespace Blah
{
namespace Input
{
// maximum number of controllers the input can handle
constexpr int max_controllers = 8;
// maximum number of buttons the input will track
constexpr int max_controller_buttons = 64;
// maximum number of controller axis the input will track
constexpr int max_controller_axis = 16;
// maximum number of mouse buttons the input will track
constexpr int max_mouse_buttons = 16;
// maximum number of keys the input will track
constexpr int max_keyboard_keys = 512;
// maximum length of text input that can be received per-frame
constexpr int max_text_input = 256;
// maximum number of nodes within a virtual input device
constexpr int max_virtual_nodes = 32;
}
struct ControllerState
{
@ -247,40 +281,40 @@ namespace Blah
int axis_count;
// An array holding the pressed state of each button
bool pressed[BLAH_MAX_CONTROLLER_BUTTONS];
bool pressed[Input::max_controller_buttons];
// An array holding the down state of each button
bool down[BLAH_MAX_CONTROLLER_BUTTONS];
bool down[Input::max_controller_buttons];
// An array holding the released state of each button
bool released[BLAH_MAX_CONTROLLER_BUTTONS];
bool released[Input::max_controller_buttons];
// An array holding the value state of each axis
float axis[BLAH_MAX_CONTROLLER_AXIS];
float axis[Input::max_controller_axis];
// Timestamp, in milliseconds, since each button was last pressed
uint64_t button_timestamp[BLAH_MAX_CONTROLLER_BUTTONS];
uint64_t button_timestamp[Input::max_controller_buttons];
// Timestamp, in milliseconds, since each axis last had a value set
uint64_t axis_timestamp[BLAH_MAX_CONTROLLER_AXIS];
uint64_t axis_timestamp[Input::max_controller_axis];
};
struct KeyboardState
{
bool pressed[BLAH_MAX_KEYBOARD_KEYS];
bool down[BLAH_MAX_KEYBOARD_KEYS];
bool released[BLAH_MAX_KEYBOARD_KEYS];
uint64_t timestamp[BLAH_MAX_KEYBOARD_KEYS];
char text[BLAH_MAX_TEXT];
bool pressed[Input::max_keyboard_keys];
bool down[Input::max_keyboard_keys];
bool released[Input::max_keyboard_keys];
uint64_t timestamp[Input::max_keyboard_keys];
char text[Input::max_text_input];
};
struct MouseState
{
bool pressed[BLAH_MAX_MOUSE_BUTTONS];
bool down[BLAH_MAX_MOUSE_BUTTONS];
bool released[BLAH_MAX_MOUSE_BUTTONS];
uint64_t timestamp[BLAH_MAX_MOUSE_BUTTONS];
bool pressed[Input::max_mouse_buttons];
bool down[Input::max_mouse_buttons];
bool released[Input::max_mouse_buttons];
uint64_t timestamp[Input::max_mouse_buttons];
Vec2 screen_position;
Vec2 draw_position;
Vec2 position;
@ -292,7 +326,7 @@ namespace Blah
{
// All the Gamepads. Note that not all gamepads are necessarily connected,
// and each one must be checked before use.
ControllerState controllers[BLAH_MAX_CONTROLLERS];
ControllerState controllers[Input::max_controllers];
// The current Keyboard state
KeyboardState keyboard;
@ -303,7 +337,6 @@ namespace Blah
};
// Keyboard Keys
// These are generally copied from the SDL2 Scancode Keys
enum class Key
{
#define DEFINE_KEY(name, value) name = value,
@ -314,22 +347,9 @@ namespace Blah
// Game Controller Buttons
enum class Button
{
None = -1,
A = 0,
B = 1,
X = 2,
Y = 3,
Back = 4,
Select = 5,
Start = 6,
LeftStick= 7,
RightStick = 8,
LeftShoulder = 9,
RightShoulder = 10,
Up = 11,
Down = 12,
Left = 13,
Right = 14
#define DEFINE_BTN(name, value) name = value,
BLAH_BUTTON_DEFINITIONS
#undef DEFINE_BTN
};
// Game Controller Axes
@ -427,5 +447,8 @@ namespace Blah
// returns a string name of the given key
const char* name_of(Key key);
// returns a string name of the given button
const char* name_of(Button button);
}
}

View File

@ -7,7 +7,7 @@ using namespace Blah;
VirtualAxis& VirtualAxis::add_keys(Key negative, Key positive)
{
if (m_axes_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_axes_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualAxis Keys out of bounds!");
else
{
@ -20,7 +20,7 @@ VirtualAxis& VirtualAxis::add_keys(Key negative, Key positive)
VirtualAxis& VirtualAxis::add_buttons(int gamepad_id, Button negative, Button positive)
{
if (m_axes_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_axes_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualAxis Buttons out of bounds!");
else
{
@ -33,7 +33,7 @@ VirtualAxis& VirtualAxis::add_buttons(int gamepad_id, Button negative, Button po
VirtualAxis& VirtualAxis::add_axis(int gamepad_id, Axis axis, float deadzone)
{
if (m_axes_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_axes_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualAxis Axes out of bounds!");
else
{

View File

@ -41,9 +41,9 @@ namespace Blah
void update();
};
KeysNode m_keys[BLAH_MAX_VIRTUAL_NODES];
ButtonsNode m_buttons[BLAH_MAX_VIRTUAL_NODES];
AxisNode m_axes[BLAH_MAX_VIRTUAL_NODES];
KeysNode m_keys[Input::max_virtual_nodes];
ButtonsNode m_buttons[Input::max_virtual_nodes];
AxisNode m_axes[Input::max_virtual_nodes];
int m_keys_len = 0;
int m_buttons_len = 0;
int m_axes_len = 0;

View File

@ -7,7 +7,7 @@ using namespace Blah;
VirtualButton& VirtualButton::add_key(Key key)
{
if (m_keys_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_keys_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualButton Keys out of bounds!");
else
{
@ -20,7 +20,7 @@ VirtualButton& VirtualButton::add_key(Key key)
VirtualButton& VirtualButton::add_button(int gamepad_id, Button button)
{
if (m_buttons_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_buttons_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualButton Buttons out of bounds!");
else
{
@ -33,7 +33,7 @@ VirtualButton& VirtualButton::add_button(int gamepad_id, Button button)
VirtualButton& VirtualButton::add_axis(int gamepad_id, Axis axis, float threshold, bool greater_than)
{
if (m_axes_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_axes_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualButton Axes out of bounds!");
else
{

View File

@ -46,9 +46,9 @@ namespace Blah
void update();
};
KeyNode m_keys[BLAH_MAX_VIRTUAL_NODES];
ButtonNode m_buttons[BLAH_MAX_VIRTUAL_NODES];
AxisNode m_axes[BLAH_MAX_VIRTUAL_NODES];
KeyNode m_keys[Input::max_virtual_nodes];
ButtonNode m_buttons[Input::max_virtual_nodes];
AxisNode m_axes[Input::max_virtual_nodes];
int m_keys_len = 0;
int m_buttons_len = 0;
int m_axes_len = 0;

View File

@ -17,7 +17,7 @@ VirtualStick::VirtualStick(float iDeadzone)
VirtualStick& VirtualStick::add_keys(Key left, Key right, Key up, Key down)
{
if (m_keys_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_keys_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualStick Keys out of bounds!");
else
{
@ -30,7 +30,7 @@ VirtualStick& VirtualStick::add_keys(Key left, Key right, Key up, Key down)
VirtualStick& VirtualStick::add_buttons(int gamepad_id, Button left, Button right, Button up, Button down)
{
if (m_buttons_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_buttons_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualStick Buttons out of bounds!");
else
{
@ -43,7 +43,7 @@ VirtualStick& VirtualStick::add_buttons(int gamepad_id, Button left, Button righ
VirtualStick& VirtualStick::add_axes(int gamepad_id, Axis horizontal, Axis vertical, float deadzone)
{
if (m_axes_len >= BLAH_MAX_VIRTUAL_NODES)
if (m_axes_len >= Input::max_virtual_nodes)
BLAH_ERROR("VirtualStick Axes out of bounds!");
else
{

View File

@ -48,9 +48,9 @@ namespace Blah
void update();
};
KeysNode m_keys[BLAH_MAX_VIRTUAL_NODES];
ButtonsNode m_buttons[BLAH_MAX_VIRTUAL_NODES];
AxesNode m_axes[BLAH_MAX_VIRTUAL_NODES];
KeysNode m_keys[Input::max_virtual_nodes];
ButtonsNode m_buttons[Input::max_virtual_nodes];
AxesNode m_axes[Input::max_virtual_nodes];
int m_keys_len = 0;
int m_buttons_len = 0;
int m_axes_len = 0;