twn_util.h: separate internal things away, remove indirect includes in places
This commit is contained in:
		| @@ -4,6 +4,7 @@ for that certain steps are taken: | |||||||
| * number of public api calls is kept at the minimum | * 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, | * 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) |   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 | * /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 | one of main inspirations for that is opengl model | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| #ifndef TWN_CAMERA_H | #ifndef TWN_CAMERA_H | ||||||
| #define TWN_CAMERA_H | #define TWN_CAMERA_H | ||||||
|  |  | ||||||
| #include "twn_util.h" | #include "twn_types.h" | ||||||
| #include "twn_engine_api.h" | #include "twn_engine_api.h" | ||||||
|  |  | ||||||
| /* TODO: make it cached? */ | /* TODO: make it cached? */ | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| #ifndef TWN_CONTEXT_H | #ifndef TWN_CONTEXT_H | ||||||
| #define TWN_CONTEXT_H | #define TWN_CONTEXT_H | ||||||
|  |  | ||||||
| #include "twn_input.h" | #include "twn_types.h" | ||||||
| #include "twn_engine_api.h" | #include "twn_engine_api.h" | ||||||
|  |  | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| #ifndef TWN_DRAW_H | #ifndef TWN_DRAW_H | ||||||
| #define TWN_DRAW_H | #define TWN_DRAW_H | ||||||
|  |  | ||||||
| #include "twn_util.h" | #include "twn_types.h" | ||||||
| #include "twn_option.h" | #include "twn_option.h" | ||||||
| #include "twn_camera.h" | #include "twn_camera.h" | ||||||
| #include "twn_engine_api.h" | #include "twn_engine_api.h" | ||||||
|   | |||||||
| @@ -2,21 +2,22 @@ | |||||||
| #ifndef TWN_GAME_API_H | #ifndef TWN_GAME_API_H | ||||||
| #define TWN_GAME_API_H | #define TWN_GAME_API_H | ||||||
|  |  | ||||||
|  | #include "twn_input.h" | ||||||
| #include "twn_context.h" | #include "twn_context.h" | ||||||
| #include "twn_draw.h" | #include "twn_draw.h" | ||||||
| #include "twn_audio.h" | #include "twn_audio.h" | ||||||
| #include "twn_util.h" |  | ||||||
| #include "twn_input.h" |  | ||||||
| #include "twn_engine_api.h" | #include "twn_engine_api.h" | ||||||
|  | #include "twn_util.h" | ||||||
|  |  | ||||||
|  | #ifndef TWN_NOT_C | ||||||
|  |  | ||||||
| /* sole game logic and display function. |    /* sole game logic and display function. | ||||||
|    all state must be used from and saved to supplied state pointer. */ |       all state must be used from and saved to supplied state pointer. */ | ||||||
| TWN_API extern void game_tick(void); |    TWN_API extern void game_tick(void); | ||||||
|  |  | ||||||
| /* called when application is closing. */ |  | ||||||
| TWN_API extern void game_end(void); |  | ||||||
|  |  | ||||||
|  |    /* called when application is closing. */ | ||||||
|  |    TWN_API extern void game_end(void); | ||||||
|  |  | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -3,21 +3,13 @@ | |||||||
|  |  | ||||||
| #include "twn_types.h" | #include "twn_types.h" | ||||||
| #include "twn_engine_api.h" | #include "twn_engine_api.h" | ||||||
| #include "twn_types.h" |  | ||||||
|  |  | ||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
|  | #include <stddef.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| #include <math.h> | #include <math.h> | ||||||
|  |  | ||||||
|  |  | ||||||
| /* 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 | #ifndef M_PI | ||||||
| #define M_PI 3.14159265358979323846264338327950288   /**< pi */ | #define M_PI 3.14159265358979323846264338327950288   /**< pi */ | ||||||
| #endif | #endif | ||||||
| @@ -26,16 +18,13 @@ | |||||||
| #define DEG2RAD (M_PI / 180) | #define DEG2RAD (M_PI / 180) | ||||||
| #define RAD2DEG (180 / M_PI) | #define RAD2DEG (180 / M_PI) | ||||||
|  |  | ||||||
|  | #ifndef TWN_NOT_C | ||||||
|  |  | ||||||
| /*                   */ |     TWN_API void *cmalloc(size_t size); | ||||||
| /* GENERAL UTILITIES */ |     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); | #endif /* TWN_NOT_C */ | ||||||
| #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())) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| TWN_API void log_info(const char *restrict format, ...); | 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, ...); | 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 */ | /* TODO: this is why generics were invented. sorry, i'm tired today */ | ||||||
| TWN_API double clamp(double d, double min, double max); | TWN_API double clamp(double d, double min, double max); | ||||||
| TWN_API float clampf(float f, float min, float max); | TWN_API float clampf(float f, float min, float max); | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| #include "twn_game_object_c.h" | #include "twn_game_object_c.h" | ||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
|  | #include "twn_util_c.h" | ||||||
|  |  | ||||||
| #include <x-watcher.h> | #include <x-watcher.h> | ||||||
| #include <SDL2/SDL.h> | #include <SDL2/SDL.h> | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| #include "twn_game_object_c.h" | #include "twn_game_object_c.h" | ||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
|  | #include "twn_util_c.h" | ||||||
|  |  | ||||||
| #include <errhandlingapi.h> | #include <errhandlingapi.h> | ||||||
| #include <libloaderapi.h> | #include <libloaderapi.h> | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| #include "twn_gpu_texture_c.h" | #include "twn_gpu_texture_c.h" | ||||||
| #include "twn_util.h" | #include "twn_util_c.h" | ||||||
|  |  | ||||||
|  |  | ||||||
| GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps) { | GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps) { | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| #include "twn_draw_c.h" | #include "twn_draw_c.h" | ||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
| #include "twn_util.h" | #include "twn_util_c.h" | ||||||
|  |  | ||||||
| #ifdef EMSCRIPTEN | #ifdef EMSCRIPTEN | ||||||
| #include <GLES2/gl2.h> | #include <GLES2/gl2.h> | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| #include "twn_draw_c.h" | #include "twn_draw_c.h" | ||||||
| #include "twn_draw.h" | #include "twn_draw.h" | ||||||
| #include "twn_util.h" | #include "twn_util.h" | ||||||
|  | #include "twn_util_c.h" | ||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
|  |  | ||||||
| #include <stb_truetype.h> | #include <stb_truetype.h> | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| #include "twn_audio_c.h" | #include "twn_audio_c.h" | ||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
| #include "twn_util.h" | #include "twn_util.h" | ||||||
|  | #include "twn_util_c.h" | ||||||
|  |  | ||||||
| #include <SDL2/SDL.h> | #include <SDL2/SDL.h> | ||||||
| #include <stb_ds.h> | #include <stb_ds.h> | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
| #include "twn_engine_context_c.h" | #include "twn_engine_context_c.h" | ||||||
| #include "twn_input_c.h" | #include "twn_input_c.h" | ||||||
| #include "twn_util.h" | #include "twn_util.h" | ||||||
|  | #include "twn_util_c.h" | ||||||
| #include "twn_game_object_c.h" | #include "twn_game_object_c.h" | ||||||
| #include "twn_audio_c.h" | #include "twn_audio_c.h" | ||||||
| #include "twn_textures_c.h" | #include "twn_textures_c.h" | ||||||
|   | |||||||
| @@ -3,6 +3,23 @@ | |||||||
|  |  | ||||||
| #include "twn_types.h" | #include "twn_types.h" | ||||||
|  |  | ||||||
|  | #include <SDL2/SDL.h> | ||||||
|  |  | ||||||
|  | #include <stdbool.h> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #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 */ | /* note: you must free the returned string */ | ||||||
| char *expand_asterisk(const char *mask, const char *to); | 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) { | static inline Vec2 fast_cossine(float a) { | ||||||
|     const float s = sinf(a); |     const float s = SDL_sinf(a); | ||||||
|     return (Vec2){ |     return (Vec2){ | ||||||
|         .x = fast_sqrt(1.0f - s * s) * (a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1), |         .x = fast_sqrt(1.0f - s * s) * (a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1), | ||||||
|         .y = s |         .y = s | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user