aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cart.c12
-rw-r--r--src/fb.c18
-rw-r--r--src/game.c53
-rw-r--r--src/input.c16
-rw-r--r--src/menu.c1
-rw-r--r--src/path.c2
-rw-r--r--src/picow.c10
-rw-r--r--src/player.c4
8 files changed, 75 insertions, 41 deletions
diff --git a/src/cart.c b/src/cart.c
index b460d30..31ea2ec 100644
--- a/src/cart.c
+++ b/src/cart.c
@@ -60,6 +60,8 @@ struct pw_carts *pw_carts_list(char const *dir) {
goto error;
}
+ // Set an SDL error while returning an empty carts list. This allows a
+ // caller to disambiguate between empty and missing directories.
SDL_SetError("missing carts directory");
carts->count = 0;
return carts;
@@ -73,6 +75,10 @@ struct pw_carts *pw_carts_list(char const *dir) {
}
size_t count = (size_t)icount;
+ if (count > SIZE_MAX / sizeof(struct pw_cart_entry)) {
+ goto error;
+ }
+
carts = SDL_malloc(sizeof(struct pw_carts) +
count * sizeof(struct pw_cart_entry));
if (!carts) {
@@ -349,8 +355,8 @@ static int cmp_cart_name(void const *a, void const *b) {
struct pw_multicart *pw_multicart_init(struct pw_cart_entry *entry,
struct pw_cart *cart) {
if (!entry) {
- SDL_InvalidParamError("cart");
- return false;
+ SDL_InvalidParamError("entry");
+ return NULL;
}
struct pw_multicart *multi = SDL_malloc(sizeof(*multi));
@@ -368,6 +374,8 @@ struct pw_multicart *pw_multicart_init(struct pw_cart_entry *entry,
for (size_t i = 0; i < entry->nmulti; ++i) {
SDL_memcpy(&dst->multi[i], &entry->multi[i], sizeof(struct pw_cart_file));
}
+ } else {
+ dst->multi = NULL;
}
SDL_memcpy(&dst->file, &entry->file, sizeof(struct pw_cart_file));
dst->nmulti = entry->nmulti;
diff --git a/src/fb.c b/src/fb.c
index 1e3d601..b57d924 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -3,7 +3,7 @@
struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) {
if (!renderer) {
SDL_InvalidParamError("renderer");
- return false;
+ return NULL;
}
SDL_Texture *texture = NULL;
@@ -32,10 +32,10 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) {
return fb;
error:
- if (!texture) {
+ if (texture) {
SDL_DestroyTexture(texture);
}
- if (!fb) {
+ if (fb) {
SDL_free(fb);
}
return NULL;
@@ -127,15 +127,17 @@ bool pw_fb_blit(struct pw_framebuffer *fb,
void *buffer;
int pitch;
- if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch) ||
- pitch != sizeof(uint32_t[PW_FB_WIDTH])) {
+ if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch)) {
return false;
}
- SDL_memcpy(buffer, pixels, sizeof(uint32_t[PW_FB_WIDTH * PW_FB_HEIGHT]));
- SDL_UnlockTexture(fb->texture);
+ bool ok = pitch == sizeof(uint32_t[PW_FB_WIDTH]);
+ if (ok) {
+ SDL_memcpy(buffer, pixels, sizeof(uint32_t[PW_FB_WIDTH * PW_FB_HEIGHT]));
+ }
- return true;
+ SDL_UnlockTexture(fb->texture);
+ return ok;
}
bool pw_fb_render(struct pw_framebuffer *fb) {
diff --git a/src/game.c b/src/game.c
index 72d5046..acc7f2a 100644
--- a/src/game.c
+++ b/src/game.c
@@ -1,3 +1,5 @@
+#include <math.h>
+
#include <SDL3/SDL_log.h>
#include "game.h"
@@ -52,9 +54,9 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer) {
struct RuntimeInitArgs init = {
.mem_alloc_type = Alloc_With_Allocator,
- .mem_alloc_option.allocator.malloc_func = &wasm_malloc,
- .mem_alloc_option.allocator.realloc_func = &wasm_realloc,
- .mem_alloc_option.allocator.free_func = &wasm_free,
+ .mem_alloc_option.allocator.malloc_func = (void *)wasm_malloc,
+ .mem_alloc_option.allocator.realloc_func = (void *)wasm_realloc,
+ .mem_alloc_option.allocator.free_func = (void *)wasm_free,
};
if (!wasm_runtime_full_init(&init)) {
SDL_SetError("unspecified error initializing WebAssembly runtime");
@@ -78,10 +80,11 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer) {
goto error;
}
- struct NativeGlobal genv[] = {};
- if (!wasm_runtime_link_globals(game->module, "env", genv,
- SDL_arraysize(genv)) ||
- !wasm_runtime_link_globals(game->module, "global", genv,
+ struct NativeGlobal genv[] = {
+ {"NaN", {WASM_F64, .of.f64 = NAN}},
+ {"Infinity", {WASM_F64, .of.f64 = INFINITY}},
+ };
+ if (!wasm_runtime_link_globals(game->module, "global", genv,
SDL_arraysize(genv))) {
SDL_SetError("unspecified error registiering host globals");
goto error;
@@ -112,6 +115,9 @@ void pw_game_free(struct pw_game *game) {
return;
}
+ if (game->exec) {
+ wasm_runtime_destroy_exec_env(game->exec);
+ }
if (game->instance) {
wasm_runtime_deinstantiate(game->instance);
}
@@ -123,6 +129,7 @@ void pw_game_free(struct pw_game *game) {
}
pw_input_free(game->input);
pw_fb_free(game->fb);
+ SDL_free(game);
}
bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
@@ -135,16 +142,23 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
return false;
}
- char err[1024];
-
if (game->cart) {
pw_multicart_free(game->cart);
}
game->cart = cart;
+ if (game->exec) {
+ wasm_runtime_destroy_exec_env(game->exec);
+ }
if (game->instance) {
wasm_runtime_deinstantiate(game->instance);
}
+ game->instance = NULL;
+ game->exec = NULL;
+ game->dyncall = NULL;
+ game->mix = NULL;
+
+ char err[1024];
game->instance =
wasm_runtime_instantiate(game->module, 1024, 0, err, sizeof err);
if (!game->instance) {
@@ -162,16 +176,16 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
wasm_function_inst_t mix =
wasm_runtime_lookup_function(game->instance, "_mix_0");
if (!ctor || !start || !stackalloc || !dyncall || !mix) {
- return SDL_SetError("could not required exports");
+ return SDL_SetError("could not find required exports");
}
- wasm_exec_env_t exec = wasm_runtime_create_exec_env(game->instance, 1024);
- if (exec == NULL) {
+ game->exec = wasm_runtime_create_exec_env(game->instance, 1024);
+ if (game->exec == NULL) {
return SDL_SetError("failed to create execution environment");
}
- wasm_runtime_set_user_data(exec, game);
+ wasm_runtime_set_user_data(game->exec, game);
- if (!wasm_runtime_call_wasm(exec, ctor, 0, NULL)) {
+ if (!wasm_runtime_call_wasm(game->exec, ctor, 0, NULL)) {
return SDL_SetError("error calling main");
}
@@ -179,7 +193,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
{
char const value[] = "./pico8";
uint32_t args[] = {(uint32_t)sizeof(value)};
- if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ if (!wasm_runtime_call_wasm(game->exec, stackalloc, 1, args)) {
return SDL_SetError("error allocating argument 0");
}
@@ -196,7 +210,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
{
uint32_t const value[] = {arg0, 0};
uint32_t args[] = {(uint32_t)sizeof(value)};
- if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) {
+ if (!wasm_runtime_call_wasm(game->exec, stackalloc, 1, args)) {
return SDL_SetError("error allocating arguments array");
}
@@ -212,7 +226,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
game->mainfn = 0;
{
uint32_t args[] = {1, argv};
- if (!wasm_runtime_call_wasm(exec, start, 2, args)) {
+ if (!wasm_runtime_call_wasm(game->exec, start, 2, args)) {
return SDL_SetError("error calling main");
}
if (args[0]) {
@@ -223,7 +237,6 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
return SDL_SetError("main loop function not set");
}
- game->exec = exec;
game->dyncall = dyncall;
game->mix = mix;
return true;
@@ -240,7 +253,7 @@ bool pw_game_event(struct pw_game *game, struct pw_players *players,
}
bool pw_game_iterate(struct pw_game *game) {
- if (!game) {
+ if (!game || !game->exec || !game->dyncall || !game->mix) {
SDL_InvalidParamError("game");
return false;
}
@@ -260,7 +273,7 @@ bool pw_game_iterate(struct pw_game *game) {
return SDL_SetError("error calling audio mixer");
}
if (!args[0]) {
- return SDL_SetError("audio mixing failed %d %d", args[0], args[1]);
+ return SDL_SetError("audio mixing failed");
}
}
{
diff --git a/src/input.c b/src/input.c
index 9e41ebe..db491dd 100644
--- a/src/input.c
+++ b/src/input.c
@@ -5,7 +5,7 @@
struct pw_input *pw_input_init(void) {
struct pw_input *input = SDL_malloc(sizeof(*input));
if (!input) {
- return false;
+ return NULL;
}
SDL_zerop(input);
@@ -195,7 +195,7 @@ bool pw_input_event(struct pw_input *input, struct pw_players *players,
return false;
}
if (!players) {
- SDL_InvalidParamError("input");
+ SDL_InvalidParamError("players");
return false;
}
if (!event) {
@@ -225,19 +225,19 @@ static void update_keymod(struct pw_input *input) {
input->keyboard.mod |= PW_KEYMOD_LSHIFT;
}
if (state & SDL_KMOD_RSHIFT) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ input->keyboard.mod |= PW_KEYMOD_RSHIFT;
}
if (state & SDL_KMOD_LCTRL) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ input->keyboard.mod |= PW_KEYMOD_LCTRL;
}
if (state & SDL_KMOD_RCTRL) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ input->keyboard.mod |= PW_KEYMOD_RCTRL;
}
if (state & SDL_KMOD_LALT) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ input->keyboard.mod |= PW_KEYMOD_LALT;
}
if (state & SDL_KMOD_RALT) {
- input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ input->keyboard.mod |= PW_KEYMOD_RALT;
}
}
@@ -299,7 +299,7 @@ enum pw_button pw_input_btn(struct pw_input *input, size_t player) {
// menu needs to be set if it is set for any player.
if (!player) {
for (size_t i = 1; i < PW_PLAYERS_MAX; i++) {
- button |= input->players[player] & PW_BUTTON_MENU;
+ button |= input->players[i] & PW_BUTTON_MENU;
}
}
diff --git a/src/menu.c b/src/menu.c
index c185cd9..1f1922f 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -157,6 +157,7 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) {
next = saturating_add(next, 1);
break;
}
+ break;
}
menu->selection.next = SDL_min(next, menu->carts->count - 1);
diff --git a/src/path.c b/src/path.c
index 2651b45..e2751a9 100644
--- a/src/path.c
+++ b/src/path.c
@@ -45,7 +45,7 @@ bool pw_stripext(char *name, size_t sz, char const *path, char const *ext) {
size_t plen = SDL_strlen(path);
size_t elen = SDL_strlen(ext);
size_t nlen = plen - elen;
- if (plen < elen || SDL_strcmp(path + nlen, ext)) {
+ if (plen < elen || SDL_strcasecmp(path + nlen, ext)) {
SDL_SetError("path does not have extension");
return false;
}
diff --git a/src/picow.c b/src/picow.c
index 599fea2..09c3953 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -59,6 +59,9 @@ struct pw_appstate {
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
struct pw_appstate *state = SDL_calloc(1, sizeof *state);
+ if (!state) {
+ return SDL_APP_FAILURE;
+ }
*appstate = state;
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
@@ -130,7 +133,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
case SDL_EVENT_GAMEPAD_REMOVED:
if (!pw_players_event(state->players, event)) {
return failf("failed to player controller connection events");
- };
+ }
break;
default:
switch (state->scene) {
@@ -179,6 +182,10 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
struct pw_appstate *state = appstate;
(void)result;
+ if (!state) {
+ goto bye;
+ }
+
pw_players_free(state->players);
pw_menu_free(state->menu);
pw_game_free(state->game);
@@ -186,5 +193,6 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
SDL_DestroyWindow(state->window);
SDL_free(state);
+bye:
SDL_Log("bye");
}
diff --git a/src/player.c b/src/player.c
index 7d2b37a..20c2570 100644
--- a/src/player.c
+++ b/src/player.c
@@ -59,6 +59,7 @@ static bool remove_gamepad(struct pw_players *players,
}
SDL_CloseGamepad(players->gamepads[i]);
+ players->gamepads[i] = NULL;
SDL_LogDebug(0, "disconnected gamepad for player %zu", i + 1);
return true;
}
@@ -96,7 +97,8 @@ size_t pw_players_count(struct pw_players *players) {
size_t pw_player_find(struct pw_players *players, SDL_JoystickID gamepad) {
for (size_t i = 0; i < PW_PLAYERS_MAX; i++) {
- if (SDL_GetGamepadID(players->gamepads[i]) == gamepad) {
+ if (players->gamepads[i] &&
+ SDL_GetGamepadID(players->gamepads[i]) == gamepad) {
return i;
}
}