Add preliminary joystick support for snake example

This commit is contained in:
DamianS-eng
2025-03-14 17:06:47 -04:00
committed by Sam Lantinga
parent aad1e35162
commit 60857935ca

View File

@@ -21,6 +21,9 @@
#define THREE_BITS 0x7U /* ~CHAR_MAX >> (CHAR_BIT - SNAKE_CELL_MAX_BITS) */
#define SHIFT(x, y) (((x) + ((y) * SNAKE_GAME_WIDTH)) * SNAKE_CELL_MAX_BITS)
static SDL_Joystick *joystick = NULL;
typedef enum
{
SNAKE_CELL_NOTHING = 0U,
@@ -238,6 +241,26 @@ static SDL_AppResult handle_key_event_(SnakeContext *ctx, SDL_Scancode key_code)
return SDL_APP_CONTINUE;
}
static SDL_AppResult handle_hat_event_(SnakeContext *ctx, Uint8 hat) {
switch (hat) {
case SDL_HAT_RIGHT:
snake_redir(ctx, SNAKE_DIR_RIGHT);
break;
case SDL_HAT_UP:
snake_redir(ctx, SNAKE_DIR_UP);
break;
case SDL_HAT_LEFT:
snake_redir(ctx, SNAKE_DIR_LEFT);
break;
case SDL_HAT_DOWN:
snake_redir(ctx, SNAKE_DIR_DOWN);
break;
default:
break;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void *appstate)
{
AppState *as = (AppState *)appstate;
@@ -305,7 +328,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
}
}
if (!SDL_Init(SDL_INIT_VIDEO)) {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
@@ -316,7 +340,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
*appstate = as;
if (!SDL_CreateWindowAndRenderer("examples/demo/snake", SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, 0, &as->window, &as->renderer)) {
if (!SDL_CreateWindowAndRenderer("examples/snake", SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, 0, &as->window, &as->renderer)) {
return SDL_APP_FAILURE;
}
@@ -333,6 +357,20 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
switch (event->type) {
case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS;
case SDL_EVENT_JOYSTICK_ADDED:
if (joystick == NULL) {
joystick = SDL_OpenJoystick(event->jdevice.which);
if (!joystick) {
SDL_Log("Failed to open joystick ID %u: %s", (unsigned int) event->jdevice.which, SDL_GetError());
}
}
case SDL_EVENT_JOYSTICK_REMOVED:
if (joystick && (SDL_GetJoystickID(joystick) == event->jdevice.which)) {
SDL_CloseJoystick(joystick);
joystick = NULL;
}
case SDL_EVENT_JOYSTICK_HAT_MOTION:
return handle_hat_event_(ctx, event->jhat.value);
case SDL_EVENT_KEY_DOWN:
return handle_key_event_(ctx, event->key.scancode);
}
@@ -341,6 +379,9 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
if (joystick) {
SDL_CloseJoystick(joystick);
}
if (appstate != NULL) {
AppState *as = (AppState *)appstate;
SDL_DestroyRenderer(as->renderer);