diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-10 22:51:12 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-10 22:51:12 +0200 |
| commit | 898291f9d5e447a85d36bef885896498f01acb20 (patch) | |
| tree | 28ef5af7861ef6dc1d5d15250e38c208d54c4c25 | |
| parent | eb12c5386f4ea75f4a715b50d73eb8bc74b12815 (diff) | |
Support Different Scaling Modes
We now support pixel perfect scaling (what existed before) as well
as new scaling modes:
- `fill`: Fill fit to output, but keep aspect ratio
- `stretch`: Fill every pixel in the output, may look OK on certain
displays that are close to 1:1 aspect ratio
We also fixed a small mouse position computation bug (we would jump
to the next pixel too early).
| -rw-r--r-- | src/fb.c | 34 | ||||
| -rw-r--r-- | src/fb.h | 6 | ||||
| -rw-r--r-- | src/game.c | 4 | ||||
| -rw-r--r-- | src/game.h | 3 | ||||
| -rw-r--r-- | src/menu.c | 43 | ||||
| -rw-r--r-- | src/menu.h | 5 | ||||
| -rw-r--r-- | src/picow.c | 25 | ||||
| -rw-r--r-- | src/scale.c | 109 | ||||
| -rw-r--r-- | src/scale.h | 20 |
9 files changed, 186 insertions, 63 deletions
@@ -1,6 +1,7 @@ #include "fb.h" -struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) { +struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer, + enum pw_scale_mode mode) { if (!renderer) { SDL_InvalidParamError("renderer"); return NULL; @@ -14,7 +15,7 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) { texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT); - if (!texture || !SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST)) { + if (!texture) { goto error; } @@ -23,6 +24,7 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) { goto error; } + fb->mode = mode; fb->texture = texture; SDL_zero(fb->target); if (!pw_fb_iterate(fb)) { @@ -57,37 +59,13 @@ bool pw_fb_iterate(struct pw_framebuffer *fb) { return false; } - SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture); - if (!renderer) { - return false; - } - - int rw, rh; - if (!SDL_GetRenderOutputSize(renderer, &rw, &rh)) { - return false; - } - - int scale = SDL_min(rw / PW_FB_WIDTH, rh / PW_FB_HEIGHT); - if (!scale) { - return false; - } - - int w = scale * PW_FB_WIDTH; - int h = scale * PW_FB_HEIGHT; - int x = (rw - w) / 2; - int y = (rh - h) / 2; - - fb->target.w = (float)w; - fb->target.h = (float)h; - fb->target.x = (float)x; - fb->target.y = (float)y; - return true; + return pw_scale(fb->mode, fb->texture, &fb->target); } static uint8_t project_component(float r, float start, float size, uint8_t dim) { float dimf = (float)dim; - float df = SDL_roundf(((r - start) * dimf) / size); + float df = SDL_floorf(((r - start) * dimf) / size); return (uint8_t)SDL_clamp(df, 0.0, (float)(dim - 1)); } @@ -3,6 +3,8 @@ #include <SDL3/SDL_render.h> +#include "scale.h" + #define PW_FB_WIDTH 128 #define PW_FB_HEIGHT 128 @@ -10,6 +12,7 @@ * A PICO-8 framebuffer. */ struct pw_framebuffer { + enum pw_scale_mode mode; SDL_Texture *texture; SDL_FRect target; }; @@ -25,7 +28,8 @@ struct pw_coordinates { /** * Allocates and initializes the PICO-8 framebuffer. */ -struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer); +struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer, + enum pw_scale_mode mode); /** * Cleanup and free the PICO-8 framebuffer. @@ -29,7 +29,7 @@ static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) { (void)simulate_infinite_loop; } -struct pw_game *pw_game_init(SDL_Renderer *renderer) { +struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode) { if (!renderer) { SDL_InvalidParamError("renderer"); return NULL; @@ -42,7 +42,7 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer) { } SDL_zerop(game); - game->fb = pw_fb_init(renderer); + game->fb = pw_fb_init(renderer, mode); if (!game->fb) { goto error; } @@ -10,6 +10,7 @@ #include "fb.h" #include "input.h" #include "player.h" +#include "scale.h" /** * Executing PICO-8 game state. @@ -32,7 +33,7 @@ struct pw_game { /** * Allocates and initializes the game state. */ -struct pw_game *pw_game_init(SDL_Renderer *renderer); +struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode); /** * Frees the game state. @@ -3,7 +3,8 @@ #include "input.h" #include "menu.h" -struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts) { +struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, + enum pw_scale_mode mode) { if (!renderer) { SDL_InvalidParamError("renderer"); return NULL; @@ -35,6 +36,17 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts) { goto error; } + switch (mode) { + case PW_SCALE_MODE_STRETCH: + // Stretching the cart PNGs in the menu looks silly, so in case PICO-w is + // running in stretch mode, then use fill mode for the cart selection. + menu->mode = PW_SCALE_MODE_FILL; + break; + default: + menu->mode = mode; + break; + } + return menu; error: @@ -164,29 +176,6 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { return true; } -static bool center_rect(SDL_Renderer *renderer, SDL_FRect *target) { - int rw, rh; - if (!SDL_GetRenderOutputSize(renderer, &rw, &rh)) { - return false; - } - - int scale = SDL_min(rw / PW_PNG_WIDTH, rh / PW_PNG_HEIGHT); - if (!scale) { - return false; - } - - int w = scale * PW_PNG_WIDTH; - int h = scale * PW_PNG_HEIGHT; - int x = (rw - w) / 2; - int y = (rh - h) / 2; - - target->w = (float)w; - target->h = (float)h; - target->x = (float)x; - target->y = (float)y; - return true; -} - bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart) { if (!menu) { SDL_InvalidParamError("menu"); @@ -218,8 +207,12 @@ bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart) { SDL_Texture *png = menu->cart->png; SDL_Renderer *renderer = SDL_GetRendererFromTexture(png); + if (!renderer) { + return false; + } + SDL_FRect target; - if (!renderer || !center_rect(renderer, &target)) { + if (!pw_scale(menu->mode, png, &target)) { return false; } @@ -5,6 +5,7 @@ #include <SDL3/SDL_render.h> #include "cart.h" +#include "scale.h" /** * The cart selection menu state. @@ -12,6 +13,7 @@ struct pw_menu { struct pw_carts *carts; struct pw_cart *cart; + enum pw_scale_mode mode; struct { size_t current; size_t next; @@ -22,7 +24,8 @@ struct pw_menu { /** * Allocates and initializes the cart selection menu state. */ -struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts); +struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, + enum pw_scale_mode mode); /** * Frees the cart selection menu. diff --git a/src/picow.c b/src/picow.c index 09c3953..9b89447 100644 --- a/src/picow.c +++ b/src/picow.c @@ -10,10 +10,12 @@ #include "menu.h" #include "path.h" #include "player.h" +#include "scale.h" static void usage(char const *name) { - printf("Usage: %s [-c <carts>] [-v[v[v]]]\n" + printf("Usage: %s [-c <carts>] [-S <mode>] [-v[v[v]]]\n" " -c <carts> Carts directory. Defaults to 'carts'.\n" + " -S <mode> Scale mode: 'pp' (default), 'fill' or 'stretch'.\n" " -v[v[v]] Increasingly verbose logging.\n", pw_basename(name)); } @@ -69,10 +71,11 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { } char *carts = "carts"; + enum pw_scale_mode mode = PW_SCALE_MODE_PIXEL_PERFECT; SDL_LogPriority logp = SDL_GetLogPriority(0); int c; - while ((c = getopt(argc, argv, "hc:v")) != -1) { + while ((c = getopt(argc, argv, "hc:S:v")) != -1) { switch (c) { case 'h': usage(argv[0]); @@ -80,10 +83,22 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { case 'c': carts = optarg; break; + case 'S': + if (!SDL_strcmp(optarg, "pp")) { + mode = PW_SCALE_MODE_PIXEL_PERFECT; + } else if (!SDL_strcmp(optarg, "fill")) { + mode = PW_SCALE_MODE_FILL; + } else if (!SDL_strcmp(optarg, "stretch")) { + mode = PW_SCALE_MODE_STRETCH; + } else { + fprintf(stderr, "ERROR: invalid scaling mode '%s'\n", optarg); + return SDL_APP_FAILURE; + } + break; case 'v': logp--; if (logp == SDL_LOG_PRIORITY_INVALID) { - fprintf(stderr, "ERROR: specified -v too many times\n"); // NOLINT + fprintf(stderr, "ERROR: specified -v too many times\n"); return SDL_APP_FAILURE; } break; @@ -109,12 +124,12 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return failf("failed to initialize player controllers"); } - state->menu = pw_menu_init(state->renderer, carts); + state->menu = pw_menu_init(state->renderer, carts, mode); if (!state->menu) { return failf("failed to initialize cart selection state"); } - state->game = pw_game_init(state->renderer); + state->game = pw_game_init(state->renderer, mode); if (!state->game) { return failf("failed to initialize PICO-8 game state"); } diff --git a/src/scale.c b/src/scale.c new file mode 100644 index 0000000..da35e63 --- /dev/null +++ b/src/scale.c @@ -0,0 +1,109 @@ +#include "scale.h" +#include "SDL3/SDL_error.h" +#include "SDL3/SDL_render.h" +#include "SDL3/SDL_stdinc.h" + +static SDL_ScaleMode scale_pixel_perfect(float twf, float thf, int rw, int rh, + SDL_FRect *target) { + int tw = (int)twf; + int th = (int)thf; + int scale = SDL_min(rw / tw, rh / th); + if (!scale) { + return SDL_SCALEMODE_INVALID; + } + + int w = scale * tw; + int h = scale * th; + int x = (rw - w) / 2; + int y = (rh - h) / 2; + + target->w = (float)w; + target->h = (float)h; + target->x = (float)x; + target->y = (float)y; + + return SDL_SCALEMODE_NEAREST; +} + +static SDL_ScaleMode scale_fill(float twf, float thf, int rw, int rh, + SDL_FRect *target) { + float rwf = (float)rw; + float rhf = (float)rh; + float scale = SDL_min(rwf / twf, rhf / thf); + + float w = scale * twf; + float h = scale * thf; + float x = (rw - w) / 2.; + float y = (rh - h) / 2.; + + target->w = w; + target->h = h; + target->x = x; + target->y = y; + + return SDL_SCALEMODE_PIXELART; +} + +static SDL_ScaleMode scale_stretch(float twf, float thf, int rw, int rh, + SDL_FRect *target) { + (void)twf; + (void)thf; + + target->w = rw; + target->h = rh; + target->x = 0.; + target->y = 0.; + + return SDL_SCALEMODE_PIXELART; +} + +bool pw_scale(enum pw_scale_mode mode, SDL_Texture *texture, + SDL_FRect *target) { + SDL_ScaleMode (*scalefn)(float, float, int, int, SDL_FRect *); + switch (mode) { + case PW_SCALE_MODE_PIXEL_PERFECT: + scalefn = scale_pixel_perfect; + break; + case PW_SCALE_MODE_FILL: + scalefn = scale_fill; + break; + case PW_SCALE_MODE_STRETCH: + scalefn = scale_stretch; + break; + default: + SDL_InvalidParamError("mode"); + return false; + } + if (!texture) { + SDL_InvalidParamError("texture"); + return false; + } + if (!target) { + SDL_InvalidParamError("target"); + return false; + } + + float twf, thf; + if (!SDL_GetTextureSize(texture, &twf, &thf)) { + return false; + } + + SDL_Renderer *renderer = SDL_GetRendererFromTexture(texture); + if (!renderer) { + return false; + } + + int rw, rh; + if (!SDL_GetRenderOutputSize(renderer, &rw, &rh)) { + return false; + } + + SDL_FRect t; + SDL_ScaleMode m = scalefn(twf, thf, rw, rh, &t); + if (m == SDL_SCALEMODE_INVALID || !SDL_SetTextureScaleMode(texture, m)) { + return false; + } + + SDL_memcpy(target, &t, sizeof(*target)); + return true; +} diff --git a/src/scale.h b/src/scale.h new file mode 100644 index 0000000..6294905 --- /dev/null +++ b/src/scale.h @@ -0,0 +1,20 @@ +#ifndef PW_SCALE_H +#define PW_SCALE_H + +#include <SDL3/SDL_render.h> + +/** + * Scaling mode to use for textures. + */ +enum pw_scale_mode { + PW_SCALE_MODE_PIXEL_PERFECT, + PW_SCALE_MODE_FILL, + PW_SCALE_MODE_STRETCH, +}; + +/** + * Scale's a texture to its renderer output. + */ +bool pw_scale(enum pw_scale_mode mode, SDL_Texture *texture, SDL_FRect *target); + +#endif |
