From 0fe10236678c50bd03f30ddac5ee24ce8dc62d28 Mon Sep 17 00:00:00 2001 From: wanp Date: Wed, 25 Sep 2024 19:42:34 -0300 Subject: [PATCH] use SDL stdlib where possible --- include/twn_util.h | 5 +---- src/rendering/twn_gl_15_rendering.c | 6 +++--- src/rendering/twn_rendering.c | 2 +- src/rendering/twn_text.c | 16 +++++++--------- src/twn_audio.c | 6 +++--- src/twn_input.c | 4 ++-- src/twn_loop.c | 7 ++----- src/twn_textures.c | 2 +- src/twn_util.c | 28 +++++++++++++++++----------- 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/include/twn_util.h b/include/twn_util.h index d3fba71..bd5d7b0 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -7,11 +7,8 @@ #include #include -#include #include #include -#include -#include /* */ @@ -31,7 +28,7 @@ TWN_API void log_warn(const char *restrict format, ...); /* for when there's absolutely no way to continue */ -TWN_API noreturn void die_abruptly(void); +TWN_API _Noreturn void die_abruptly(void); /* "critical" allocation functions which will log and abort() on failure. */ diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index 4ae7aad..1da4391 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -176,8 +176,8 @@ void render_circle(const CirclePrimitive *circle) { glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); - free(vertices); - free(indices); + SDL_free(vertices); + SDL_free(indices); } @@ -222,7 +222,7 @@ bool push_to_vertex_buffer_builder(VertexBufferBuilder *builder, if (builder->bytes_left == 0) return false; - memcpy(builder->mapping, bytes, size); + SDL_memcpy(builder->mapping, bytes, size); builder->bytes_left -= size; /* trigger data send */ diff --git a/src/rendering/twn_rendering.c b/src/rendering/twn_rendering.c index e04d485..e88acf3 100644 --- a/src/rendering/twn_rendering.c +++ b/src/rendering/twn_rendering.c @@ -26,7 +26,7 @@ void render_queue_clear(void) { /* if you're gonna remove it, this is also being done in main.c */ for (size_t i = 0; i < arrlenu(ctx.render_queue_2d); ++i) { if (ctx.render_queue_2d[i].type == PRIMITIVE_2D_TEXT) { - free(ctx.render_queue_2d[i].text.text); + SDL_free(ctx.render_queue_2d[i].text.text); } } diff --git a/src/rendering/twn_text.c b/src/rendering/twn_text.c index eefc6cf..21fbb35 100644 --- a/src/rendering/twn_text.c +++ b/src/rendering/twn_text.c @@ -53,16 +53,16 @@ static FontData *text_load_font_data(const char *path, int height_px) { TEXT_FONT_TEXTURE_SIZE, TEXT_FONT_TEXTURE_SIZE ); - free(bitmap); + SDL_free(bitmap); return font_data; } static void text_destroy_font_data(FontData *font_data) { - free(font_data->file_bytes); + SDL_free(font_data->file_bytes); delete_gpu_texture(font_data->texture); - free(font_data); + SDL_free(font_data); } @@ -114,7 +114,7 @@ static void ensure_font_cache(const char *font_path, int height_px) { bool is_cached = false; for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) { FontData *font_data = ctx.text_cache.data[i]; - if ((strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) { + if ((SDL_strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) { is_cached = true; break; } @@ -130,7 +130,7 @@ static FontData *get_font_data(const char *font_path, int height_px) { FontData *font_data = NULL; for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) { font_data = ctx.text_cache.data[i]; - if ((strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) { + if ((SDL_strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) { break; } } @@ -161,11 +161,9 @@ void text_cache_deinit(TextCache *cache) { void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path) { ensure_font_cache(font_path, height_px); - /* the string might not be around by the time it's used, so copy it */ + /* the original string might not be around by the time it's used, so copy it */ /* TODO: arena */ - /* NOTE: can we trust strlen? */ - char *dup_string = cmalloc(strlen(string) + 1); - strcpy(dup_string, string); + char *dup_string = SDL_strdup(string); TextPrimitive text = { .color = color, diff --git a/src/twn_audio.c b/src/twn_audio.c index 8e4493c..2ddfb7f 100644 --- a/src/twn_audio.c +++ b/src/twn_audio.c @@ -65,14 +65,14 @@ void audio_play(const char *path, const char *channel) { static AudioFileType infer_audio_file_type(const char *path) { - size_t path_len = strlen(path); + size_t path_len = SDL_strlen(path); for (int i = 0; i < audio_file_type_count; ++i) { - size_t ext_length = strlen(audio_exts[i]); + size_t ext_length = SDL_strlen(audio_exts[i]); if (path_len <= ext_length) continue; - if (strcmp(&path[path_len - ext_length], audio_exts[i]) == 0) + if (SDL_strcmp(&path[path_len - ext_length], audio_exts[i]) == 0) return (AudioFileType)i; } diff --git a/src/twn_input.c b/src/twn_input.c index 5efb76e..6b0911b 100644 --- a/src/twn_input.c +++ b/src/twn_input.c @@ -108,7 +108,7 @@ static void input_bind_code_to_action(InputState *input, if (action->num_bindings == SDL_arraysize(action->bindings)) { --action->num_bindings; size_t shifted_size = (sizeof action->bindings) - (sizeof action->bindings[0]); - memmove(action->bindings, action->bindings + 1, shifted_size); + SDL_memmove(action->bindings, action->bindings + 1, shifted_size); } action->bindings[action->num_bindings++] = (Button) { @@ -169,7 +169,7 @@ static void input_unbind_code_from_action(InputState *input, /* remove the element to unbind and shift the rest so there isn't a gap */ size_t elements_after_index = action->num_bindings - index; size_t shifted_size = elements_after_index * sizeof action->bindings[0]; - memmove(action->bindings + index, action->bindings + index + 1, shifted_size); + SDL_memmove(action->bindings + index, action->bindings + index + 1, shifted_size); --action->num_bindings; } diff --git a/src/twn_loop.c b/src/twn_loop.c index 824982d..17d355e 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -17,10 +17,7 @@ #include #endif -#include #include -#include -#include #include @@ -380,7 +377,7 @@ static void clean_up(void) { /* if you're gonna remove this, it's also being done in rendering.c */ for (size_t i = 0; i < arrlenu(ctx.render_queue_2d); ++i) { if (ctx.render_queue_2d[i].type == PRIMITIVE_2D_TEXT) { - free(ctx.render_queue_2d[i].text.text); + SDL_free(ctx.render_queue_2d[i].text.text); } } arrfree(ctx.render_queue_2d); @@ -408,7 +405,7 @@ int enter_loop(int argc, char **argv) { return EXIT_FAILURE; for (int i = 1; i < (argc - 1); ++i) { - if (strcmp(argv[i], "--data-dir") == 0) { + if (SDL_strcmp(argv[i], "--data-dir") == 0) { if (!PHYSFS_mount(argv[i+1], NULL, true)) { CRY_PHYSFS("Data dir mount override failed."); return EXIT_FAILURE; diff --git a/src/twn_textures.c b/src/twn_textures.c index f9325c0..9c26c36 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -308,7 +308,7 @@ void textures_dump_atlases(TextureCache *cache) { size_t i = 0; for (; i < arrlenu(cache->atlas_surfaces); ++i) { - snprintf(buf, sizeof buf, string_template, i); + SDL_snprintf(buf, sizeof buf, string_template, i); SDL_RWops *handle = PHYSFSRWOPS_openWrite(buf); diff --git a/src/twn_util.c b/src/twn_util.c index 20821e7..10f14fc 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -5,17 +5,23 @@ #include #define STB_DS_IMPLEMENTATION #define STBDS_ASSERT SDL_assert +#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size)) +#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr)) #include #define STB_RECT_PACK_IMPLEMENTATION +#define STBRP_SORT SDL_qsort #define STBRP_ASSERT SDL_assert #include #define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_malloc(x,u) ((void)(u), SDL_malloc(x)) +#define STBTT_free(x,u) ((void)(u), SDL_free(x)) +#define STBTT_assert(x) SDL_assert(x) +#define STBTT_strlen(x) SDL_strlen(x) +#define STBTT_memcpy SDL_memcpy +#define STBTT_memset SDL_memset #include -#include #include -#include -#include void cry_impl(const char *file, const int line, const char *title, const char *text) { @@ -57,7 +63,7 @@ void log_warn(const char *restrict format, ...) { } -noreturn static void alloc_failure_death(void) { +_Noreturn static void alloc_failure_death(void) { log_critical("Allocation failure. Aborting NOW."); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "MEMORY ALLOCATION FAILURE.", @@ -70,7 +76,7 @@ noreturn static void alloc_failure_death(void) { } -noreturn void die_abruptly(void) { +_Noreturn void die_abruptly(void) { /* a zombie window will linger if we don't at least try to quit SDL */ SDL_Quit(); abort(); @@ -78,7 +84,7 @@ noreturn void die_abruptly(void) { void *cmalloc(size_t size) { - void *ptr = malloc(size); + void *ptr = SDL_malloc(size); if (ptr == NULL) alloc_failure_death(); return ptr; @@ -86,7 +92,7 @@ void *cmalloc(size_t size) { void *crealloc(void *ptr, size_t size) { - void *out = realloc(ptr, size); + void *out = SDL_realloc(ptr, size); if (out == NULL) alloc_failure_death(); return out; @@ -94,7 +100,7 @@ void *crealloc(void *ptr, size_t size) { void *ccalloc(size_t num, size_t size) { - void *ptr = calloc(num, size); + void *ptr = SDL_calloc(num, size); if (ptr == NULL) alloc_failure_death(); return ptr; @@ -161,13 +167,13 @@ char *file_to_str(const char *path) { bool strends(const char *str, const char *suffix) { - size_t str_length = strlen(str); - size_t suffix_length = strlen(suffix); + size_t str_length = SDL_strlen(str); + size_t suffix_length = SDL_strlen(suffix); if (suffix_length > str_length) return false; - return memcmp((str + str_length) - suffix_length, suffix, suffix_length) == 0; + return SDL_memcmp((str + str_length) - suffix_length, suffix, suffix_length) == 0; }