From f4b52b5450796ad5dcf8e8dbdb7227bd199a2222 Mon Sep 17 00:00:00 2001 From: wanp Date: Thu, 26 Sep 2024 21:32:08 -0300 Subject: [PATCH] make textures_dump_atlases work again and expose it as a utility function --- apps/platformer/game.c | 7 +++++++ apps/scenery/game.c | 7 +++++++ include/twn_util.h | 4 ++++ src/twn_textures.c | 25 ------------------------- src/twn_textures_c.h | 3 --- src/twn_util.c | 24 ++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/apps/platformer/game.c b/apps/platformer/game.c index b56507e..f7fd7e0 100644 --- a/apps/platformer/game.c +++ b/apps/platformer/game.c @@ -23,6 +23,9 @@ void game_tick(void) { input_add_action(&ctx.input, "debug_toggle"); input_bind_action_scancode(&ctx.input, "debug_toggle", SDL_SCANCODE_BACKSPACE); + input_add_action(&ctx.input, "debug_dump_atlases"); + input_bind_action_scancode(&ctx.input, "debug_dump_atlases", SDL_SCANCODE_HOME); + input_add_action(&ctx.input, "player_left"); input_bind_action_scancode(&ctx.input, "player_left", SDL_SCANCODE_A); @@ -54,6 +57,10 @@ void game_tick(void) { ctx.debug = !ctx.debug; } + if (input_is_action_just_pressed(&ctx.input, "debug_dump_atlases")) { + textures_dump_atlases(); + } + state->scene->tick(state); /* there's a scene switch pending, we can do it now that the tick is done */ diff --git a/apps/scenery/game.c b/apps/scenery/game.c index b56507e..f7fd7e0 100644 --- a/apps/scenery/game.c +++ b/apps/scenery/game.c @@ -23,6 +23,9 @@ void game_tick(void) { input_add_action(&ctx.input, "debug_toggle"); input_bind_action_scancode(&ctx.input, "debug_toggle", SDL_SCANCODE_BACKSPACE); + input_add_action(&ctx.input, "debug_dump_atlases"); + input_bind_action_scancode(&ctx.input, "debug_dump_atlases", SDL_SCANCODE_HOME); + input_add_action(&ctx.input, "player_left"); input_bind_action_scancode(&ctx.input, "player_left", SDL_SCANCODE_A); @@ -54,6 +57,10 @@ void game_tick(void) { ctx.debug = !ctx.debug; } + if (input_is_action_just_pressed(&ctx.input, "debug_dump_atlases")) { + textures_dump_atlases(); + } + state->scene->tick(state); /* there's a scene switch pending, we can do it now that the tick is done */ diff --git a/include/twn_util.h b/include/twn_util.h index bd5d7b0..054f42c 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -68,6 +68,10 @@ TWN_API int64_t file_to_bytes(const char *path, unsigned char **buf_out); TWN_API char *file_to_str(const char *path); +/* saves all texture atlases as BMP files in the write directory */ +TWN_API void textures_dump_atlases(void); + + /* returns true if str ends with suffix */ TWN_API TWN_API bool strends(const char *str, const char *suffix); diff --git a/src/twn_textures.c b/src/twn_textures.c index 4dfbd5d..83b40a1 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -300,31 +300,6 @@ void textures_cache_deinit(TextureCache *cache) { } -void textures_dump_atlases(TextureCache *cache) { - PHYSFS_mkdir("/dump"); - - const char string_template[] = "/dump/atlas%zd.png"; - char buf[2048]; /* larger than will ever be necessary */ - - size_t i = 0; - for (; i < arrlenu(cache->atlas_surfaces); ++i) { - SDL_snprintf(buf, sizeof buf, string_template, i); - - SDL_RWops *handle = PHYSFSRWOPS_openWrite(buf); - - if (handle == NULL) { - CRY("Texture atlas dump failed.", "File could not be opened"); - return; - } - - /* TODO: */ - // IMG_SavePNG_RW(cache->atlas_surfaces[i], handle, true); - CRY("Unimplemented", "textures_dump_atlases dumping is not there, sorry"); - log_info("Dumped atlas %s", buf); - } -} - - static enum TextureMode infer_texture_mode(SDL_Surface *surface) { const uint32_t amask = surface->format->Amask; if (amask == 0) diff --git a/src/twn_textures_c.h b/src/twn_textures_c.h index c3e2e63..e5781df 100644 --- a/src/twn_textures_c.h +++ b/src/twn_textures_c.h @@ -50,9 +50,6 @@ typedef struct TextureKey { uint16_t id; } TextureKey; void textures_cache_init(struct TextureCache *cache, SDL_Window *window); void textures_cache_deinit(struct TextureCache *cache); -/* for debugging */ -void textures_dump_atlases(struct TextureCache *cache); - /* loads an image if it isn't in the cache, otherwise a no-op. */ /* can be called from anywhere at any time after init, useful if you want to */ /* preload textures you know will definitely be used */ diff --git a/src/twn_util.c b/src/twn_util.c index 8f79d1d..f3e32a8 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -165,6 +165,30 @@ char *file_to_str(const char *path) { } +void textures_dump_atlases(void) { + PHYSFS_mkdir("/dump"); + + /* TODO: png instead of bmp */ + const char string_template[] = "/dump/atlas%zd.bmp"; + + size_t i = 0; + for (; i < arrlenu(ctx.texture_cache.atlas_surfaces); ++i) { + char *buf = NULL; + SDL_asprintf(&buf, string_template, i); + + SDL_RWops *handle = PHYSFSRWOPS_openWrite(buf); + + if (handle == NULL) { + CRY("Texture atlas dump failed.", "File could not be opened"); + return; + } + + SDL_SaveBMP_RW(ctx.texture_cache.atlas_surfaces[i], handle, true); + log_info("Dumped atlas %zu to %s", i, buf); + } +} + + bool strends(const char *str, const char *suffix) { size_t str_length = SDL_strlen(str); size_t suffix_length = SDL_strlen(suffix);