diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-13 09:56:30 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-13 09:56:30 +0200 |
| commit | b5cc8d759a09ad2754efb671dc1af12d52fc542e (patch) | |
| tree | 8f2731fefaaa7b2afdfdbd80a768b72172b5a6c0 | |
| parent | e06434fd89345fdc4e931f6fa3904626d75097db (diff) | |
Added Debug Stats
We now print debug stats (ATM, just FPS) overlaying the actual game.
We use the PICO-8 font to keep things consistent; I probable should
choose a better color though...
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | carts/font.p8 | 88 | ||||
| -rw-r--r-- | src/cart.c | 71 | ||||
| -rw-r--r-- | src/config.c | 30 | ||||
| -rw-r--r-- | src/config.h | 1 | ||||
| -rw-r--r-- | src/debug.c | 169 | ||||
| -rw-r--r-- | src/debug.h | 35 | ||||
| -rw-r--r-- | src/fb.c | 4 | ||||
| -rw-r--r-- | src/game.c | 4 | ||||
| -rw-r--r-- | src/game.h | 5 | ||||
| -rw-r--r-- | src/menu.c | 8 | ||||
| -rw-r--r-- | src/menu.h | 5 | ||||
| -rw-r--r-- | src/picow.c | 45 | ||||
| -rw-r--r-- | src/png.c | 39 | ||||
| -rw-r--r-- | src/png.h | 24 | ||||
| -rw-r--r-- | tools/generate-sysroot.c | 1 |
16 files changed, 462 insertions, 75 deletions
@@ -65,7 +65,8 @@ $(SYSTEM)/sysroot.h $(SYSTEM)/sysroot.c: $(TOOLS)/generate-sysroot \ $(SYSTEM)/pico8.data.pod \ $(SYSTEM)/pico8.wasm \ $(SYSTEM)/stub/pico8.wasm \ - $(SYSTEM)/unknown.p8.png + $(SYSTEM)/unknown.p8.png \ + $(SYSTEM)/font.png @ echo "TOOL\t$(SYSTEM)/sysroot.h $(SYSTEM)/sysroot.c" @ $< $(SYSTEM) @@ -83,6 +84,11 @@ $(SYSTEM)/%.p8.png: carts/%.p8 @ mkdir -p $(dir $@) @ $(PICO8) $< -export $@ >/dev/null +$(SYSTEM)/%.png: carts/%.p8 + @ echo "PICO8\t$@" + @ mkdir -p $(dir $@) + @ $(PICO8) $< -export $@ >/dev/null + $(SYSTEM)/stub/%.wasm: stub/%.wat @ echo "WAT2WASM\t$@" @ mkdir -p $(dir $@) diff --git a/carts/font.p8 b/carts/font.p8 new file mode 100644 index 0000000..4f7da42 --- /dev/null +++ b/carts/font.p8 @@ -0,0 +1,88 @@ +pico-8 cartridge // http://www.pico-8.com +version 43 +__lua__ +poke(0x5f55,0x00) +cls(0) +for j=0,7 do + for i=0,31 do + x=i*4 + y=j*8 + c=i+j*32 + if c<32 or c>126 then + fillp(0b1010010010100100) + rectfill(x,y,x+2,y+4,0x70) + fillp(0) + else + if c>64 and c<91 then + c+=32 + elseif c>96 and c<123 then + c-=32 + end + print(chr(c),x,y,7) + end + end +end +poke(0x5f55,0x60) +cstore() +__gfx__ +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000700707070707770707077000700070007007070000000000000000000707770770077707770707077707000777077707770000000000070000070007770 +00000700707077707700007077007000700000700700070000000000000007007070070000700070707070007000007070707070070007000700777007000070 +00000700000070700770070007700000700000707770777000007770000007007070070077700770777077707770007077707770000000007000000000700770 +00000000000077707770700070700000700000700700070007000000000007007070070070000070007000707070007070700070070007000700777007000000 +00000700000070700700707077700000070007007070000070000000070070007770777077707770007077707770007077700070000070000070000070000700 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +07007770777007707700777077700770707077707770707070007770770007707770070077700770777070707070707070707070777077007000077007000000 +70707070707070007070700070007000707007000700707070007770707070707070707070707000070070707070707070707070007070000700007070700000 +70707770770070007070770077007000777007000700770070007070707070707770707077007770070070707070707007007770070070000700007000000000 +70007070707070007070700070007070707007000700707070007070707070707000770070700070070070707770777070700070700070000700007000000000 +07707070777007707770777070007770707077707700707077707070707077007000077070707700070007700700777070707770777077000070077000007770 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +07000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007700700770000007070 +00700770770007707700777077700770707077707770707070007770770007700770070077000770777070707070707070707070777007000700070000700700 +00007070770070007070770077007000707007000700770070007770707070707070707070707000070070707070707007007770007077000700077077707070 +00007770707070007070700070007070777007000700707070007070707070707770770077000070070070707770777007000070700007000700070070000700 +00007070777007707700077070007770707077707700707007707070707077007000077070707700070007700700777070707700777007700700770000007070 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 +07000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700070007000700 +70707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070707070 @@ -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; } diff --git a/src/config.c b/src/config.c index d841739..48d4208 100644 --- a/src/config.c +++ b/src/config.c @@ -13,8 +13,9 @@ #include "path.h" static void usage(char const *name) { - printf("Usage: %s [-c <carts>] [-S <mode>] [-v[v[v]]]\n" + printf("Usage: %s [-c <carts>] [-d] [-S <mode>] [-v[v[v]]]\n" " -c <carts> Carts directory.\n" + " -d Print additional debug statistics.\n" " -S <mode> Scale mode: 'pp' (default), 'fill' or 'stretch'.\n" " -v[v[v]] Increasingly verbose logging.\n", pw_basename(name)); @@ -78,6 +79,21 @@ static bool parse_log_priority(char const *s, SDL_LogPriority *l) { return true; } +static bool parse_bool(char const *s, bool *b) { + struct named_value values[] = { + {"no", false}, {"false", false}, {"0", false}, + {"yes", true}, {"true", true}, {"1", true}, + }; + int v; + if (!parse_named_value(values, SDL_arraysize(values), s, &v)) { + SDL_SetError("invalid boolean flag '%s'", s); + return false; + } + + *b = (bool)v; + return true; +} + static void parse_config_ini(struct pw_config *config) { char ini[1024]; if (!pw_pathjoin(ini, sizeof(ini), config->carts, "config.ini", NULL)) { @@ -109,7 +125,12 @@ static void parse_config_ini(struct pw_config *config) { continue; } - if (!SDL_strcmp(key, "carts")) { + if (!SDL_strcmp(key, "debug")) { + if (!parse_bool(value, &config->debug)) { + SDL_LogWarn(0, "%s", SDL_GetError()); + SDL_ClearError(); + } + } else if (!SDL_strcmp(key, "carts")) { if (config->carts) { SDL_free(config->carts); } @@ -168,7 +189,7 @@ struct pw_config *pw_config_load(int argc, char **argv) { #if defined(_POSIX_VERSION) int c; optind = 1; - while ((c = getopt(argc, argv, ":hc:S:v")) != -1) { + while ((c = getopt(argc, argv, ":hc:dS:v")) != -1) { switch (c) { case 'h': usage(name); @@ -180,6 +201,9 @@ struct pw_config *pw_config_load(int argc, char **argv) { } config->carts = SDL_strdup(optarg); break; + case 'd': + config->debug = true; + break; case 'S': if (!parse_scale_mode(optarg, &config->mode)) { goto cleanup; diff --git a/src/config.h b/src/config.h index 89e8e82..7cc2134 100644 --- a/src/config.h +++ b/src/config.h @@ -10,6 +10,7 @@ * The configuration for PICO-w. */ struct pw_config { + bool debug; char *carts; enum pw_scale_mode mode; SDL_LogPriority logp; diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..dc06c51 --- /dev/null +++ b/src/debug.c @@ -0,0 +1,169 @@ +#include <SDL3/SDL_timer.h> + +#include "debug.h" +#include "png.h" +#include "sysroot.h" + +#define PW_FONT_WIDTH 128 +#define PW_FONT_HEIGHT 128 + +#ifndef PW_FONT_SIZE +#define PW_FONT_SIZE 4. +#endif + +struct pw_debug *pw_debug_init(SDL_Renderer *renderer) { + if (!renderer) { + SDL_InvalidParamError("renderer"); + return NULL; + } + + SDL_Palette *palette = NULL; + SDL_Surface *surface = NULL; + union pw_png_pixel *image = NULL; + SDL_Texture *font = NULL; + struct pw_debug *debug = NULL; + + SDL_Color colors[] = { + {0x00, 0x00, 0x00, 0x00}, + {0xff, 0x00, 0xff, 0xff}, + }; + palette = SDL_CreatePalette(2); + if (!palette || + !SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors))) { + goto error; + } + + surface = + SDL_CreateSurface(PW_FONT_WIDTH, PW_FONT_HEIGHT, SDL_PIXELFORMAT_INDEX8); + if (!surface || !SDL_SetSurfacePalette(surface, palette)) { + goto error; + } + SDL_SetSurfaceColorKey(surface, true, 0); + SDL_DestroyPalette(palette); + palette = NULL; + + image = + pw_png_load(font_png.data, font_png.size, PW_FONT_WIDTH, PW_FONT_HEIGHT); + if (!image) { + goto error; + } + union pw_png_pixel mask = {.ch = {0xff, 0xff, 0xff, 0x00}}; + uint8_t *pixels = surface->pixels; + for (size_t i = 0; i < PW_FONT_WIDTH * PW_FONT_HEIGHT; ++i) { + pixels[i] = !!(image[i].raw & mask.raw); + } + SDL_free(image); + image = NULL; + + font = SDL_CreateTextureFromSurface(renderer, surface); + if (!font || !SDL_SetTextureScaleMode(font, SDL_SCALEMODE_NEAREST)) { + goto error; + } + SDL_DestroySurface(surface); + surface = NULL; + + debug = SDL_calloc(1, sizeof(*debug)); + if (!debug) { + goto error; + } + + debug->font = font; + return debug; + +error: + if (palette) { + SDL_DestroyPalette(palette); + } + if (surface) { + SDL_DestroySurface(surface); + } + if (image) { + SDL_free(image); + } + if (font) { + SDL_DestroyTexture(font); + } + if (debug) { + SDL_free(debug); + } + return NULL; +} + +void pw_debug_free(struct pw_debug *debug) { + if (!debug) { + SDL_InvalidParamError("debug"); + return; + } + + SDL_DestroyTexture(debug->font); + SDL_free(debug); +} + +bool pw_debug_iterate(struct pw_debug *debug) { + if (!debug) { + SDL_InvalidParamError("debug"); + return false; + } + + if (!debug->fps.clock) { + debug->fps.clock = SDL_GetTicks(); + } else if (++debug->fps.count > 10) { + uint64_t count = debug->fps.count; + uint64_t start = debug->fps.clock; + debug->fps.clock = SDL_GetTicks(); + debug->fps.count = 0; + debug->fps.sample = + 1000. * (double)count / (double)(debug->fps.clock - start); + } + + return true; +} + +static bool debug_print(struct pw_debug *debug, float x, float y, + char const *fmt, ...) { + SDL_Renderer *renderer = SDL_GetRendererFromTexture(debug->font); + if (!renderer) { + return false; + } + float sx, sy; + if (!SDL_GetRenderScale(renderer, &sx, &sy)) { + return false; + } + sx *= PW_FONT_SIZE; + sy *= PW_FONT_SIZE; + + va_list ap; + char *text; + va_start(ap, fmt); + int n = SDL_vasprintf(&text, fmt, ap); + va_end(ap); + + if (n < 0) { + return false; + } + + bool ok = true; + SDL_FRect src = {0., 0., 4., 5.}; + SDL_FRect dst = {x * sx, y * sy, src.w * sx, src.h * sy}; + for (int i = 0; i < n; i++) { + uint8_t c = (uint8_t)text[i]; + uint8_t i = c % 32; + uint8_t j = c / 32; + src.x = (float)(i * 4); + src.y = (float)(j * 8); + ok = ok && SDL_RenderTexture(renderer, debug->font, &src, &dst); + dst.x += dst.w; + } + + SDL_free(text); + return true; +} + +bool pw_debug_render(struct pw_debug *debug) { + if (!debug) { + SDL_InvalidParamError("debug"); + return false; + } + + return debug_print(debug, 2, 2, "FPS: %.2f", debug->fps.sample); +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..7b00453 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,35 @@ +#ifndef PW_DEBUG_H +#define PW_DEBUG_H + +#include <SDL3/SDL_render.h> + +struct pw_debug { + SDL_Texture *font; + struct { + uint64_t clock; + uint64_t count; + double sample; + } fps; +}; + +/** + * Initializes debug state. + */ +struct pw_debug *pw_debug_init(SDL_Renderer *renderer); + +/** + * Frees debug state. + */ +void pw_debug_free(struct pw_debug *debug); + +/** + * Iterates a new frame and updates debug information. + */ +bool pw_debug_iterate(struct pw_debug *debug); + +/** + * Prints debug information to screen. + */ +bool pw_debug_render(struct pw_debug *debug); + +#endif @@ -137,7 +137,5 @@ bool pw_fb_render(struct pw_framebuffer *fb) { return false; } - return SDL_RenderClear(renderer) && - SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target) && - SDL_RenderPresent(renderer); + return SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target); } @@ -473,5 +473,7 @@ bool pw_game_iterate(struct pw_game *game) { } } - return pw_fb_render(game->fb); + return true; } + +bool pw_game_render(struct pw_game *game) { return pw_fb_render(game->fb); } @@ -54,4 +54,9 @@ bool pw_game_event(struct pw_game *game, struct pw_players *players, */ bool pw_game_iterate(struct pw_game *game); +/** + * Renders the game state. + */ +bool pw_game_render(struct pw_game *game); + #endif @@ -205,6 +205,10 @@ bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart) { } *cart = NULL; + return true; +} + +bool pw_menu_render(struct pw_menu *menu) { SDL_Texture *png = menu->cart->png; SDL_Renderer *renderer = SDL_GetRendererFromTexture(png); if (!renderer) { @@ -216,7 +220,5 @@ bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart) { return false; } - return SDL_RenderClear(renderer) && - SDL_RenderTexture(renderer, png, NULL, &target) && - SDL_RenderPresent(renderer); + return SDL_RenderTexture(renderer, png, NULL, &target); } @@ -43,4 +43,9 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event); */ bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart); +/** + * Renders the cart selection menu. + */ +bool pw_menu_render(struct pw_menu *menu); + #endif diff --git a/src/picow.c b/src/picow.c index bc26b44..62d829b 100644 --- a/src/picow.c +++ b/src/picow.c @@ -4,6 +4,7 @@ #include "cart.h" #include "config.h" +#include "debug.h" #include "game.h" #include "menu.h" #include "player.h" @@ -15,7 +16,7 @@ static SDL_AppResult failf(char const *fmt, ...) { int n = SDL_vasprintf(&msg, fmt, ap); va_end(ap); - if (n <= 0) { + if (n < 0) { msg = "unspecified failure"; } @@ -26,7 +27,7 @@ static SDL_AppResult failf(char const *fmt, ...) { SDL_LogCritical(0, "%s", msg); } - if (n > 0) { + if (n >= 0) { SDL_free(msg); } @@ -42,6 +43,7 @@ struct pw_appstate { SDL_Window *window; SDL_Renderer *renderer; struct pw_config *config; + struct pw_debug *debug; struct pw_players *players; struct pw_menu *menu; struct pw_game *game; @@ -86,6 +88,13 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { SDL_LogWarn(0, "failed to enable V-sync: %s", SDL_GetError()); } + if (state->config->debug) { + state->debug = pw_debug_init(state->renderer); + if (!state->debug) { + return failf("failed to initialize debug state"); + } + } + state->players = pw_players_init(); if (!state->players) { return failf("failed to initialize player controllers"); @@ -139,6 +148,10 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { SDL_AppResult SDL_AppIterate(void *appstate) { struct pw_appstate *state = appstate; + if (state->debug && !pw_debug_iterate(state->debug)) { + return SDL_APP_FAILURE; + } + if (state->scene == PW_SCENE_CART_SELECTION) { struct pw_multicart *cart; if (!pw_menu_iterate(state->menu, &cart)) { @@ -158,18 +171,42 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } } + bool ok = true; + + ok = ok && SDL_RenderClear(state->renderer); + switch (state->scene) { + case PW_SCENE_CART_SELECTION: + ok = ok && pw_menu_render(state->menu); + break; + case PW_SCENE_GAME: + ok = ok && pw_game_render(state->game); + break; + } + if (state->debug) { + ok = ok && pw_debug_render(state->debug); + } + ok = ok && SDL_RenderPresent(state->renderer); + + if (!ok) { + return SDL_APP_FAILURE; + } return SDL_APP_CONTINUE; } void SDL_AppQuit(void *appstate, SDL_AppResult result) { - struct pw_appstate *state = appstate; - (void)result; + if (result != SDL_APP_SUCCESS) { + SDL_LogCritical(0, "application error: %s", SDL_GetError()); + } + struct pw_appstate *state = appstate; if (!state) { goto bye; } pw_config_free(state->config); + if (state->debug) { + pw_debug_free(state->debug); + } pw_players_free(state->players); pw_menu_free(state->menu); pw_game_free(state->game); diff --git a/src/png.c b/src/png.c new file mode 100644 index 0000000..878d659 --- /dev/null +++ b/src/png.c @@ -0,0 +1,39 @@ +#include <SDL3/SDL_error.h> + +#include "png.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> + +union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) { + int x, y, n; + int ok = stbi_info_from_memory(data, sz, &x, &y, &n); + if (!ok || x != w || y != h) { + SDL_SetError("unexpected PNG dimensions"); + return NULL; + } + + stbi_uc *image = stbi_load_from_memory(data, sz, &x, &y, &n, 4); + if (!image) { + SDL_SetError("malformed 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 x-by-y 32-bit pixels. + return (union pw_png_pixel *)image; +} diff --git a/src/png.h b/src/png.h new file mode 100644 index 0000000..516c4fa --- /dev/null +++ b/src/png.h @@ -0,0 +1,24 @@ +#ifndef PW_PNG_H +#define PW_PNG_H + +#include <SDL3/SDL_stdinc.h> + +/** + * A pixel from a loaded PNG image. + */ +union pw_png_pixel { + uint32_t raw; + struct { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + } ch; +}; + +/** + * Loads a PNG with exact dimensions. + */ +union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h); + +#endif diff --git a/tools/generate-sysroot.c b/tools/generate-sysroot.c index e6d93f8..3663144 100644 --- a/tools/generate-sysroot.c +++ b/tools/generate-sysroot.c @@ -125,6 +125,7 @@ int main(int argc, char *argv[argc + 1]) { {"pico8_wasm", "pico8.wasm"}, {"stub_pico8_wasm", "stub/pico8.wasm"}, {"unknown_p8_png", "unknown.p8.png"}, + {"font_png", "font.png"}, }; for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) { char file[PW_PATH_MAX]; |
