aboutsummaryrefslogtreecommitdiff
path: root/src/cart.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cart.c')
-rw-r--r--src/cart.c59
1 files changed, 36 insertions, 23 deletions
diff --git a/src/cart.c b/src/cart.c
index f639aec..8899cd4 100644
--- a/src/cart.c
+++ b/src/cart.c
@@ -10,7 +10,7 @@ static bool cart_file_path(struct pw_cart_file *file, char const *dir,
char const *filename, char const *ext) {
if (!pw_pathjoin(file->path, PW_CART_PATH_MAX, dir, filename, NULL) ||
!pw_stripext(file->name, PW_CART_NAME_MAX, filename, ext)) {
- SDL_SetError("invalid cart file name %s", filename);
+ SDL_SetError("invalid file name %s", filename);
return false;
}
@@ -48,22 +48,24 @@ struct pw_carts *pw_carts_list(char const *dir) {
goto error;
}
- size_t count = (size_t)icount;
- if (count > SIZE_MAX / sizeof(struct pw_cart_file)) {
+ size_t max_count = (size_t)icount;
+ if (max_count > SIZE_MAX / sizeof(struct pw_cart_file)) {
goto error;
}
- carts = SDL_malloc(sizeof(*carts) + count * sizeof(*carts->files));
+ carts = SDL_malloc(sizeof(*carts) + max_count * sizeof(*carts->files));
if (!carts) {
goto error;
}
- carts->count = 0;
- for (size_t i = 0; i < count; i++) {
- struct pw_cart_file *file = &carts->files[i];
+ size_t count = 0;
+ for (size_t i = 0; i < max_count; i++) {
+ struct pw_cart_file *file = &carts->files[count];
if (!cart_file_path(file, dir, cartfiles[i], ".png")) {
- goto error;
+ SDL_LogWarn(0, "unsupported cart file: %s", SDL_GetError());
+ continue;
}
+ count++;
}
carts->count = count;
@@ -140,20 +142,10 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) {
goto error;
}
- union pw_png_pixel *image = pw_png_load(
- pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size,
- PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
- if (!image) {
+ if (!pw_cart_png_reset(png)) {
goto error;
}
- bool ok = blit_image(image, png->texture);
- SDL_free(image);
- if (!ok) {
- goto error;
- }
-
- SDL_zeroa(png->rom);
return png;
error:
@@ -247,17 +239,38 @@ cleanup:
bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png) {
if (!file) {
- SDL_InvalidParamError("file");
- return false;
+ return SDL_InvalidParamError("file");
}
if (!png) {
- SDL_InvalidParamError("png");
- return false;
+ return SDL_InvalidParamError("png");
}
return load_cart_png(file->path, png->texture, png->rom);
}
+bool pw_cart_png_reset(struct pw_cart_png *png) {
+ if (!png) {
+ return SDL_InvalidParamError("png");
+ }
+
+ union pw_png_pixel *image = pw_png_load(
+ pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size,
+ PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
+ if (!image) {
+ SDL_LogCritical(0, "unexpected error parsing builtin unknown cart PNG");
+ return false;
+ }
+
+ bool ok = blit_image(image, png->texture);
+ SDL_free(image);
+ if (!ok) {
+ return false;
+ }
+
+ SDL_zeroa(png->rom);
+ return true;
+}
+
static struct pw_fs_node_buffer *embedded_cart(struct pw_fs_entry *e,
char const **filename) {
static char const cart_dir[] = "user_data/carts/";