Skip to content

Commit

Permalink
Clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Mar 23, 2024
1 parent 59e0f66 commit 9afb71d
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 2 deletions.
6 changes: 4 additions & 2 deletions seika/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct SkaInputState {
.cleanupKeyStateJustReleased = {0}, \
.cleanupKeyStateJustPressedCount = 0, \
.cleanupKeyStateJustReleasedCount = 0, \
.inputActionHandleIndex = 1, \
.inputActionHandleIndex = 0, \
.inputActionData = {{0}} \
}

Expand Down Expand Up @@ -360,6 +360,7 @@ SkaInputActionHandle ska_input_add_input_action(const char* actionName, const Sk
break;
}
actionData->action.actionValues[i] = actionValues[i];
actionData->action.actionValuesCount++;
}
return newHandle;
}
Expand All @@ -386,7 +387,8 @@ bool ska_input_remove_input_action(SkaInputActionHandle handle, SkaInputDeviceIn
SkaInputActionHandle ska_input_find_input_action_handle(const char* actionName, SkaInputDeviceIndex deviceIndex) {
for (SkaInputActionHandle handle = 0; handle < SKA_INPUT_MAX_INPUT_ACTIONS; handle++) {
SkaInputActionData* actionData = &inputState.inputActionData[deviceIndex][handle];
if (actionData->handle == SKA_INPUT_INVALID_INPUT_ACTION_HANDLE) {
// TODO: Shouldn't have to check for NULL name as handle should be enough
if (actionData->handle == SKA_INPUT_INVALID_INPUT_ACTION_HANDLE || actionData->action.name == NULL) {
continue;
}
if (strcmp(actionData->action.name, actionName) == 0) {
Expand Down
171 changes: 171 additions & 0 deletions seika/sdl_input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#pragma once

#include <SDL3/SDL.h>

#include "input.h"

SkaInputKey ska_sdl_keycode_to_input_key(int keycode) {
switch (keycode) {
case SDLK_TAB: return SkaInputKey_KEYBOARD_TAB;
case SDLK_LEFT: return SkaInputKey_KEYBOARD_LEFT;
case SDLK_RIGHT: return SkaInputKey_KEYBOARD_RIGHT;
case SDLK_UP: return SkaInputKey_KEYBOARD_UP;
case SDLK_DOWN: return SkaInputKey_KEYBOARD_DOWN;
case SDLK_PAGEDOWN: return SkaInputKey_KEYBOARD_PAGE_DOWN;
case SDLK_PAGEUP: return SkaInputKey_KEYBOARD_PAGE_UP;
case SDLK_HOME: return SkaInputKey_KEYBOARD_HOME;
case SDLK_END: return SkaInputKey_KEYBOARD_END;
case SDLK_INSERT: return SkaInputKey_KEYBOARD_INSERT;
case SDLK_DELETE: return SkaInputKey_KEYBOARD_DELETE;
case SDLK_BACKSPACE: return SkaInputKey_KEYBOARD_BACKSPACE;
case SDLK_SPACE: return SkaInputKey_KEYBOARD_SPACE;
case SDLK_RETURN: return SkaInputKey_KEYBOARD_RETURN;
case SDLK_ESCAPE: return SkaInputKey_KEYBOARD_ESCAPE;
case SDLK_QUOTE: return SkaInputKey_KEYBOARD_QUOTE;
case SDLK_COMMA: return SkaInputKey_KEYBOARD_COMMA;
case SDLK_MINUS: return SkaInputKey_KEYBOARD_MINUS;
case SDLK_PERIOD: return SkaInputKey_KEYBOARD_PERIOD;
case SDLK_SLASH: return SkaInputKey_KEYBOARD_SLASH;
case SDLK_SEMICOLON: return SkaInputKey_KEYBOARD_SEMICOLON;
case SDLK_EQUALS: return SkaInputKey_KEYBOARD_EQUALS;
case SDLK_LEFTBRACKET: return SkaInputKey_KEYBOARD_LEFT_BRACKET;
case SDLK_BACKSLASH: return SkaInputKey_KEYBOARD_BACKSLASH;
case SDLK_RIGHTBRACKET: return SkaInputKey_KEYBOARD_RIGHT_BRACKET;
case SDLK_BACKQUOTE: return SkaInputKey_KEYBOARD_BACKQUOTE;
case SDLK_CAPSLOCK: return SkaInputKey_KEYBOARD_CAPS_LOCK;
case SDLK_SCROLLLOCK: return SkaInputKey_KEYBOARD_SCROLL_LOCK;
case SDLK_NUMLOCKCLEAR: return SkaInputKey_KEYBOARD_NUM_LOCK_CLEAR;
case SDLK_PRINTSCREEN: return SkaInputKey_KEYBOARD_PRINT_SCREEN;
case SDLK_PAUSE: return SkaInputKey_KEYBOARD_PAUSE;
case SDLK_KP_0: return SkaInputKey_KEYBOARD_KEYPAD_0;
case SDLK_KP_1: return SkaInputKey_KEYBOARD_KEYPAD_1;
case SDLK_KP_2: return SkaInputKey_KEYBOARD_KEYPAD_2;
case SDLK_KP_3: return SkaInputKey_KEYBOARD_KEYPAD_3;
case SDLK_KP_4: return SkaInputKey_KEYBOARD_KEYPAD_4;
case SDLK_KP_5: return SkaInputKey_KEYBOARD_KEYPAD_5;
case SDLK_KP_6: return SkaInputKey_KEYBOARD_KEYPAD_6;
case SDLK_KP_7: return SkaInputKey_KEYBOARD_KEYPAD_7;
case SDLK_KP_8: return SkaInputKey_KEYBOARD_KEYPAD_8;
case SDLK_KP_9: return SkaInputKey_KEYBOARD_KEYPAD_9;
case SDLK_KP_PERIOD: return SkaInputKey_KEYBOARD_KEYPAD_PERIOD;
case SDLK_KP_MULTIPLY: return SkaInputKey_KEYBOARD_KEYPAD_MULTIPLY;
case SDLK_KP_MINUS: return SkaInputKey_KEYBOARD_KEYPAD_MINUS;
case SDLK_KP_PLUS: return SkaInputKey_KEYBOARD_KEYPAD_PLUS;
case SDLK_KP_ENTER: return SkaInputKey_KEYBOARD_KEYPAD_ENTER;
case SDLK_KP_EQUALS: return SkaInputKey_KEYBOARD_KEYPAD_EQUALS;
case SDLK_LCTRL: return SkaInputKey_KEYBOARD_LEFT_CONTROL;
case SDLK_LSHIFT: return SkaInputKey_KEYBOARD_LEFT_SHIFT;
case SDLK_LALT: return SkaInputKey_KEYBOARD_LEFT_ALT;
case SDLK_LGUI: return SkaInputKey_KEYBOARD_LEFT_GUI;
case SDLK_RCTRL: return SkaInputKey_KEYBOARD_RIGHT_CONTROL;
case SDLK_RSHIFT: return SkaInputKey_KEYBOARD_RIGHT_SHIFT;
case SDLK_RALT: return SkaInputKey_KEYBOARD_RIGHT_ALT;
case SDLK_RGUI: return SkaInputKey_KEYBOARD_RIGHT_GUI;
case SDLK_APPLICATION: return SkaInputKey_KEYBOARD_APPLICATION;
case SDLK_0: return SkaInputKey_KEYBOARD_NUM_0;
case SDLK_1: return SkaInputKey_KEYBOARD_NUM_1;
case SDLK_2: return SkaInputKey_KEYBOARD_NUM_2;
case SDLK_3: return SkaInputKey_KEYBOARD_NUM_3;
case SDLK_4: return SkaInputKey_KEYBOARD_NUM_4;
case SDLK_5: return SkaInputKey_KEYBOARD_NUM_5;
case SDLK_6: return SkaInputKey_KEYBOARD_NUM_6;
case SDLK_7: return SkaInputKey_KEYBOARD_NUM_7;
case SDLK_8: return SkaInputKey_KEYBOARD_NUM_8;
case SDLK_9: return SkaInputKey_KEYBOARD_NUM_9;
case SDLK_a: return SkaInputKey_KEYBOARD_A;
case SDLK_b: return SkaInputKey_KEYBOARD_B;
case SDLK_c: return SkaInputKey_KEYBOARD_C;
case SDLK_d: return SkaInputKey_KEYBOARD_D;
case SDLK_e: return SkaInputKey_KEYBOARD_E;
case SDLK_f: return SkaInputKey_KEYBOARD_F;
case SDLK_g: return SkaInputKey_KEYBOARD_G;
case SDLK_h: return SkaInputKey_KEYBOARD_H;
case SDLK_i: return SkaInputKey_KEYBOARD_I;
case SDLK_j: return SkaInputKey_KEYBOARD_J;
case SDLK_k: return SkaInputKey_KEYBOARD_K;
case SDLK_l: return SkaInputKey_KEYBOARD_L;
case SDLK_m: return SkaInputKey_KEYBOARD_M;
case SDLK_n: return SkaInputKey_KEYBOARD_N;
case SDLK_o: return SkaInputKey_KEYBOARD_O;
case SDLK_p: return SkaInputKey_KEYBOARD_P;
case SDLK_q: return SkaInputKey_KEYBOARD_Q;
case SDLK_r: return SkaInputKey_KEYBOARD_R;
case SDLK_s: return SkaInputKey_KEYBOARD_S;
case SDLK_t: return SkaInputKey_KEYBOARD_T;
case SDLK_u: return SkaInputKey_KEYBOARD_U;
case SDLK_v: return SkaInputKey_KEYBOARD_V;
case SDLK_w: return SkaInputKey_KEYBOARD_W;
case SDLK_x: return SkaInputKey_KEYBOARD_X;
case SDLK_y: return SkaInputKey_KEYBOARD_Y;
case SDLK_z: return SkaInputKey_KEYBOARD_Z;
case SDLK_F1: return SkaInputKey_KEYBOARD_F1;
case SDLK_F2: return SkaInputKey_KEYBOARD_F2;
case SDLK_F3: return SkaInputKey_KEYBOARD_F3;
case SDLK_F4: return SkaInputKey_KEYBOARD_F4;
case SDLK_F5: return SkaInputKey_KEYBOARD_F5;
case SDLK_F6: return SkaInputKey_KEYBOARD_F6;
case SDLK_F7: return SkaInputKey_KEYBOARD_F7;
case SDLK_F8: return SkaInputKey_KEYBOARD_F8;
case SDLK_F9: return SkaInputKey_KEYBOARD_F9;
case SDLK_F10: return SkaInputKey_KEYBOARD_F10;
case SDLK_F11: return SkaInputKey_KEYBOARD_F11;
case SDLK_F12: return SkaInputKey_KEYBOARD_F12;
case SDLK_F13: return SkaInputKey_KEYBOARD_F13;
case SDLK_F14: return SkaInputKey_KEYBOARD_F14;
case SDLK_F15: return SkaInputKey_KEYBOARD_F15;
case SDLK_F16: return SkaInputKey_KEYBOARD_F16;
case SDLK_F17: return SkaInputKey_KEYBOARD_F17;
case SDLK_F18: return SkaInputKey_KEYBOARD_F18;
case SDLK_F19: return SkaInputKey_KEYBOARD_F19;
case SDLK_F20: return SkaInputKey_KEYBOARD_F20;
case SDLK_F21: return SkaInputKey_KEYBOARD_F21;
case SDLK_F22: return SkaInputKey_KEYBOARD_F22;
case SDLK_F23: return SkaInputKey_KEYBOARD_F23;
case SDLK_F24: return SkaInputKey_KEYBOARD_F24;
case SDLK_AC_BACK: return SkaInputKey_KEYBOARD_APP_BACK;
case SDLK_AC_FORWARD: return SkaInputKey_KEYBOARD_APP_FORWARD;
default: return SkaInputKey_INVALID;
}
}


SkaInputKey ska_sdl_mouse_button_to_input_key(SDL_MouseButtonEvent button) {
switch (button.button) {
case SDL_BUTTON_LEFT: return SkaInputKey_MOUSE_BUTTON_LEFT;
case SDL_BUTTON_RIGHT: return SkaInputKey_MOUSE_BUTTON_RIGHT;
case SDL_BUTTON_MIDDLE: return SkaInputKey_MOUSE_BUTTON_MIDDLE;
default: return SkaInputKey_INVALID;
}
}

SkaInputKey ska_sdl_gamepad_button_to_input_key(SDL_GamepadButton button) {
switch (button) {
case SDL_GAMEPAD_BUTTON_SOUTH: return SkaInputKey_GAMEPAD_FACE_BUTTON_SOUTH;
case SDL_GAMEPAD_BUTTON_EAST: return SkaInputKey_GAMEPAD_FACE_BUTTON_EAST;
case SDL_GAMEPAD_BUTTON_WEST: return SkaInputKey_GAMEPAD_FACE_BUTTON_WEST;
case SDL_GAMEPAD_BUTTON_NORTH: return SkaInputKey_GAMEPAD_FACE_BUTTON_NORTH;
case SDL_GAMEPAD_BUTTON_BACK: return SkaInputKey_GAMEPAD_BACK;
case SDL_GAMEPAD_BUTTON_START: return SkaInputKey_GAMEPAD_START;
case SDL_GAMEPAD_BUTTON_LEFT_STICK: return SkaInputKey_GAMEPAD_LEFT_ANALOG_BUTTON;
case SDL_GAMEPAD_BUTTON_RIGHT_STICK: return SkaInputKey_GAMEPAD_RIGHT_ANALOG_BUTTON;
case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: return SkaInputKey_GAMEPAD_LEFT_SHOULDER;
case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return SkaInputKey_GAMEPAD_RIGHT_SHOULDER;
case SDL_GAMEPAD_BUTTON_DPAD_UP: return SkaInputKey_GAMEPAD_DPAD_UP;
case SDL_GAMEPAD_BUTTON_DPAD_DOWN: return SkaInputKey_GAMEPAD_DPAD_DOWN;
case SDL_GAMEPAD_BUTTON_DPAD_LEFT: return SkaInputKey_GAMEPAD_DPAD_LEFT;
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: return SkaInputKey_GAMEPAD_DPAD_RIGHT;
default: return SkaInputKey_INVALID;
}
}

SkaInputKey ska_sdl_gamepad_axis_to_input_key(SDL_GamepadAxis axis) {
switch (axis) {
case SDL_GAMEPAD_AXIS_LEFTX: return SkaInputKey_GAMEPAD_LEFT_ANALOG_2D_AXIS_X;
case SDL_GAMEPAD_AXIS_LEFTY: return SkaInputKey_GAMEPAD_LEFT_ANALOG_2D_AXIS_Y;
case SDL_GAMEPAD_AXIS_RIGHTX: return SkaInputKey_GAMEPAD_RIGHT_ANALOG_2D_AXIS_X;
case SDL_GAMEPAD_AXIS_RIGHTY: return SkaInputKey_GAMEPAD_RIGHT_ANALOG_2D_AXIS_Y;
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: return SkaInputKey_GAMEPAD_LEFT_TRIGGER;
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: return SkaInputKey_GAMEPAD_RIGHT_TRIGGER;
default: return SkaInputKey_INVALID;
}
}
29 changes: 29 additions & 0 deletions test/new_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,33 @@ void seika_input_test(void) {
const SkaVector2 fourthAxisInput = ska_input_get_axis_input(SkaInputAxis_RIGHT, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX);
TEST_ASSERT_EQUAL_FLOAT(1.0f, fourthAxisInput.x);
TEST_ASSERT_EQUAL_FLOAT(0.0f, fourthAxisInput.y);

TEST_MESSAGE("Testing input actions");
SkaInputActionHandle jumpActionHandle = ska_input_add_input_action(
"jump",
(SkaInputActionValue[]){ { .key = SkaInputKey_KEYBOARD_SPACE, .strengthThreshold = 0.5f }, { .key = SkaInputKey_KEYBOARD_RETURN, .strengthThreshold = 0.5f }, { SkaInputKey_INVALID } },
SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX
);
TEST_ASSERT_NOT_EQUAL_UINT32(SKA_INPUT_INVALID_INPUT_ACTION_HANDLE, jumpActionHandle);
jumpActionHandle = ska_input_find_input_action_handle("jump", SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX);
TEST_ASSERT_NOT_EQUAL_UINT32(SKA_INPUT_INVALID_INPUT_ACTION_HANDLE, jumpActionHandle);

ska_input_register_input_event(SkaInputSourceType_KEYBOARD, SkaInputKey_KEYBOARD_RETURN, SkaInputTriggerType_PRESSED, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX, 0.0f);
TEST_ASSERT_TRUE(ska_input_is_input_action_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_TRUE(ska_input_is_input_action_just_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_FALSE(ska_input_is_input_action_just_released(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_EQUAL_FLOAT(1.0f, ska_input_get_input_action_strength(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));

ska_input_new_frame();
TEST_ASSERT_TRUE(ska_input_is_input_action_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_FALSE(ska_input_is_input_action_just_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_FALSE(ska_input_is_input_action_just_released(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));

ska_input_register_input_event(SkaInputSourceType_KEYBOARD, SkaInputKey_KEYBOARD_RETURN, SkaInputTriggerType_RELEASED, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX, 0.0f);
TEST_ASSERT_FALSE(ska_input_is_input_action_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_FALSE(ska_input_is_input_action_just_pressed(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
TEST_ASSERT_TRUE(ska_input_is_input_action_just_released(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));

ska_input_remove_input_action(jumpActionHandle, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX);
TEST_ASSERT_EQUAL_UINT32(SKA_INPUT_INVALID_INPUT_ACTION_HANDLE, ska_input_find_input_action_handle("jump", SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX));
}

0 comments on commit 9afb71d

Please sign in to comment.