From 1a7322dccf13109e173b21c595934d698f45506c Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sat, 12 Oct 2024 21:16:25 +0300 Subject: [PATCH] twn_util.h: separate internal things away, remove indirect includes in places --- docs/interop.txt | 1 + include/twn_camera.h | 2 +- include/twn_context.h | 2 +- include/twn_draw.h | 2 +- include/twn_game_api.h | 19 +++++++------- include/twn_util.h | 35 +++++-------------------- src/game_object/twn_linux_game_object.c | 1 + src/game_object/twn_win32_game_object.c | 1 + src/rendering/twn_gl_15_gpu_texture.c | 2 +- src/rendering/twn_gl_any_rendering.c | 2 +- src/rendering/twn_text.c | 1 + src/twn_audio.c | 1 + src/twn_loop.c | 1 + src/twn_util_c.h | 19 +++++++++++++- 14 files changed, 45 insertions(+), 44 deletions(-) diff --git a/docs/interop.txt b/docs/interop.txt index 2048066..f539d64 100644 --- a/docs/interop.txt +++ b/docs/interop.txt @@ -4,6 +4,7 @@ for that certain steps are taken: * number of public api calls is kept at the minimum * procedure signatures can only use basic types, no aggregates, with exception of Vec/Matrix types and alike, with no expectation on new additions (see /include/twn_types.h) +* optionals can be expressed via pointer passage of value primitives, with NULL expressive default * /include/twn_game_api.json file is hand-kept with a schema to aid automatic generation and other tooling one of main inspirations for that is opengl model diff --git a/include/twn_camera.h b/include/twn_camera.h index fb8ca28..ff3031d 100644 --- a/include/twn_camera.h +++ b/include/twn_camera.h @@ -1,7 +1,7 @@ #ifndef TWN_CAMERA_H #define TWN_CAMERA_H -#include "twn_util.h" +#include "twn_types.h" #include "twn_engine_api.h" /* TODO: make it cached? */ diff --git a/include/twn_context.h b/include/twn_context.h index 9ae0037..bda9b58 100644 --- a/include/twn_context.h +++ b/include/twn_context.h @@ -1,7 +1,7 @@ #ifndef TWN_CONTEXT_H #define TWN_CONTEXT_H -#include "twn_input.h" +#include "twn_types.h" #include "twn_engine_api.h" #include diff --git a/include/twn_draw.h b/include/twn_draw.h index 9eeff65..58f1930 100644 --- a/include/twn_draw.h +++ b/include/twn_draw.h @@ -1,7 +1,7 @@ #ifndef TWN_DRAW_H #define TWN_DRAW_H -#include "twn_util.h" +#include "twn_types.h" #include "twn_option.h" #include "twn_camera.h" #include "twn_engine_api.h" diff --git a/include/twn_game_api.h b/include/twn_game_api.h index b835a72..9c8975e 100644 --- a/include/twn_game_api.h +++ b/include/twn_game_api.h @@ -2,21 +2,22 @@ #ifndef TWN_GAME_API_H #define TWN_GAME_API_H - +#include "twn_input.h" #include "twn_context.h" #include "twn_draw.h" #include "twn_audio.h" -#include "twn_util.h" -#include "twn_input.h" #include "twn_engine_api.h" +#include "twn_util.h" +#ifndef TWN_NOT_C -/* sole game logic and display function. - all state must be used from and saved to supplied state pointer. */ -TWN_API extern void game_tick(void); - -/* called when application is closing. */ -TWN_API extern void game_end(void); + /* sole game logic and display function. + all state must be used from and saved to supplied state pointer. */ + TWN_API extern void game_tick(void); + /* called when application is closing. */ + TWN_API extern void game_end(void); + +#endif #endif diff --git a/include/twn_util.h b/include/twn_util.h index 1e43c5d..454825c 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -3,21 +3,13 @@ #include "twn_types.h" #include "twn_engine_api.h" -#include "twn_types.h" #include +#include #include #include -/* 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 @@ -26,16 +18,13 @@ #define DEG2RAD (M_PI / 180) #define RAD2DEG (180 / M_PI) +#ifndef TWN_NOT_C -/* */ -/* GENERAL UTILITIES */ -/* */ + 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); -TWN_API 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())) +#endif /* TWN_NOT_C */ TWN_API void log_info(const char *restrict format, ...); @@ -43,18 +32,6 @@ TWN_API void log_critical(const char *restrict format, ...); 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); - - -/* "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 */ -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); - - /* 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); diff --git a/src/game_object/twn_linux_game_object.c b/src/game_object/twn_linux_game_object.c index 5de0e1f..d54ccc0 100644 --- a/src/game_object/twn_linux_game_object.c +++ b/src/game_object/twn_linux_game_object.c @@ -1,5 +1,6 @@ #include "twn_game_object_c.h" #include "twn_engine_context_c.h" +#include "twn_util_c.h" #include #include diff --git a/src/game_object/twn_win32_game_object.c b/src/game_object/twn_win32_game_object.c index ea40ff5..a9f3ec3 100644 --- a/src/game_object/twn_win32_game_object.c +++ b/src/game_object/twn_win32_game_object.c @@ -1,5 +1,6 @@ #include "twn_game_object_c.h" #include "twn_engine_context_c.h" +#include "twn_util_c.h" #include #include diff --git a/src/rendering/twn_gl_15_gpu_texture.c b/src/rendering/twn_gl_15_gpu_texture.c index 7d6301c..99b4dfc 100644 --- a/src/rendering/twn_gl_15_gpu_texture.c +++ b/src/rendering/twn_gl_15_gpu_texture.c @@ -1,5 +1,5 @@ #include "twn_gpu_texture_c.h" -#include "twn_util.h" +#include "twn_util_c.h" GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps) { diff --git a/src/rendering/twn_gl_any_rendering.c b/src/rendering/twn_gl_any_rendering.c index a7795c9..a710583 100644 --- a/src/rendering/twn_gl_any_rendering.c +++ b/src/rendering/twn_gl_any_rendering.c @@ -1,6 +1,6 @@ #include "twn_draw_c.h" #include "twn_engine_context_c.h" -#include "twn_util.h" +#include "twn_util_c.h" #ifdef EMSCRIPTEN #include diff --git a/src/rendering/twn_text.c b/src/rendering/twn_text.c index 2a13bb8..478ad96 100644 --- a/src/rendering/twn_text.c +++ b/src/rendering/twn_text.c @@ -1,6 +1,7 @@ #include "twn_draw_c.h" #include "twn_draw.h" #include "twn_util.h" +#include "twn_util_c.h" #include "twn_engine_context_c.h" #include diff --git a/src/twn_audio.c b/src/twn_audio.c index c8d53c2..a9b48a3 100644 --- a/src/twn_audio.c +++ b/src/twn_audio.c @@ -1,6 +1,7 @@ #include "twn_audio_c.h" #include "twn_engine_context_c.h" #include "twn_util.h" +#include "twn_util_c.h" #include #include diff --git a/src/twn_loop.c b/src/twn_loop.c index c5ff5d5..5eeff38 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -2,6 +2,7 @@ #include "twn_engine_context_c.h" #include "twn_input_c.h" #include "twn_util.h" +#include "twn_util_c.h" #include "twn_game_object_c.h" #include "twn_audio_c.h" #include "twn_textures_c.h" diff --git a/src/twn_util_c.h b/src/twn_util_c.h index 14f237d..a094ef9 100644 --- a/src/twn_util_c.h +++ b/src/twn_util_c.h @@ -3,6 +3,23 @@ #include "twn_types.h" +#include + +#include + + +#define MAX SDL_max +#define MIN SDL_min + +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())) + +/* for when there's absolutely no way to continue */ +_Noreturn void die_abruptly(void); + /* note: you must free the returned string */ char *expand_asterisk(const char *mask, const char *to); @@ -22,7 +39,7 @@ static inline float fast_sqrt(float x) static inline Vec2 fast_cossine(float a) { - const float s = sinf(a); + const float s = SDL_sinf(a); return (Vec2){ .x = fast_sqrt(1.0f - s * s) * (a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1), .y = s