diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-27 22:26:03 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-27 22:26:03 +0200 |
| commit | c27b738725ae25a2a074d6fcb0f83ee86183abd3 (patch) | |
| tree | e69f711bbb5728b4dcd6ce80c94c27aa46b9f6e1 /src | |
| parent | 02ac0ab99c1a7b727dc40b32138730401cb94d89 (diff) | |
Basic Scene Management
We now have two separate scenes with some basic code for managing
them as well as switching to a new scene:
- Cart selection
- In-game
Additionally, we started linting with `clang-tidy`; suppress some
warning around `fprintf` and `memset` usage which are fine in our
case.
Diffstat (limited to 'src')
| -rw-r--r-- | src/picow.c | 91 |
1 files changed, 63 insertions, 28 deletions
diff --git a/src/picow.c b/src/picow.c index 8e18aa4..a006c80 100644 --- a/src/picow.c +++ b/src/picow.c @@ -57,6 +57,11 @@ struct pw_gamepad { } state; }; +enum pw_scene { + PW_SCENE_CART_SELECTION, + PW_SCENE_GAME, +}; + struct pw_appstate { SDL_Window *window; SDL_Renderer *renderer; @@ -69,6 +74,8 @@ struct pw_appstate { int ncarts; int cartidx; int cartidx2; + enum pw_scene scene; + enum pw_scene scene2; }; #define FAIL(msg) \ @@ -176,7 +183,7 @@ static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, goto cleanup; } - SDL_memset(cart->name, 0, sizeof cart->name); + SDL_memset(cart->name, 0, sizeof cart->name); // NOLINT char *name = SDL_strrchr(file, '/'); name = name ? name + 1 : file; char *ext = SDL_strrchr(name, '.'); @@ -236,7 +243,8 @@ static void update_gamepad_state(struct pw_appstate *state, break; } - if (state->ncarts > 0 && pressed && gamepad == &state->gamepads[0].state) { + if (state->scene == PW_SCENE_CART_SELECTION && state->ncarts > 0 && pressed && + gamepad == &state->gamepads[0].state) { switch (button) { case PW_BUTTON_LEFT: state->cartidx2 = SDL_max(0, state->cartidx2 - 1); @@ -250,7 +258,10 @@ static void update_gamepad_state(struct pw_appstate *state, case PW_BUTTON_DOWN: state->cartidx2 = SDL_min(state->ncarts - 1, state->cartidx2 + 10); break; - default: + case PW_BUTTON_O: + case PW_BUTTON_X: + case PW_BUTTON_MENU: + state->scene2 = PW_SCENE_GAME; break; } } @@ -471,6 +482,42 @@ static void render_pxtexture(SDL_Renderer *renderer, SDL_RenderTexture(renderer, texture->handle, NULL, &texture->target); } +static SDL_AppResult scene_cart_selection(struct pw_appstate *state) { + if (state->cartidx != state->cartidx2) { + state->cartidx = state->cartidx2; + if (!load_cart(&state->cartpng, &state->cart, + state->carts[state->cartidx])) { + return FAIL("failed to load cart png"); + } + } + + SDL_RenderClear(state->renderer); + if (state->ncarts > 0) { + render_pxtexture(state->renderer, &state->cartpng); + } else { + render_pxtexture(state->renderer, &state->unknown); + } + SDL_RenderPresent(state->renderer); + + return SDL_APP_CONTINUE; +} + +static SDL_AppResult scene_game(struct pw_appstate *state) { + void *fb; + int pitch; + if (!SDL_LockTexture(state->framebuffer.handle, NULL, &fb, &pitch)) { + return FAIL("failed to lock framebuffer"); + } + blit_framebuffer(state, (uint32_t *)fb); + SDL_UnlockTexture(state->framebuffer.handle); + + SDL_RenderClear(state->renderer); + render_pxtexture(state->renderer, &state->framebuffer); + SDL_RenderPresent(state->renderer); + + return SDL_APP_CONTINUE; +} + SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { struct pw_appstate *state = SDL_calloc(1, sizeof *state); *appstate = state; @@ -494,12 +541,12 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { case 'v': logp--; if (logp == SDL_LOG_PRIORITY_INVALID) { - fprintf(stderr, "ERROR: specified -v too many times\n"); + fprintf(stderr, "ERROR: specified -v too many times\n"); // NOLINT return SDL_APP_FAILURE; } break; default: - fprintf(stderr, "ERROR: unknown option -%c\n", c); + fprintf(stderr, "ERROR: unknown option -%c\n", c); // NOLINT return SDL_APP_FAILURE; } } @@ -550,38 +597,26 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return FAIL("unable to load cart"); } + state->scene = PW_SCENE_CART_SELECTION; + state->scene2 = PW_SCENE_CART_SELECTION; return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppIterate(void *appstate) { struct pw_appstate *state = appstate; - if (state->cartidx != state->cartidx2) { - state->cartidx = state->cartidx2; - if (!load_cart(&state->cartpng, &state->cart, - state->carts[state->cartidx])) { - return FAIL("failed to load cart png"); - } + if (state->scene != state->scene2) { + state->scene = state->scene2; } - void *fb; - int pitch; - if (!SDL_LockTexture(state->framebuffer.handle, NULL, &fb, &pitch)) { - return FAIL("failed to lock framebuffer"); - } - blit_framebuffer(state, (uint32_t *)fb); - SDL_UnlockTexture(state->framebuffer.handle); - - SDL_RenderClear(state->renderer); - render_pxtexture(state->renderer, &state->framebuffer); - if (state->ncarts > 0) { - render_pxtexture(state->renderer, &state->cartpng); - } else { - render_pxtexture(state->renderer, &state->unknown); + switch (state->scene) { + case PW_SCENE_CART_SELECTION: + return scene_cart_selection(state); + break; + case PW_SCENE_GAME: + return scene_game(state); + break; } - SDL_RenderPresent(state->renderer); - - return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { |
