aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/picow.c135
1 files changed, 97 insertions, 38 deletions
diff --git a/src/picow.c b/src/picow.c
index ef468c8..60bf80d 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -23,31 +23,34 @@ struct pw_cart {
uint8_t rom[32 * 1024];
};
-union pw_gamepad_state {
- uint8_t raw;
- struct pw_gamepad_buttons {
- bool left : 1;
- bool right : 1;
- bool up : 1;
- bool down : 1;
- bool o : 1;
- bool x : 1;
- bool menu : 1;
- bool reserved : 1;
- } b;
+struct pw_pxtexture {
+ SDL_Texture *handle;
+ SDL_FRect target;
};
struct pw_gamepad {
SDL_Gamepad *handle;
- union pw_gamepad_state state;
+ union pw_gamepad_state {
+ uint8_t raw;
+ struct pw_gamepad_buttons {
+ bool left : 1;
+ bool right : 1;
+ bool up : 1;
+ bool down : 1;
+ bool o : 1;
+ bool x : 1;
+ bool menu : 1;
+ bool reserved : 1;
+ } b;
+ } state;
};
struct pw_appstate {
SDL_Window *window;
SDL_Renderer *renderer;
- SDL_Texture *framebuffer;
+ struct pw_pxtexture framebuffer;
+ struct pw_pxtexture unknown;
struct pw_gamepad gamepads[8];
- SDL_FRect target;
};
#define FAIL(msg) \
@@ -55,10 +58,7 @@ struct pw_appstate {
#define FAILF(msg, ...) \
(SDL_LogCritical(0, msg ". %s", __VA_ARGS__, SDL_GetError()), SDL_APP_FAILURE)
-static void load_png_cart(struct pw_cart *cart, struct pw_png *png) {
-}
-
-static bool load_cart(struct pw_cart *cart, char *file) {
+static bool load_cart(struct pw_png *png, struct pw_cart *cart, char *file) {
bool result = false;
SDL_IOStream *rom = SDL_IOFromFile(file, "r");
@@ -79,8 +79,7 @@ static bool load_cart(struct pw_cart *cart, char *file) {
if (n != size) {
goto cleanup;
}
- struct pw_png png;
- if (!pw_png_decode(&png, data, n)) {
+ if (!pw_png_decode(png, data, n)) {
SDL_SetError("failed to decode cart .png");
goto cleanup;
}
@@ -88,7 +87,7 @@ static bool load_cart(struct pw_cart *cart, char *file) {
for (size_t i = 0; i < sizeof cart->rom; i++) {
size_t x = i % 160;
size_t y = i / 160;
- union pw_pixel p = png.image[y][x];
+ union pw_pixel p = png->image[y][x];
cart->rom[i] = ((p.ch.a & 0b11) << 6) | ((p.ch.r & 0b11) << 4) |
((p.ch.g & 0b11) << 2) | ((p.ch.b & 0b11));
}
@@ -111,20 +110,66 @@ cleanup:
return result;
}
-static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) {
+static bool load_unknown_png(struct pw_png *png) {
+ if (!pw_png_decode(png, pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.len)) {
+ SDL_SetError("failed to decode unknown.p8.png");
+ return false;
+ }
+
+ return true;
+}
+
+static bool init_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture, int w, int h) {
+ texture->handle =
+ SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
+ SDL_TEXTUREACCESS_STREAMING, w, h);
+ return texture->handle != NULL;
+}
+
+static bool update_pxtexture_target(struct pw_pxtexture *texture, int w, int h) {
float ww = (float)w, wh = (float)h;
float fbw, fbh;
- if (!SDL_GetTextureSize(state->framebuffer, &fbw, &fbh)) {
- return FAIL("failed to lock framebuffer");
+ if (!SDL_GetTextureSize(texture->handle, &fbw, &fbh)) {
+ return false;
}
float scale = SDL_floor(SDL_min(ww / fbw, wh / fbh));
- state->target.w = scale * fbw;
- state->target.h = scale * fbh;
+ texture->target.w = scale * fbw;
+ texture->target.h = scale * fbh;
+
+ texture->target.x = SDL_floor((ww - texture->target.w) / 2.0);
+ texture->target.y = SDL_floor((wh - texture->target.h) / 2.0);
- state->target.x = SDL_floor((ww - state->target.w) / 2.0);
- state->target.y = SDL_floor((wh - state->target.h) / 2.0);
+ return true;
+}
+
+static bool blit_cart_png(struct pw_pxtexture *texture, struct pw_png *png) {
+ SDL_Rect region = {0, 0, 160, 205};
+ uint32_t *fb;
+ int pitch;
+ if (!SDL_LockTexture(texture->handle, &region, (void **)&fb, &pitch)) {
+ return false;
+ }
+ // clear the steganographic encoding of the cart data, we only want to show
+ // it as an error texture.
+ for (size_t i = 0; i < 160 * 205; i++) {
+ size_t x = i % 160;
+ size_t y = i / 160;
+ fb[i] = png->image[y][x].raw & 0xfcfcfcfc;
+ }
+
+ SDL_UnlockTexture(texture->handle);
+ return true;
+}
+
+static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) {
+ if (!update_pxtexture_target(&state->framebuffer, w, h)) {
+ return FAIL("failed to update framebuffer target");
+ }
+ if (!update_pxtexture_target(&state->unknown, w, h)) {
+ return FAIL("failed to update framebuffer target");
+ }
return SDL_APP_CONTINUE;
}
@@ -282,6 +327,10 @@ static void blit_framebuffer(struct pw_appstate *state, uint32_t *fb) {
}
}
+static void render_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture) {
+ SDL_RenderTexture(renderer, texture->handle, NULL, &texture->target);
+}
+
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
struct pw_appstate *state = SDL_calloc(1, sizeof *state);
*appstate = state;
@@ -323,14 +372,22 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return FAIL("failed to create window");
}
- state->framebuffer =
- SDL_CreateTexture(state->renderer, SDL_PIXELFORMAT_RGBA32,
- SDL_TEXTUREACCESS_STREAMING, 128, 128);
- if (state->framebuffer == NULL ||
- !SDL_SetWindowMinimumSize(state->window, 128, 128)) {
+ if (!SDL_SetWindowMinimumSize(state->window, 160, 205)) {
+ return FAIL("failed to configure minimum window size");
+ }
+
+ if (!init_pxtexture(state->renderer, &state->framebuffer, 128, 128)) {
return FAIL("failed to create framebuffer");
}
+ if (!init_pxtexture(state->renderer, &state->unknown, 160, 205)) {
+ return FAIL("failed to create unknown cart texture");
+ }
+ struct pw_png unknown;
+ if (!load_unknown_png(&unknown) || !blit_cart_png(&state->unknown, &unknown)) {
+ return FAIL("failed to load unknown cart image");
+ }
+
return SDL_APP_CONTINUE;
}
@@ -339,14 +396,15 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
void *fb;
int pitch;
- if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &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);
+ SDL_UnlockTexture(state->framebuffer.handle);
SDL_RenderClear(state->renderer);
- SDL_RenderTexture(state->renderer, state->framebuffer, NULL, &state->target);
+ render_pxtexture(state->renderer, &state->framebuffer);
+ render_pxtexture(state->renderer, &state->unknown);
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
@@ -382,7 +440,8 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) {
SDL_CloseGamepad(state->gamepads[i].handle);
}
- SDL_DestroyTexture(state->framebuffer);
+ SDL_DestroyTexture(state->framebuffer.handle);
+ SDL_DestroyTexture(state->unknown.handle);
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
SDL_free(state);