diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-10 14:12:51 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-10 14:14:45 +0200 |
commit | 43ecfa66fa626f5cc1d461b20b3c477d3d37a285 (patch) | |
tree | 816b4d8601b97795d3aed2a2a057b1360453bc67 | |
parent | 75c1afb03e7a4f137d852548fb0eb13e4030b471 (diff) | |
download | tzr-43ecfa66fa626f5cc1d461b20b3c477d3d37a285.tar.gz |
switch to GameController API
-rw-r--r-- | headers/TZR_globals.h | 6 | ||||
-rw-r--r-- | sources/TZR_CycleEvents.c | 16 | ||||
-rw-r--r-- | sources/TZR_PollEvent.c | 22 | ||||
-rw-r--r-- | sources/TZR_Quit.c | 2 | ||||
-rw-r--r-- | sources/globals.c | 6 |
5 files changed, 30 insertions, 22 deletions
diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index e12f31d..e63859b 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -2,7 +2,7 @@ #include "TZR_types.h" #include <stddef.h> #include <SDL2/SDL_scancode.h> -#include <SDL2/SDL_joystick.h> +#include <SDL2/SDL_gamecontroller.h> extern TZR_Config ___tzr_config; extern TZR_Color ___tzr_color; @@ -20,11 +20,11 @@ extern int ___tzr_mouse_x; extern int ___tzr_mouse_y; extern TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES]; extern TZR_KeyState ___tzr_mousestates[256]; -extern TZR_KeyState ___tzr_joystates[256]; +extern TZR_KeyState ___tzr_joystates[SDL_CONTROLLER_BUTTON_MAX]; extern float ___tzr_scale; extern int ___tzr_off_x; extern int ___tzr_off_y; extern Mix_Music *___tzr_music; -extern SDL_Joystick **___tzr_joysticks; +extern SDL_GameController **___tzr_joysticks; extern size_t ___tzr_joysticks_capacity; extern size_t ___tzr_joysticks_size; diff --git a/sources/TZR_CycleEvents.c b/sources/TZR_CycleEvents.c index 0a6a9af..29b5ec9 100644 --- a/sources/TZR_CycleEvents.c +++ b/sources/TZR_CycleEvents.c @@ -28,9 +28,23 @@ TZR_CycleEvents(void) next_state(&___tzr_keystates[i]); for (int i = 0; i < 256; i++) next_state(&___tzr_mousestates[i]); - for (int i = 0; i < 256; i++) + for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) next_state(&___tzr_joystates[i]); TZR_Event e; while (TZR_PollEvent(&e)) ; + + for (size_t i = 0; i < ___tzr_joysticks_size; i++) { + if (___tzr_joysticks[i] == NULL) + continue; + for (int k = 0; k < SDL_CONTROLLER_BUTTON_MAX; k++) { + if (SDL_GameControllerGetButton(___tzr_joysticks[i], k)) { + if (___tzr_joystates[k] != TZR_KEYSTATE_DOWN) + ___tzr_joystates[k] = TZR_KEYSTATE_PRESS; + } else { + if (___tzr_joystates[k] != TZR_KEYSTATE_UP) + ___tzr_joystates[k] = TZR_KEYSTATE_RELEASE; + } + } + } } diff --git a/sources/TZR_PollEvent.c b/sources/TZR_PollEvent.c index 651a084..893a0b7 100644 --- a/sources/TZR_PollEvent.c +++ b/sources/TZR_PollEvent.c @@ -1,8 +1,10 @@ #include "TZR_events.h" #include "TZR_globals.h" #include "sdl_error.h" -#include <SDL2/SDL_events.h> #include "reserve.h" +#include "getbind.h" +#include <SDL2/SDL_events.h> +#include <SDL2/SDL_gamecontroller.h> int TZR_PollEvent(TZR_Event *e) @@ -65,7 +67,7 @@ TZR_PollEvent(TZR_Event *e) i = ___tzr_joysticks_size - 1; } - SDL_Joystick *const jstick = SDL_JoystickOpen(se.jdevice.which); + SDL_GameController *jstick = SDL_GameControllerOpen(se.jdevice.which); if (jstick == NULL) { sdl_error(0); break; @@ -76,24 +78,16 @@ TZR_PollEvent(TZR_Event *e) } case SDL_JOYDEVICEREMOVED: for (size_t i = 0; i < ___tzr_joysticks_size; i++) { - if (se.jdevice.which == - SDL_JoystickInstanceID(___tzr_joysticks[i])) - { - SDL_JoystickClose(___tzr_joysticks[i]); + SDL_Joystick *const jstick = + SDL_GameControllerGetJoystick(___tzr_joysticks[i]); + if (se.jdevice.which == SDL_JoystickInstanceID(jstick)) { + SDL_GameControllerClose(___tzr_joysticks[i]); ___tzr_joysticks[i] = NULL; break; } } fprintf(stderr, "didn't remove no joystick\n"); break; - case SDL_JOYAXISMOTION: - break; - case SDL_JOYBUTTONDOWN: - ___tzr_keystates[se.jbutton.button] = TZR_KEYSTATE_PRESS; - break; - case SDL_JOYBUTTONUP: - ___tzr_keystates[se.jbutton.button] = TZR_KEYSTATE_RELEASE; - break; default: break; } diff --git a/sources/TZR_Quit.c b/sources/TZR_Quit.c index 4998629..2cf708b 100644 --- a/sources/TZR_Quit.c +++ b/sources/TZR_Quit.c @@ -14,7 +14,7 @@ TZR_Quit(void) ___tzr_resources_size = 0; ___tzr_resources_capacity = 0; for (size_t i = 0; i < ___tzr_joysticks_size; i++) - SDL_JoystickClose(___tzr_joysticks[i]); + SDL_GameControllerClose(___tzr_joysticks[i]); if (___tzr_joysticks != NULL) free(___tzr_joysticks); ___tzr_joysticks = NULL; diff --git a/sources/globals.c b/sources/globals.c index 2d69846..a048771 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -2,7 +2,7 @@ #include <stddef.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_scancode.h> -#include <SDL2/SDL_joystick.h> +#include <SDL2/SDL_gamecontroller.h> TZR_Config ___tzr_config = {0}; TZR_Color ___tzr_color = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; @@ -20,11 +20,11 @@ int ___tzr_mouse_x = 0; int ___tzr_mouse_y = 0; TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES] = {0}; TZR_KeyState ___tzr_mousestates[256] = {0}; //doc says than mouse button is u8 -TZR_KeyState ___tzr_joystates[256] = {0}; //doc says than joy button is u8 +TZR_KeyState ___tzr_joystates[SDL_CONTROLLER_BUTTON_MAX] = {0}; float ___tzr_scale = 1.0; int ___tzr_off_x = 1.0; int ___tzr_off_y = 1.0; Mix_Music *___tzr_music = NULL; -SDL_Joystick **___tzr_joysticks = NULL; +SDL_GameController **___tzr_joysticks = NULL; size_t ___tzr_joysticks_capacity = 0; size_t ___tzr_joysticks_size = 0; |