Add a method that automatically opens gamepads so they will send events

This commit is contained in:
2026-05-21 21:43:51 -04:00
parent f695a035c8
commit e3edd5b855
6 changed files with 35 additions and 4 deletions

View File

@@ -22,6 +22,33 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_controller_list_keyboards(void)
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *akgl_controller_open_gamepads(void)
{
int count = 0;
int i = 0;
SDL_JoystickID *gamepads = NULL;
SDL_Gamepad *gamepad = NULL;
PREPARE_ERROR(e);
if ( SDL_HasGamepad() ) {
gamepads = SDL_GetGamepads(&count);
if ( count > 0 ) {
FAIL_ZERO_RETURN(e, gamepads, AKERR_NULLPOINTER, "%s", SDL_GetError());
for ( i = 0; i < count ; i++ ) {
gamepad = SDL_OpenGamepad(gamepads[i]);
FAIL_ZERO_RETURN(e, gamepad, AKERR_NULLPOINTER, "%s", SDL_GetError());
SDL_Log("Gamepad %d is %s", i, SDL_GetGamepadNameForID(gamepads[i]));
}
SDL_free(gamepads);
} else {
SDL_Log("No gamepads enumerated");
}
} else {
SDL_Log("No gamepads connected");
}
SUCCEED_RETURN(e);
}
akerr_ErrorContext *akgl_controller_handle_event(void *appstate, SDL_Event *event)
{
int i = 0;

View File

@@ -85,6 +85,8 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_game_init()
FAIL_ZERO_RETURN(errctx, 0, AKGL_ERR_SDL, "%s", SDL_GetError());
}
}
PASS(errctx, akgl_controller_open_gamepads());
SUCCEED_RETURN(errctx);
}
@@ -137,7 +139,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_game_init_screen()
camera.y = 0;
camera.w = screenwidth;
camera.h = screenheight;
SUCCEED(e);
SUCCEED_RETURN(e);
}
void akgl_game_updateFPS()

View File

@@ -115,7 +115,7 @@ akerr_ErrorContext *akgl_heap_release_actor(akgl_Actor *ptr)
if ( ptr->refcount == 0 ) {
for ( i = 0; i < AKGL_ACTOR_MAX_CHILDREN; i++ ) {
if ( ptr->children[i] != NULL ) {
CATCH_AND_RETURN(errctx, akgl_heap_release_actor(ptr->children[i]));
PASS(errctx, akgl_heap_release_actor(ptr->children[i]));
}
}
SDL_ClearProperty(AKGL_REGISTRY_ACTOR, (char *)&ptr->name);

View File

@@ -149,7 +149,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_registry_load_properties(char *fname)
} FINISH(errctx, true);
}
SDL_Log("Properties loaded");
SUCCEED(errctx);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext AKERR_NOIGNORE *akgl_set_property(char *name, char *src)