aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-05-09 21:54:16 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-05-09 21:54:16 +0200
commit775d758653a196184ee4e572185a9092eb72c0d8 (patch)
tree5afd491dd3e5535d37e2202358ac6bb05a40f20a
parent8d9137cf56c0a38cf1a398ab6afd30a674ce4fc2 (diff)
Refactor PICO-w and Platform Code
We move some stuff to the `picow` library, and move around code in the SDL platform. I'm pretty set on making the SDL platform the "defacto" platform implementation.
-rw-r--r--Makefile11
-rw-r--r--include/picow.h28
-rw-r--r--platform/sdl/main.c100
-rw-r--r--src/picow.c13
-rw-r--r--src/png.c10
5 files changed, 88 insertions, 74 deletions
diff --git a/Makefile b/Makefile
index 334df24..25c9f65 100644
--- a/Makefile
+++ b/Makefile
@@ -6,11 +6,11 @@ CLANG_TIDY ?= clang-tidy
.PHONY: platform/sdl
platform/sdl: platform/sdl/picow system ;
-platform/sdl/picow: platform/sdl/main.c src/png.c include/picow.h
- $(CC) -o $@ $< src/png.c -Iinclude/ -Ilib/ $$(pkg-config SDL3 --cflags --libs)
+platform/sdl/picow: platform/sdl/main.c src/picow.c src/png.c include/picow.h
+ $(CC) -o $@ $< src/picow.c src/png.c -Iinclude/ -Ilib/ $$(pkg-config SDL3 --cflags --libs)
.PHONY: system
-system: system/pico8.data.pod system/pico8.wasm ;
+system: system/pico8.data.pod system/pico8.wasm system/boot.p8.rom ;
system/pico8.data.pod: export/pico8.js tools/extract-data-pod.exe system/.dir
tools/extract-data-pod.exe $< $@
@@ -18,6 +18,9 @@ system/pico8.data.pod: export/pico8.js tools/extract-data-pod.exe system/.dir
system/pico8.wasm: export/pico8.wasm system/.dir
cp $< $@
+system/boot.p8.rom: carts/boot.p8 system/.dir
+ $(PICO8) $< -export $@
+
export/%.html export/%.js export/%.wasm: export/%.w ;
export/%.w: carts/%.p8 export/.dir
$(PICO8) $< -export "-w $(@:.w=.html)"
@@ -39,4 +42,4 @@ tools/%.exe: tools/%.c
.PHONY: check
check:
@$(CLANG_FORMAT) -i include/*.h src/*.c platform/*/*.c tools/*.c
- @$(CLANG_TIDY) src/*.c --header-filter=include/ -- -Iinclude/ -Ilib/
+ @$(CLANG_TIDY) src/*.c --checks=all --header-filter=include/ -- -Iinclude/ -Ilib/
diff --git a/include/picow.h b/include/picow.h
index 41de424..f1b6aab 100644
--- a/include/picow.h
+++ b/include/picow.h
@@ -5,14 +5,9 @@
#include <stddef.h>
#include <stdint.h>
-struct pw_cart {
- char name[32];
- uint8_t rom[32 * 1024];
-};
-
union pw_pixel {
uint32_t raw;
- struct {
+ struct pw_pixel_channels {
uint8_t r;
uint8_t g;
uint8_t b;
@@ -24,9 +19,18 @@ struct pw_png {
union pw_pixel image[205][160];
};
-union pw_buttons {
+bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize);
+
+struct pw_cart {
+ char name[32];
+ uint8_t rom[32 * 1024];
+};
+
+void pw_load_png_cart(struct pw_cart *cart, struct pw_png *png);
+
+union pw_gamepad {
uint8_t raw;
- struct {
+ struct pw_gamepad_buttons {
bool left : 1;
bool right : 1;
bool up : 1;
@@ -38,7 +42,11 @@ union pw_buttons {
} b;
};
-bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize);
-void pw_png_load_rom(struct pw_png *png, struct pw_cart *cart);
+struct pw_state {
+ size_t ncarts;
+ struct pw_cart *carts;
+
+ union pw_gamepad gamepads[8];
+};
#endif
diff --git a/platform/sdl/main.c b/platform/sdl/main.c
index 060860c..7887b3f 100644
--- a/platform/sdl/main.c
+++ b/platform/sdl/main.c
@@ -20,19 +20,13 @@ static void usage(char const *name) {
}
struct appstate {
- size_t ncarts;
- struct pw_cart *carts;
+ struct pw_state picow;
SDL_Window *window;
SDL_Renderer *renderer;
- SDL_Texture *fb;
+ SDL_Texture *framebuffer;
SDL_FRect target;
-
- struct gamepad {
- SDL_Gamepad *device;
- // TODO(nlordell): This should probably be `pw_state`.
- union pw_buttons buttons;
- } gamepads[8];
+ SDL_Gamepad *gamepads[8];
};
#define FAIL(msg) \
@@ -72,7 +66,7 @@ static bool load_cart(struct pw_cart *cart, char *s) {
SDL_SetError("failed to decode cart .png");
goto cleanup;
}
- pw_png_load_rom(&png, cart);
+ pw_load_png_cart(cart, &png);
} else {
size_t n = SDL_ReadIO(rom, cart->rom, sizeof cart->rom);
if (n != sizeof cart->rom) {
@@ -115,7 +109,7 @@ cleanup:
static SDL_AppResult update_target_rect(struct appstate *state, int w, int h) {
float ww = (float)w, wh = (float)h;
float fbw, fbh;
- if (!SDL_GetTextureSize(state->fb, &fbw, &fbh)) {
+ if (!SDL_GetTextureSize(state->framebuffer, &fbw, &fbh)) {
return FAIL("failed to lock framebuffer");
}
@@ -132,10 +126,10 @@ static SDL_AppResult update_target_rect(struct appstate *state, int w, int h) {
static SDL_AppResult add_gamepad(struct appstate *state,
SDL_GamepadDeviceEvent *event) {
size_t ngamepads = SDL_arraysize(state->gamepads);
- size_t i;
+ size_t i;
for (i = 0; i < ngamepads; i++) {
- if (state->gamepads[i].device == NULL) {
+ if (state->gamepads[i] == NULL) {
break;
}
}
@@ -149,43 +143,47 @@ static SDL_AppResult add_gamepad(struct appstate *state,
return FAIL("failed to open gamepad");
}
- SDL_LogDebug(0, "connected gamepad to player %zu", i + 1);
- state->gamepads[i].device = gamepad;
+ SDL_LogDebug(0, "connected gamepad for player %zu", i + 1);
+ state->gamepads[i] = gamepad;
return SDL_APP_CONTINUE;
}
-static struct gamepad *find_gamepad(struct appstate *state, SDL_JoystickID id) {
+static SDL_AppResult remove_gamepad(struct appstate *state,
+ SDL_GamepadDeviceEvent *event) {
size_t ngamepads = SDL_arraysize(state->gamepads);
for (size_t i = 0; i < ngamepads; i++) {
- if (SDL_GetGamepadID(state->gamepads[i].device) == id) {
- return &state->gamepads[i];
+ if (SDL_GetGamepadID(state->gamepads[i]) == event->which) {
+ SDL_CloseGamepad(state->gamepads[i]);
+ SDL_LogDebug(0, "disconnected gamepad for player %zu", i + 1);
+ state->picow.gamepads[i].raw = 0;
+ break;
}
}
- return NULL;
+ return SDL_APP_CONTINUE;
}
-static SDL_AppResult remove_gamepad(struct appstate *state,
- SDL_GamepadDeviceEvent *event) {
- struct gamepad *gamepad = find_gamepad(state, event->which);
- if (gamepad != NULL) {
- SDL_CloseGamepad(gamepad->device);
- gamepad->device = NULL;
- gamepad->buttons.raw = 0;
+static union pw_gamepad *get_gamepad_state(struct appstate *state,
+ SDL_JoystickID id) {
+ size_t ngamepads = SDL_arraysize(state->gamepads);
+ for (size_t i = 0; i < ngamepads; i++) {
+ if (SDL_GetGamepadID(state->gamepads[i]) == id) {
+ return &state->picow.gamepads[i];
+ }
}
- return SDL_APP_CONTINUE;
+ return NULL;
}
static SDL_AppResult handle_axis(struct appstate *state,
SDL_GamepadAxisEvent *event) {
- struct gamepad *gamepad = find_gamepad(state, event->which);
+ union pw_gamepad *gamepad = get_gamepad_state(state, event->which);
if (gamepad == NULL) {
return SDL_APP_CONTINUE;
}
#define MAP_AXIS(sdlaxis, minus, plus) \
case sdlaxis: \
- gamepad->buttons.b.minus = event->value < -10000; \
- gamepad->buttons.b.plus = event->value > 10000; \
+ gamepad->b.minus = event->value < -10000; \
+ gamepad->b.plus = event->value > 10000; \
break
switch (event->axis) {
@@ -196,7 +194,7 @@ static SDL_AppResult handle_axis(struct appstate *state,
// We also update the gamepad for player 2, which receives
// keypresses from the player 1's device to make playing games like
// POOM more fun.
- if (gamepad == &state->gamepads[0] && state->gamepads[1].device == NULL) {
+ if (gamepad == &state->picow.gamepads[0] && state->gamepads[1] == NULL) {
gamepad++;
switch (event->axis) {
MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHTX, left, right);
@@ -213,14 +211,14 @@ static SDL_AppResult handle_axis(struct appstate *state,
static SDL_AppResult handle_button(struct appstate *state,
SDL_GamepadButtonEvent *event) {
- struct gamepad *gamepad = find_gamepad(state, event->which);
+ union pw_gamepad *gamepad = get_gamepad_state(state, event->which);
if (gamepad == NULL) {
return SDL_APP_CONTINUE;
}
#define MAP_BUTTON(sdlbtn, gbtn) \
case sdlbtn: \
- gamepad->buttons.b.gbtn = event->down; \
+ gamepad->b.gbtn = event->down; \
break
switch (event->button) {
@@ -249,8 +247,8 @@ static SDL_AppResult handle_keyboard(struct appstate *state,
}
static void blit_framebuffer(struct appstate *state, uint32_t *fb) {
- uint8_t r = state->gamepads[0].buttons.raw << 1;
- uint8_t g = state->gamepads[1].buttons.raw << 2;
+ uint8_t r = state->picow.gamepads[0].raw << 1;
+ uint8_t g = state->picow.gamepads[1].raw << 2;
union pw_pixel px = {.ch = {r, g, 0xff, 0xff}};
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
@@ -259,7 +257,7 @@ static void blit_framebuffer(struct appstate *state, uint32_t *fb) {
}
}
-SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) {
+SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
struct appstate *state = SDL_calloc(1, sizeof *state);
*appstate = state;
@@ -293,16 +291,16 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) {
}
SDL_SetLogPriority(0, logp);
- state->ncarts = argc - optind;
- if (state->ncarts == 0) {
+ state->picow.ncarts = argc - optind;
+ if (state->picow.ncarts == 0) {
fprintf(stderr, "ERROR: must specify at least one cart.\n");
return SDL_APP_FAILURE;
}
- state->carts = SDL_malloc(sizeof(struct pw_cart[state->ncarts]));
- for (int i = 0; i < state->ncarts; i++) {
+ state->picow.carts = SDL_malloc(sizeof(struct pw_cart[state->picow.ncarts]));
+ for (int i = 0; i < state->picow.ncarts; i++) {
char *s = argv[optind + i];
- if (!load_cart(&state->carts[i], s)) {
+ if (!load_cart(&state->picow.carts[i], s)) {
return FAILF("could not load cart '%s'", s);
}
}
@@ -314,9 +312,11 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) {
return FAIL("failed to create window");
}
- state->fb = SDL_CreateTexture(state->renderer, SDL_PIXELFORMAT_RGBA32,
- SDL_TEXTUREACCESS_STREAMING, 128, 128);
- if (state->fb == NULL || !SDL_SetWindowMinimumSize(state->window, 128, 128)) {
+ state->framebuffer =
+ SDL_CreateTexture(state->renderer, SDL_PIXELFORMAT_RGBA32,
+ SDL_TEXTUREACCESS_STREAMING, 128, 128);
+ if (state->framebuffer == NULL ||
+ !SDL_SetWindowMinimumSize(state->window, 128, 128)) {
return FAIL("failed to create framebuffer");
}
@@ -328,14 +328,14 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
void *fb;
int pitch;
- if (!SDL_LockTexture(state->fb, NULL, &fb, &pitch)) {
+ if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &pitch)) {
return FAIL("failed to lock framebuffer");
}
blit_framebuffer(state, (uint32_t *)fb);
- SDL_UnlockTexture(state->fb);
+ SDL_UnlockTexture(state->framebuffer);
SDL_RenderClear(state->renderer);
- SDL_RenderTexture(state->renderer, state->fb, NULL, &state->target);
+ SDL_RenderTexture(state->renderer, state->framebuffer, NULL, &state->target);
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
@@ -375,12 +375,12 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
struct appstate *state = appstate;
for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) {
- SDL_CloseGamepad(state->gamepads[i].device);
+ SDL_CloseGamepad(state->gamepads[i]);
}
- SDL_DestroyTexture(state->fb);
+ SDL_DestroyTexture(state->framebuffer);
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
- SDL_free(state->carts);
+ SDL_free(state->picow.carts);
SDL_free(state);
SDL_Log(0, "bye");
diff --git a/src/picow.c b/src/picow.c
index 8b13789..a153c7f 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -1 +1,14 @@
+#include "picow.h"
+_Static_assert(sizeof(struct pw_gamepad_buttons) == 1,
+ "unexpected button state size");
+
+void pw_load_png_cart(struct pw_cart *cart, struct pw_png *png) {
+ 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];
+ cart->rom[i] = ((p.ch.a & 0b11) << 6) | ((p.ch.r & 0b11) << 4) |
+ ((p.ch.g & 0b11) << 2) | ((p.ch.b & 0b11));
+ }
+}
diff --git a/src/png.c b/src/png.c
index b751c57..c907ce3 100644
--- a/src/png.c
+++ b/src/png.c
@@ -31,13 +31,3 @@ bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize) {
return true;
}
-
-void pw_png_load_rom(struct pw_png *png, struct pw_cart *cart) {
- 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];
- cart->rom[i] = ((p.ch.a & 0b11) << 6) | ((p.ch.r & 0b11) << 4) |
- ((p.ch.g & 0b11) << 2) | ((p.ch.b & 0b11));
- }
-}