diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-14 23:11:27 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-14 23:16:39 +0200 |
| commit | ba49282bf8bc1e52db08a53bf3606d2324f8caf5 (patch) | |
| tree | 7c49b9e9c8142b8305036f2089471f5887710826 | |
| parent | 5dd41ca230e51b7526c4d37a7b94a1a59739d8d7 (diff) | |
PNG Cart Support
We now support loading cart roms from .p8.png file :tada:!
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | doc/journal.md | 4 | ||||
| -rw-r--r-- | include/picow.h | 2 | ||||
| -rw-r--r-- | src/png.c | 44 | ||||
| -rw-r--r-- | tools/extract-data-pod.c | 2 |
5 files changed, 51 insertions, 3 deletions
@@ -3,7 +3,7 @@ PICO8 = pico8 .PHONY: platform/sdl platform/sdl: platform/sdl/picow system ; -platform/sdl/picow: platform/sdl/main.c +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) .PHONY: system diff --git a/doc/journal.md b/doc/journal.md index 411554c..73225a0 100644 --- a/doc/journal.md +++ b/doc/journal.md @@ -1,3 +1,7 @@ +# 14.4.2025 + +Nothing too exciting in the way of PICO-8 development, but we did settle on a PNG parsing library (`stb`) and now support extracting PICO-8 32kB roms from `.p8.png` files. It was mostly painless and not very interesting. + # 12.4.2025 Started looking into WASM runtimes to use, in particular ones that are portable C. The leading candidate right now is WebAssembly Micro Runtime (WAMR). Note that it even has Emscripten library support, so we have way fewer imports to implement. diff --git a/include/picow.h b/include/picow.h index f3a02ee..cc6286b 100644 --- a/include/picow.h +++ b/include/picow.h @@ -21,7 +21,7 @@ union pw_pixel { }; struct pw_png { - union pw_pixel image[160][205]; + union pw_pixel image[205][160]; }; bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize); diff --git a/src/png.c b/src/png.c new file mode 100644 index 0000000..defc7d1 --- /dev/null +++ b/src/png.c @@ -0,0 +1,44 @@ +#include "picow.h" + +#define STBI_ONLY_PNG +#define STBI_MAX_DIMENSIONS (160 * 205) +#define STBI_NO_FAILURE_STRINGS +#define STBI_NO_LINEAR +#define STBI_NO_STDIO +#define STBI_NO_THREAD_LOCALS +#define STB_IMAGE_IMPLEMENTATION +#include <stb/stb_image.h> + +_Static_assert(sizeof(union pw_pixel) == 4, "unexpected pixel size"); +_Static_assert(sizeof(struct pw_png) == 4 * 160 * 205, "unexpected png image size"); + +bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize) { + int x, y, n; + int ok = stbi_info_from_memory(data, datasize, &x, &y, &n); + if (!ok || x != 160 || y != 205) { + return false; + } + + unsigned char *image = stbi_load_from_memory(data, datasize, &x, &y, &n, 4); + if (!image) { + return false; + } + + memcpy(png->image, image, sizeof png->image); + stbi_image_free(image); + + 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)); + } +} diff --git a/tools/extract-data-pod.c b/tools/extract-data-pod.c index e2cc651..8770b3b 100644 --- a/tools/extract-data-pod.c +++ b/tools/extract-data-pod.c @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) { } } - FILE *o = fopen(argv[2], "w"); + FILE *o = fopen(argv[2], "wb"); check(o, "fopen(output)"); size_t datalen = (size_t)(dataptr - data); |
