Fix assumptions about keyboard/gamepad mappings by waiting for input from the default controller on startup
This commit is contained in:
147
src/akgltest.c
147
src/akgltest.c
@@ -18,6 +18,10 @@
|
||||
#include <akgl/registry.h>
|
||||
#include <akgl/text.h>
|
||||
|
||||
#define AKGLTEST_STATE_WAITFORINPUT 1 << 8
|
||||
#define AKGLTEST_STATE_LOADING 1 << 9
|
||||
#define AKGLTEST_STATE_RUNNING 1 << 10
|
||||
|
||||
int numsprites = 8;
|
||||
char *spritepaths[] = {
|
||||
"assets/sprites/little_guy_walking_left.json",
|
||||
@@ -69,7 +73,6 @@ akerr_ErrorContext AKERR_NOIGNORE *user_breakpoint(akgl_Actor *obj, SDL_Event *e
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
akgl_Control control;
|
||||
akgl_Actor *actorptr = NULL;
|
||||
*appstate = (void *)&game.state;
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -84,6 +87,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
CATCH(errctx, akgl_game_init());
|
||||
CATCH(errctx, akgl_registry_load_properties("assets/properties.json"));
|
||||
CATCH(errctx, akgl_game_init_screen());
|
||||
CATCH(errctx, akgl_controller_list_keyboards());
|
||||
|
||||
for ( int i = 0; i < numsprites ; i++) {
|
||||
strcpy((char *)&dirnamebuf, spritepaths[i]);
|
||||
@@ -111,31 +115,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
strcpy((char *)&dirnamebuf, "assets/tilemap.tmj");
|
||||
CATCH(errctx, akgl_tilemap_load((char *)&dirnamebuf, (akgl_Tilemap *)&gamemap));
|
||||
|
||||
CATCH(errctx, akgl_controller_default(0, "player", 0, 0));
|
||||
|
||||
// set custom control maps */
|
||||
memset((void *)&control, 0x00, sizeof(akgl_Control));
|
||||
|
||||
// Breakpoint
|
||||
control.key = SDLK_ESCAPE;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &user_breakpoint;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Toggle the music
|
||||
control.key = SDLK_M;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &music_toggle;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Save the game
|
||||
control.key = SDLK_S;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &savegame;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
CATCH(errctx, akgl_text_loadfont("C64Pro", "assets/C64_Pro-STYLE.ttf", 18));
|
||||
|
||||
AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_WAITFORINPUT);
|
||||
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
@@ -146,6 +129,43 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgltest_controller_get_defaults(void *appstate, SDL_Event *event)
|
||||
{
|
||||
akgl_Control control;
|
||||
int kbid = 0;
|
||||
int jsid = 0;
|
||||
|
||||
PREPARE_ERROR(e);
|
||||
|
||||
if (event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event->type == SDL_EVENT_GAMEPAD_BUTTON_UP) {
|
||||
jsid = event->gbutton.which;
|
||||
} else if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
|
||||
kbid = event->key.which;
|
||||
}
|
||||
|
||||
PASS(e, akgl_controller_default(0, "player", kbid, jsid));
|
||||
// set custom control maps */
|
||||
memset((void *)&control, 0x00, sizeof(akgl_Control));
|
||||
|
||||
// Breakpoint
|
||||
control.key = SDLK_ESCAPE;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &user_breakpoint;
|
||||
PASS(e, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Toggle the music
|
||||
control.key = SDLK_M;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &music_toggle;
|
||||
PASS(e, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Save the game
|
||||
control.key = SDLK_S;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &savegame;
|
||||
PASS(e, akgl_controller_pushmap(0, &control));
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -154,7 +174,19 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer");
|
||||
FAIL_ZERO_BREAK(errctx, event, AKERR_NULLPOINTER, "NULL event pointer");
|
||||
|
||||
if ( event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN ||
|
||||
event->type == SDL_EVENT_GAMEPAD_BUTTON_UP ||
|
||||
event->type == SDL_EVENT_KEY_DOWN ||
|
||||
event->type == SDL_EVENT_KEY_UP ) {
|
||||
if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_WAITFORINPUT) ) {
|
||||
// get the player's default keyboard or gamepad id from the event
|
||||
CATCH(errctx, akgltest_controller_get_defaults(appstate, event));
|
||||
AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_WAITFORINPUT);
|
||||
AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_RUNNING);
|
||||
} else {
|
||||
CATCH(errctx, akgl_controller_handle_event(appstate, event));
|
||||
}
|
||||
}
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
@@ -164,10 +196,35 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgltest_iterate_waitforinput(void)
|
||||
{
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
akgl_game_updateFPS();
|
||||
|
||||
ATTEMPT {
|
||||
PASS(errctx, akgl_text_rendertextat(
|
||||
SDL_GetPointerProperty(
|
||||
AKGL_REGISTRY_FONT,
|
||||
"C64Pro",
|
||||
NULL),
|
||||
"PRESS ANY KEY OR JOYSTICK BUTTON",
|
||||
(SDL_Color){255, 255, 255, 255},
|
||||
0,
|
||||
40,
|
||||
120)
|
||||
);
|
||||
} CLEANUP {
|
||||
SDL_RenderPresent(renderer);
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgltest_iterate_running(void)
|
||||
{
|
||||
//SDL_FRect dest;
|
||||
//b2Vec2 position;
|
||||
int i = 0;
|
||||
akgl_Iterator opflags;
|
||||
char fpsText[32];
|
||||
@@ -183,20 +240,13 @@ SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
|
||||
for ( i = 0; i < AKGL_TILEMAP_MAX_LAYERS; i++ ) {
|
||||
opflags.layerid = i;
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_tilemap_draw(renderer, (akgl_Tilemap *)&gamemap, &camera, i));
|
||||
PASS(errctx, akgl_tilemap_draw(renderer, (akgl_Tilemap *)&gamemap, &camera, i));
|
||||
SDL_EnumerateProperties(AKGL_REGISTRY_ACTOR, &akgl_registry_iterate_actor, (void *)&opflags);
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
LOG_ERROR(errctx);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(errctx);
|
||||
}
|
||||
ATTEMPT {
|
||||
memset((char *)&fpsText, 0x00, 32);
|
||||
snprintf((char *)&fpsText, 31, "FPS : %d", game.fps);
|
||||
CATCH(errctx, akgl_text_rendertextat(
|
||||
PASS(errctx, akgl_text_rendertextat(
|
||||
SDL_GetPointerProperty(
|
||||
AKGL_REGISTRY_FONT,
|
||||
"C64Pro",
|
||||
@@ -211,10 +261,29 @@ SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
SDL_RenderPresent(renderer);
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
LOG_ERROR(errctx);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(errctx);
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
//SDL_FRect dest;
|
||||
//b2Vec2 position;
|
||||
|
||||
PREPARE_ERROR(e);
|
||||
|
||||
ATTEMPT {
|
||||
if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_RUNNING) ) {
|
||||
CATCH(e, akgltest_iterate_running());
|
||||
} else if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_WAITFORINPUT) ) {
|
||||
CATCH(e, akgltest_iterate_waitforinput());
|
||||
}
|
||||
} CLEANUP {
|
||||
} PROCESS(e) {
|
||||
} HANDLE_DEFAULT(e) {
|
||||
LOG_ERROR(e);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(e);
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user