From f044a75ffeaf50e494a22ea74905f542ec9e70f8 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 20 Feb 2025 13:48:44 +0300 Subject: [PATCH] reorganization of twn_util.h, reletion of some seldom used procedures --- include/twn_util.h | 39 +++++++-------------------- src/rendering/twn_sprites.c | 4 +-- src/twn_util.c | 54 ------------------------------------- src/twn_util_c.h | 2 ++ 4 files changed, 13 insertions(+), 86 deletions(-) diff --git a/include/twn_util.h b/include/twn_util.h index afeb956..164231f 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -7,19 +7,18 @@ #include #include #include -#include -#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) - -/* TODO: shouldn't be a thing */ #ifndef TWN_NOT_C + #include + + #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) TWN_API void log_info(const char *restrict format, ...); TWN_API void log_critical(const char *restrict format, ...); @@ -28,13 +27,8 @@ /* saves all texture atlases as BMP files in the write directory */ TWN_API void textures_dump_atlases(void); - /* returns true if str ends with suffix */ - TWN_API bool strends(const char *str, const char *suffix); - /* TODO: this is why generics were invented. sorry, i'm tired today */ - 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); /* sets buf_out to a pointer to a byte buffer which must be freed. */ /* returns the size of this buffer. */ @@ -52,26 +46,12 @@ TWN_API Rect rect_overlap(Rect a, Rect b); /* returns true if two rectangles are intersecting */ TWN_API bool rect_intersects(Rect a, Rect b); -TWN_API Vec2 rect_center(Rect rect); - -/* decrements an integer value, stopping at 0 */ -/* meant for tick-based timers in game logic */ -/* - * example: - * tick_timer(&player->jump_air_timer); - */ -TWN_API int32_t timer_tick_frames(int32_t frames_left); /* decrements a floating point second-based timer, stopping at 0.0f */ /* meant for poll based real time logic in game logic */ /* note that it should be decremented only on the next tick after its creation */ TWN_API float timer_tick_seconds(float seconds_left); -typedef struct TimerElapseFramesResult { - bool elapsed; int32_t frames_left; -} TimerElapseFramesResult; -TWN_API TimerElapseFramesResult timer_elapse_frames(int32_t frames_left, int32_t interval); - typedef struct TimerElapseSecondsResult { bool elapsed; float seconds_left; } TimerElapseSecondsResult; @@ -83,6 +63,5 @@ TWN_API void log_rect(Rect value, char const *identity); TWN_API void profile_start(char const *profile); TWN_API void profile_end(char const *profile); -TWN_API void profile_list_stats(void); #endif diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index a690f0e..c5dc0a5 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -216,7 +216,7 @@ void render_sprite_batch(const Primitive2D primitives[], #pragma GCC diagnostic pop - const Vec2 c = rect_center(sprite.rect); + const Vec2 c = { sprite.rect.x + sprite.rect.w / 2, sprite.rect.y + sprite.rect.h / 2 }; const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4); const Vec2 d = { .x = t.x * sprite.rect.w * (float)M_SQRT1_2, @@ -231,7 +231,7 @@ void render_sprite_batch(const Primitive2D primitives[], } else { /* rotated non-square case*/ - const Vec2 c = rect_center(sprite.rect); + const Vec2 c = { sprite.rect.x + sprite.rect.w / 2, sprite.rect.y + sprite.rect.h / 2 }; const Vec2 t = fast_cossine(sprite.rotation); const Vec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 }; diff --git a/src/twn_util.c b/src/twn_util.c index 1231300..ff11302 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -109,24 +109,12 @@ void *ccalloc(size_t num, size_t size) { } -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; -} - - int64_t file_to_bytes(const char *path, unsigned char **buf_out) { SDL_RWops *handle = PHYSFSRWOPS_openRead(path); @@ -198,17 +186,6 @@ void textures_dump_atlases(void) { } -bool strends(const char *str, const char *suffix) { - size_t str_length = SDL_strlen(str); - size_t suffix_length = SDL_strlen(suffix); - - if (suffix_length > str_length) - return false; - - return SDL_memcmp((str + str_length) - suffix_length, suffix, suffix_length) == 0; -} - - /* TODO: have our own */ Rect rect_overlap(const Rect a, const Rect b) { SDL_FRect a_sdl = { a.x, a.y, a.w, a.h }; @@ -229,43 +206,12 @@ bool rect_intersects(const Rect a, const Rect b) { } -Vec2 rect_center(Rect rect) { - return (Vec2){ - .x = rect.x + rect.w / 2, - .y = rect.y + rect.h / 2, - }; -} - - -int32_t timer_tick_frames(int32_t frames_left) { - SDL_assert(frames_left >= 0); - return MAX(frames_left - 1, 0); -} - - float timer_tick_seconds(float seconds_left) { SDL_assert(seconds_left >= 0); return MAX(seconds_left - ((float)ctx.delta_time / (float)ctx.clocks_per_second), 0.0f); } -TimerElapseFramesResult timer_elapse_frames(int32_t frames_left, int32_t interval) { - SDL_assert(frames_left >= 0); - SDL_assert(interval > 0); - - frames_left -= 1; - bool elapsed = false; - if (frames_left <= 0) { - elapsed = true; - frames_left += interval; - } - return (TimerElapseFramesResult) { - .elapsed = elapsed, - .frames_left = frames_left - }; -} - - TimerElapseSecondsResult timer_elapse_seconds(float seconds_left, float interval) { SDL_assert(seconds_left >= 0); SDL_assert(interval > 0); diff --git a/src/twn_util_c.h b/src/twn_util_c.h index 8c13c63..15ce61f 100644 --- a/src/twn_util_c.h +++ b/src/twn_util_c.h @@ -27,6 +27,8 @@ _Noreturn void die_abruptly(void); /* note: you must free the returned string */ char *expand_asterisk(const char *mask, const char *to); +void profile_list_stats(void); + /* http://www.azillionmonkeys.com/qed/sqroot.html */ static inline float fast_sqrt(float x) {