2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_util.h"
|
2024-09-27 14:50:24 +00:00
|
|
|
#include "twn_util_c.h"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_engine_context_c.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <physfsrwops.h>
|
|
|
|
#define STB_DS_IMPLEMENTATION
|
|
|
|
#define STBDS_ASSERT SDL_assert
|
2024-09-25 22:42:34 +00:00
|
|
|
#define STBDS_REALLOC(context,ptr,size) ((void)(context), SDL_realloc(ptr, size))
|
|
|
|
#define STBDS_FREE(context,ptr) ((void)(context), SDL_free(ptr))
|
2024-07-08 00:44:20 +00:00
|
|
|
#include <stb_ds.h>
|
|
|
|
#define STB_RECT_PACK_IMPLEMENTATION
|
2024-09-25 22:42:34 +00:00
|
|
|
#define STBRP_SORT SDL_qsort
|
2024-07-08 00:44:20 +00:00
|
|
|
#define STBRP_ASSERT SDL_assert
|
|
|
|
#include <stb_rect_pack.h>
|
2024-08-23 02:41:52 +00:00
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
2024-09-25 22:42:34 +00:00
|
|
|
#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
|
2024-08-23 02:41:52 +00:00
|
|
|
#include <stb_truetype.h>
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
|
|
|
|
void cry_impl(const char *file, const int line, const char *title, const char *text) {
|
|
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"TEARS AT %s:%d: %s ... %s", file, line, title, text);
|
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, text, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void log_impl(const char *restrict format, va_list args, SDL_LogPriority priority) {
|
|
|
|
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
priority,
|
|
|
|
format,
|
|
|
|
args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_info(const char *restrict format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
log_impl(format, args, SDL_LOG_PRIORITY_INFO);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_critical(const char *restrict format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
log_impl(format, args, SDL_LOG_PRIORITY_CRITICAL);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_warn(const char *restrict format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
log_impl(format, args, SDL_LOG_PRIORITY_WARN);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-25 22:42:34 +00:00
|
|
|
_Noreturn static void alloc_failure_death(void) {
|
2024-07-08 00:44:20 +00:00
|
|
|
log_critical("Allocation failure. Aborting NOW.");
|
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2024-09-26 22:37:46 +00:00
|
|
|
"MEMORY ALLOCATION FAILURE",
|
|
|
|
"CRITICAL MEMORY ALLOCATION FAILED. "
|
|
|
|
"EXITING IMMEDIATELY!",
|
2024-07-08 00:44:20 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
die_abruptly();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-25 22:42:34 +00:00
|
|
|
_Noreturn void die_abruptly(void) {
|
2024-07-08 00:44:20 +00:00
|
|
|
/* a zombie window will linger if we don't at least try to quit SDL */
|
|
|
|
SDL_Quit();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *cmalloc(size_t size) {
|
2024-09-25 22:42:34 +00:00
|
|
|
void *ptr = SDL_malloc(size);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
alloc_failure_death();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *crealloc(void *ptr, size_t size) {
|
2024-09-25 22:42:34 +00:00
|
|
|
void *out = SDL_realloc(ptr, size);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (out == NULL)
|
|
|
|
alloc_failure_death();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *ccalloc(size_t num, size_t size) {
|
2024-09-25 22:42:34 +00:00
|
|
|
void *ptr = SDL_calloc(num, size);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
alloc_failure_death();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
double clamp(double d, double min, double max) {
|
|
|
|
const double t = d < min ? min : d;
|
|
|
|
return t > max ? max : t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float clampf(float f, float min, float max) {
|
|
|
|
const float t = f < min ? min : f;
|
|
|
|
return t > max ? max : t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int clampi(int i, int min, int max) {
|
|
|
|
const int t = i < min ? min : i;
|
|
|
|
return t > max ? max : t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-08 06:46:12 +00:00
|
|
|
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
|
2024-07-08 00:44:20 +00:00
|
|
|
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
|
|
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t data_size = SDL_RWseek(handle, 0, RW_SEEK_END);
|
|
|
|
SDL_RWseek(handle, 0, RW_SEEK_SET); /* reset offset into data */
|
|
|
|
|
|
|
|
*buf_out = cmalloc(data_size);
|
|
|
|
SDL_RWread(handle, *buf_out, sizeof **buf_out, data_size / sizeof **buf_out);
|
|
|
|
SDL_RWclose(handle); /* we got all we needed from the stream */
|
|
|
|
|
|
|
|
return data_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-08 06:46:12 +00:00
|
|
|
char *file_to_str(const char *path) {
|
2024-07-08 00:44:20 +00:00
|
|
|
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
|
|
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t data_size = SDL_RWseek(handle, 0, RW_SEEK_END);
|
|
|
|
SDL_RWseek(handle, 0, RW_SEEK_SET); /* reset offset into data */
|
|
|
|
|
|
|
|
char *str_out = cmalloc(data_size + 1); /* data plus final null */
|
|
|
|
size_t len = data_size / sizeof *str_out;
|
|
|
|
|
|
|
|
SDL_RWread(handle, str_out, sizeof *str_out, len);
|
|
|
|
SDL_RWclose(handle); /* we got all we needed from the stream */
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
str_out[len] = '\0';
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
return str_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-27 00:32:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
bool strends(const char *str, const char *suffix) {
|
2024-09-25 22:42:34 +00:00
|
|
|
size_t str_length = SDL_strlen(str);
|
|
|
|
size_t suffix_length = SDL_strlen(suffix);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
if (suffix_length > str_length)
|
|
|
|
return false;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-09-25 22:42:34 +00:00
|
|
|
return SDL_memcmp((str + str_length) - suffix_length, suffix, suffix_length) == 0;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool overlap_rect(const Recti *a, const Recti *b, Recti *result) {
|
2024-07-08 00:44:20 +00:00
|
|
|
SDL_Rect a_sdl = { a->x, a->y, a->w, a->h };
|
|
|
|
SDL_Rect b_sdl = { b->x, b->y, b->w, b->h };
|
|
|
|
SDL_Rect result_sdl = { 0 };
|
|
|
|
|
|
|
|
bool intersection = SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl);
|
|
|
|
|
2024-09-20 16:39:37 +00:00
|
|
|
if (result != NULL)
|
2024-09-23 17:43:16 +00:00
|
|
|
*result = (Recti){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
|
2024-09-20 16:39:37 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool overlap_frect(const Rect *a, const Rect *b, Rect *result) {
|
2024-07-08 00:44:20 +00:00
|
|
|
SDL_FRect a_sdl = { a->x, a->y, a->w, a->h };
|
|
|
|
SDL_FRect b_sdl = { b->x, b->y, b->w, b->h };
|
|
|
|
SDL_FRect result_sdl = { 0 };
|
|
|
|
|
|
|
|
bool intersection = SDL_IntersectFRect(&a_sdl, &b_sdl, &result_sdl);
|
|
|
|
|
2024-09-20 16:39:37 +00:00
|
|
|
if (result != NULL)
|
2024-09-23 17:43:16 +00:00
|
|
|
*result = (Rect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
|
2024-09-20 16:39:37 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool intersect_rect(const Recti *a, const Recti *b) {
|
2024-09-20 16:39:37 +00:00
|
|
|
SDL_Rect a_sdl = { a->x, a->y, a->w, a->h };
|
|
|
|
SDL_Rect b_sdl = { b->x, b->y, b->w, b->h };
|
2024-09-21 01:57:55 +00:00
|
|
|
return SDL_HasIntersection(&a_sdl, &b_sdl);
|
2024-09-20 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool intersect_frect(const Rect *a, const Rect *b) {
|
2024-09-20 16:39:37 +00:00
|
|
|
SDL_FRect a_sdl = { a->x, a->y, a->w, a->h };
|
|
|
|
SDL_FRect b_sdl = { b->x, b->y, b->w, b->h };
|
2024-09-21 01:57:55 +00:00
|
|
|
return SDL_HasIntersectionF(&a_sdl, &b_sdl);
|
2024-09-20 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Rect to_frect(Recti rect) {
|
|
|
|
return (Rect) {
|
2024-07-08 00:44:20 +00:00
|
|
|
.h = (float)rect.h,
|
|
|
|
.w = (float)rect.w,
|
|
|
|
.x = (float)rect.x,
|
|
|
|
.y = (float)rect.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec2 frect_center(Rect rect) {
|
|
|
|
return (Vec2){
|
2024-07-27 13:55:38 +00:00
|
|
|
.x = rect.x + rect.w / 2,
|
|
|
|
.y = rect.y + rect.h / 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
void tick_timer(int *value) {
|
2024-07-08 20:47:22 +00:00
|
|
|
*value = MAX(*value - 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tick_ftimer(float *value) {
|
2024-09-16 13:17:00 +00:00
|
|
|
*value = MAX(*value - ((float)ctx.game.delta_time / (float)ctx.clocks_per_second), 0.0f);
|
2024-07-08 20:47:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:37:03 +00:00
|
|
|
bool repeat_ftimer(float *value, float at) {
|
2024-09-16 13:17:00 +00:00
|
|
|
*value -= (float)ctx.game.delta_time / (float)ctx.clocks_per_second;
|
2024-07-08 20:47:22 +00:00
|
|
|
if (*value < 0.0f) {
|
|
|
|
*value += at;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
2024-09-26 18:02:56 +00:00
|
|
|
|
|
|
|
/* TODO: handle utf8 */
|
|
|
|
char *expand_asterisk(const char *mask, const char *to) {
|
|
|
|
const char *offset = SDL_strchr(mask, '*');
|
|
|
|
if (!offset) {
|
|
|
|
CRY("Invalid path", "Asterisk should be given.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t const mask_len = SDL_strlen(mask);
|
|
|
|
size_t const to_len = SDL_strlen(to);
|
|
|
|
char *str = SDL_malloc(mask_len + to_len); /* NULL included, replacing the original asterisk */
|
|
|
|
SDL_memcpy(str, mask, offset - mask);
|
|
|
|
SDL_memcpy(str + (offset - mask), to, to_len);
|
|
|
|
SDL_memcpy(str + (offset - mask) + to_len, offset + 1, mask_len - (offset - mask));
|
|
|
|
str[mask_len + to_len] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|