diff options
Diffstat (limited to 'src/cart.c')
| -rw-r--r-- | src/cart.c | 71 |
1 files changed, 11 insertions, 60 deletions
@@ -2,30 +2,9 @@ #include "cart.h" #include "path.h" +#include "png.h" #include "sysroot.h" -#define STBI_ONLY_PNG -#define STBI_MAX_DIMENSIONS 205 -#define STBI_NO_FAILURE_STRINGS -#define STBI_NO_LINEAR -#define STBI_NO_STDIO -#define STBI_NO_THREAD_LOCALS -#define STBI_MALLOC(sz) SDL_malloc(sz) -#define STBI_REALLOC(p, sz) SDL_realloc(p, sz) -#define STBI_FREE(p) SDL_free(p) -#define STB_IMAGE_IMPLEMENTATION -#if defined(__GNUC__) || defined(__clang__) -static int stbi__addints_valid(int, int) __attribute__((unused)); -static int stbi__mul2shorts_valid(int, int) __attribute__((unused)); -#endif -#include <stb/stb_image.h> - -#ifdef SDL_PLATFORM_WINDOWS -#define PATH_SEP '\\' -#else -#define PATH_SEP '/' -#endif - 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_PATH_MAX, dir, filename, NULL) || @@ -167,37 +146,7 @@ void pw_carts_free(struct pw_carts *carts) { SDL_free(carts); } -union png_pixel { - uint32_t raw; - struct { - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - } ch; -}; - -static union png_pixel *load_png(uint8_t const *data, size_t sz) { - int x, y, n; - int ok = stbi_info_from_memory(data, sz, &x, &y, &n); - if (!ok || x != PW_PNG_WIDTH || y != PW_PNG_HEIGHT) { - SDL_SetError("malformed cart PNG header"); - return NULL; - } - - stbi_uc *image = stbi_load_from_memory(data, sz, &x, &y, &n, 4); - if (!image) { - SDL_SetError("malformed cart PNG"); - return NULL; - } - - // The desired channels are set to 4, making the STB image library ensure - // that the resulting image has exactly 4 8-bit colour channels per pixel; - // allowing us to treat the image as 160x205 32-bit pixels. - return (union png_pixel *)image; -} - -static bool blit_image(union png_pixel const *image, SDL_Texture *texture) { +static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { SDL_Surface *surface = NULL; if (!SDL_LockTextureToSurface(texture, NULL, &surface)) { return false; @@ -205,9 +154,10 @@ static bool blit_image(union png_pixel const *image, SDL_Texture *texture) { if (surface->w != PW_PNG_WIDTH || surface->h != PW_PNG_HEIGHT || surface->pitch != PW_PNG_WIDTH * 4 || - surface->format != SDL_PIXELFORMAT_RGBA32) { + surface->format != SDL_PIXELFORMAT_RGBA32 || + (uintptr_t)surface->pixels % sizeof(uint32_t)) { SDL_UnlockTexture(texture); - SDL_SetError("invalid cart PNG texture size or format"); + SDL_SetError("invalid cart PNG surface"); return false; } @@ -215,7 +165,7 @@ static bool blit_image(union png_pixel const *image, SDL_Texture *texture) { // steganographic encoding of the cart data. uint32_t *pixels = surface->pixels; for (size_t i = 0; i < PW_PNG_WIDTH * PW_PNG_HEIGHT; i++) { - union png_pixel px = image[i]; + union pw_png_pixel px = image[i]; px.raw &= 0xfcfcfcfc; px.ch.a |= 0x03; pixels[i] = px.raw; @@ -241,7 +191,8 @@ struct pw_cart *pw_cart_init(SDL_Renderer *renderer) { goto error; } - union png_pixel *image = load_png(unknown_p8_png.data, unknown_p8_png.size); + union pw_png_pixel *image = pw_png_load( + unknown_p8_png.data, unknown_p8_png.size, PW_PNG_WIDTH, PW_PNG_HEIGHT); if (!image) { goto error; } @@ -282,7 +233,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, bool result = false; uint8_t *data = NULL; - union png_pixel *image = NULL; + union pw_png_pixel *image = NULL; int64_t ssize = SDL_GetIOSize(png); if (ssize < 0) { @@ -304,7 +255,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, goto cleanup; } - image = load_png(data, size); + image = pw_png_load(data, size, PW_PNG_WIDTH, PW_PNG_HEIGHT); if (!image) { goto cleanup; } @@ -315,7 +266,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, // Read steganographically encoded cart ROM. for (size_t i = 0; i < PW_ROM_SIZE; i++) { - union png_pixel px = image[i]; + union pw_png_pixel px = image[i]; px.raw &= 0x03030303; rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b; } |
