use SDL stdlib where possible
This commit is contained in:
parent
ccad21ab90
commit
0fe1023667
@ -7,11 +7,8 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <physfs.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.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 */
|
||||
TWN_API noreturn void die_abruptly(void);
|
||||
TWN_API _Noreturn void die_abruptly(void);
|
||||
|
||||
|
||||
/* "critical" allocation functions which will log and abort() on failure. */
|
||||
|
@ -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 */
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -17,10 +17,7 @@
|
||||
#include <glad/glad.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <tgmath.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 */
|
||||
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;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -5,17 +5,23 @@
|
||||
#include <physfsrwops.h>
|
||||
#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 <stb_ds.h>
|
||||
#define STB_RECT_PACK_IMPLEMENTATION
|
||||
#define STBRP_SORT SDL_qsort
|
||||
#define STBRP_ASSERT SDL_assert
|
||||
#include <stb_rect_pack.h>
|
||||
#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 <stdlib.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) {
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user