use SDL stdlib where possible

This commit is contained in:
wanp 2024-09-25 19:42:34 -03:00
parent ccad21ab90
commit 0fe1023667
9 changed files with 37 additions and 39 deletions

View File

@ -7,11 +7,8 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <physfs.h> #include <physfs.h>
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <math.h>
#include <stdnoreturn.h>
/* */ /* */
@ -31,7 +28,7 @@ TWN_API void log_warn(const char *restrict format, ...);
/* for when there's absolutely no way to continue */ /* 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. */ /* "critical" allocation functions which will log and abort() on failure. */

View File

@ -176,8 +176,8 @@ void render_circle(const CirclePrimitive *circle) {
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
free(vertices); SDL_free(vertices);
free(indices); SDL_free(indices);
} }
@ -222,7 +222,7 @@ bool push_to_vertex_buffer_builder(VertexBufferBuilder *builder,
if (builder->bytes_left == 0) if (builder->bytes_left == 0)
return false; return false;
memcpy(builder->mapping, bytes, size); SDL_memcpy(builder->mapping, bytes, size);
builder->bytes_left -= size; builder->bytes_left -= size;
/* trigger data send */ /* trigger data send */

View File

@ -26,7 +26,7 @@ void render_queue_clear(void) {
/* if you're gonna remove it, this is also being done in main.c */ /* 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) { for (size_t i = 0; i < arrlenu(ctx.render_queue_2d); ++i) {
if (ctx.render_queue_2d[i].type == PRIMITIVE_2D_TEXT) { 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);
} }
} }

View File

@ -53,16 +53,16 @@ static FontData *text_load_font_data(const char *path, int height_px) {
TEXT_FONT_TEXTURE_SIZE, TEXT_FONT_TEXTURE_SIZE,
TEXT_FONT_TEXTURE_SIZE TEXT_FONT_TEXTURE_SIZE
); );
free(bitmap); SDL_free(bitmap);
return font_data; return font_data;
} }
static void text_destroy_font_data(FontData *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); 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; bool is_cached = false;
for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) { for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) {
FontData *font_data = 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; is_cached = true;
break; break;
} }
@ -130,7 +130,7 @@ static FontData *get_font_data(const char *font_path, int height_px) {
FontData *font_data = NULL; FontData *font_data = NULL;
for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) { for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) {
font_data = 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; 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) { void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path) {
ensure_font_cache(font_path, height_px); 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 */ /* TODO: arena */
/* NOTE: can we trust strlen? */ char *dup_string = SDL_strdup(string);
char *dup_string = cmalloc(strlen(string) + 1);
strcpy(dup_string, string);
TextPrimitive text = { TextPrimitive text = {
.color = color, .color = color,

View File

@ -65,14 +65,14 @@ void audio_play(const char *path, const char *channel) {
static AudioFileType infer_audio_file_type(const char *path) { 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) { 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) if (path_len <= ext_length)
continue; 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; return (AudioFileType)i;
} }

View File

@ -108,7 +108,7 @@ static void input_bind_code_to_action(InputState *input,
if (action->num_bindings == SDL_arraysize(action->bindings)) { if (action->num_bindings == SDL_arraysize(action->bindings)) {
--action->num_bindings; --action->num_bindings;
size_t shifted_size = (sizeof action->bindings) - (sizeof action->bindings[0]); 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) { 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 */ /* 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 elements_after_index = action->num_bindings - index;
size_t shifted_size = elements_after_index * sizeof action->bindings[0]; 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; --action->num_bindings;
} }

View File

@ -17,10 +17,7 @@
#include <glad/glad.h> #include <glad/glad.h>
#endif #endif
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <tgmath.h>
#include <limits.h> #include <limits.h>
@ -380,7 +377,7 @@ static void clean_up(void) {
/* if you're gonna remove this, it's also being done in rendering.c */ /* 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) { for (size_t i = 0; i < arrlenu(ctx.render_queue_2d); ++i) {
if (ctx.render_queue_2d[i].type == PRIMITIVE_2D_TEXT) { 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); arrfree(ctx.render_queue_2d);
@ -408,7 +405,7 @@ int enter_loop(int argc, char **argv) {
return EXIT_FAILURE; return EXIT_FAILURE;
for (int i = 1; i < (argc - 1); ++i) { 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)) { if (!PHYSFS_mount(argv[i+1], NULL, true)) {
CRY_PHYSFS("Data dir mount override failed."); CRY_PHYSFS("Data dir mount override failed.");
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -308,7 +308,7 @@ void textures_dump_atlases(TextureCache *cache) {
size_t i = 0; size_t i = 0;
for (; i < arrlenu(cache->atlas_surfaces); ++i) { 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); SDL_RWops *handle = PHYSFSRWOPS_openWrite(buf);

View File

@ -5,17 +5,23 @@
#include <physfsrwops.h> #include <physfsrwops.h>
#define STB_DS_IMPLEMENTATION #define STB_DS_IMPLEMENTATION
#define STBDS_ASSERT SDL_assert #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 <stb_ds.h> #include <stb_ds.h>
#define STB_RECT_PACK_IMPLEMENTATION #define STB_RECT_PACK_IMPLEMENTATION
#define STBRP_SORT SDL_qsort
#define STBRP_ASSERT SDL_assert #define STBRP_ASSERT SDL_assert
#include <stb_rect_pack.h> #include <stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION #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 <stb_truetype.h> #include <stb_truetype.h>
#include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdnoreturn.h>
#include <string.h>
void cry_impl(const char *file, const int line, const char *title, const char *text) { 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."); log_critical("Allocation failure. Aborting NOW.");
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"MEMORY ALLOCATION FAILURE.", "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 */ /* a zombie window will linger if we don't at least try to quit SDL */
SDL_Quit(); SDL_Quit();
abort(); abort();
@ -78,7 +84,7 @@ noreturn void die_abruptly(void) {
void *cmalloc(size_t size) { void *cmalloc(size_t size) {
void *ptr = malloc(size); void *ptr = SDL_malloc(size);
if (ptr == NULL) if (ptr == NULL)
alloc_failure_death(); alloc_failure_death();
return ptr; return ptr;
@ -86,7 +92,7 @@ void *cmalloc(size_t size) {
void *crealloc(void *ptr, size_t size) { void *crealloc(void *ptr, size_t size) {
void *out = realloc(ptr, size); void *out = SDL_realloc(ptr, size);
if (out == NULL) if (out == NULL)
alloc_failure_death(); alloc_failure_death();
return out; return out;
@ -94,7 +100,7 @@ void *crealloc(void *ptr, size_t size) {
void *ccalloc(size_t num, 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) if (ptr == NULL)
alloc_failure_death(); alloc_failure_death();
return ptr; return ptr;
@ -161,13 +167,13 @@ char *file_to_str(const char *path) {
bool strends(const char *str, const char *suffix) { bool strends(const char *str, const char *suffix) {
size_t str_length = strlen(str); size_t str_length = SDL_strlen(str);
size_t suffix_length = strlen(suffix); size_t suffix_length = SDL_strlen(suffix);
if (suffix_length > str_length) if (suffix_length > str_length)
return false; 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;
} }