townengine/src/util.h

158 lines
4.0 KiB
C
Raw Normal View History

2024-07-08 00:44:20 +00:00
#ifndef UTIL_H
#define UTIL_H
#include <SDL2/SDL.h>
#include <physfs.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <stdnoreturn.h>
/* */
/* GENERAL UTILITIES */
/* */
void cry_impl(const char *file, const int line, const char *title, const char *text);
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())
#define CRY_PHYSFS(title) \
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
void log_info(const char *restrict format, ...);
void log_critical(const char *restrict format, ...);
void log_warn(const char *restrict format, ...);
/* for when there's absolutely no way to continue */
noreturn void die_abruptly(void);
/* "critical" allocation functions which will log and abort() on failure. */
/* if it is reasonable to handle this gracefully, use the standard versions. */
/* the stb implementations will be configured to use these */
void *cmalloc(size_t size);
void *crealloc(void *ptr, size_t size);
void *ccalloc(size_t num, size_t size);
/* DON'T FORGET ABOUT DOUBLE EVALUATION */
/* YOU THINK YOU WON'T, AND THEN YOU DO */
/* C23's typeof could fix it, so i will */
/* leave them as macros to be replaced. */
/* use the macro in tgmath.h for floats */
#define MAX SDL_max
#define MIN SDL_min
#ifndef M_PI
2024-07-08 19:20:26 +00:00
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
#endif
2024-07-08 00:44:20 +00:00
/* sets buf_out to a pointer to a byte buffer which must be freed. */
/* returns the size of this buffer. */
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
/* returns a pointer to a string which must be freed */
2024-07-08 06:46:12 +00:00
char *file_to_str(const char *path);
2024-07-08 00:44:20 +00:00
/* returns true if str ends with suffix */
bool strends(const char *str, const char *suffix);
/* */
/* GAME LOGIC UTILITIES */
/* */
/* 32-bit color data */
typedef struct color {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} t_color;
/* a rectangle with the origin at the upper left (integer) */
typedef struct rect {
int x, y;
int w, h;
} t_rect;
bool intersect_rect(const t_rect *a, const t_rect *b, t_rect *result);
/* a rectangle with the origin at the upper left (floating point) */
typedef struct frect {
float x, y;
float w, h;
} t_frect;
bool intersect_frect(const t_frect *a, const t_frect *b, t_frect *result);
/* TODO: generics and specials (see m_to_fvec2() for an example)*/
2024-07-08 00:44:20 +00:00
t_frect to_frect(t_rect rect);
/* a point in some space (integer) */
typedef struct vec2 {
int x, y;
} t_vec2;
/* a point in some space (floating point) */
typedef struct fvec2 {
float x, y;
} t_fvec2;
/* a point in some three dimension space (floating point) */
/* y goes up, x goes to the right */
typedef struct fvec3 {
float x, y, z;
} t_fvec3;
/* a point in some space (short) */
typedef struct shvec2 {
short x, y;
} t_shvec2;
/* aren't macros to prevent double evaluation with side effects */
/* maybe could be inlined? i hope LTO will resolve this */
t_fvec2 fvec2_from_vec2(t_vec2 vec);
t_fvec2 fvec2_from_shvec2(t_shvec2 vec);
#define m_to_fvec2(p_any_vec2) (_Generic((p_any_vec2), \
t_vec2: fvec2_from_vec2, \
t_shvec2: fvec2_from_shvec2 \
)(p_any_vec2))
2024-07-08 00:44:20 +00:00
/* decrements an lvalue (which should be an int), stopping at 0 */
/* meant for tick-based timers in game logic */
/*
* example:
* tick_timer(&player->jump_air_timer);
*/
void tick_timer(int *value);
/* decrements a floating point second-based timer, stopping at 0.0 */
/* meant for poll based real time logic in game logic */
/* note that it should be decremented only on the next tick after its creation */
void tick_ftimer(float *value);
/* same as `tick_ftimer` but instead of clamping it repeats */
/* returns true if value was cycled */
bool repeat_ftimer(float *value, float at);
2024-07-08 00:44:20 +00:00
#endif