2024-07-08 00:44:20 +00:00
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_vec.h"
|
2024-08-26 21:33:37 +00:00
|
|
|
#include "twn_engine_api.h"
|
2024-10-07 07:44:18 +00:00
|
|
|
#include "twn_types.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
2024-10-07 07:44:18 +00:00
|
|
|
/* 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
|
|
|
|
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* multiply by these to convert degrees <---> radians */
|
|
|
|
#define DEG2RAD (M_PI / 180)
|
|
|
|
#define RAD2DEG (180 / M_PI)
|
|
|
|
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
/* */
|
|
|
|
/* GENERAL UTILITIES */
|
|
|
|
/* */
|
|
|
|
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API void cry_impl(const char *file, const int line, const char *title, const char *text);
|
2024-07-30 21:05:28 +00:00
|
|
|
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
|
|
|
|
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())
|
2024-07-08 00:44:20 +00:00
|
|
|
#define CRY_PHYSFS(title) \
|
|
|
|
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
|
|
|
|
|
|
|
|
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API void log_info(const char *restrict format, ...);
|
|
|
|
TWN_API void log_critical(const char *restrict format, ...);
|
|
|
|
TWN_API void log_warn(const char *restrict format, ...);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* for when there's absolutely no way to continue */
|
2024-09-25 22:42:34 +00:00
|
|
|
TWN_API _Noreturn void die_abruptly(void);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* "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 */
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API void *cmalloc(size_t size);
|
|
|
|
TWN_API void *crealloc(void *ptr, size_t size);
|
|
|
|
TWN_API void *ccalloc(size_t num, size_t size);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
/* TODO: this is why generics were invented. sorry, i'm tired today */
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API double clamp(double d, double min, double max);
|
|
|
|
TWN_API float clampf(float f, float min, float max);
|
|
|
|
TWN_API int clampi(int i, int min, int max);
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-07 07:44:18 +00:00
|
|
|
|
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-08-26 21:33:37 +00:00
|
|
|
TWN_API 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-08-26 21:33:37 +00:00
|
|
|
TWN_API char *file_to_str(const char *path);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-10-05 21:17:22 +00:00
|
|
|
/* returns true if the file exists in the filesystem */
|
|
|
|
TWN_API bool file_exists(const char *path);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-10-07 07:44:18 +00:00
|
|
|
|
2024-09-27 00:32:08 +00:00
|
|
|
/* saves all texture atlases as BMP files in the write directory */
|
|
|
|
TWN_API void textures_dump_atlases(void);
|
|
|
|
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
/* returns true if str ends with suffix */
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API TWN_API bool strends(const char *str, const char *suffix);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* */
|
|
|
|
/* GAME LOGIC UTILITIES */
|
|
|
|
/* */
|
|
|
|
|
2024-09-20 16:39:37 +00:00
|
|
|
/* calculates the overlap of two rectangles and places it in result. */
|
2024-09-20 16:48:29 +00:00
|
|
|
/* result may be NULL. if this is the case, it will simply be ignored. */
|
2024-09-20 16:39:37 +00:00
|
|
|
/* returns true if the rectangles are indeed intersecting. */
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API bool overlap_rect(const Recti *a, const Recti *b, Recti *result);
|
|
|
|
TWN_API bool overlap_frect(const Rect *a, const Rect *b, Rect *result);
|
2024-09-20 16:39:37 +00:00
|
|
|
|
2024-09-20 16:41:55 +00:00
|
|
|
/* returns true if two rectangles are intersecting */
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API bool intersect_rect(const Recti *a, const Recti *b);
|
|
|
|
TWN_API bool intersect_frect(const Rect *a, const Rect *b);
|
2024-07-27 13:55:38 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
/* TODO: generics and specials (see m_vec2_from() for an example)*/
|
|
|
|
TWN_API Rect to_frect(Recti rect);
|
2024-07-27 13:55:38 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API Vec2 frect_center(Rect rect);
|
2024-07-27 13:55:38 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
*/
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API void tick_timer(int *value);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-08 20:47:22 +00:00
|
|
|
/* 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 */
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API void tick_ftimer(float *value);
|
2024-07-08 20:47:22 +00:00
|
|
|
|
2024-07-09 12:37:03 +00:00
|
|
|
/* same as `tick_ftimer` but instead of clamping it repeats */
|
2024-07-08 20:47:22 +00:00
|
|
|
/* returns true if value was cycled */
|
2024-08-26 21:33:37 +00:00
|
|
|
TWN_API bool repeat_ftimer(float *value, float at);
|
2024-07-08 20:47:22 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
#endif
|