twn_util: separate internal c-only features from public header
This commit is contained in:
parent
f646bd7adc
commit
60a9307ce3
42
include/twn_types.h
Normal file
42
include/twn_types.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#ifndef TWN_TYPES_H
|
||||||
|
#define TWN_TYPES_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* plain data aggregates that are accepted between public procedure boundaries */
|
||||||
|
|
||||||
|
/* 32-bit color data */
|
||||||
|
typedef struct Color {
|
||||||
|
_Alignas(4)
|
||||||
|
uint8_t r;
|
||||||
|
uint8_t g;
|
||||||
|
uint8_t b;
|
||||||
|
uint8_t a;
|
||||||
|
} Color;
|
||||||
|
|
||||||
|
|
||||||
|
/* a rectangle with the origin at the upper left (integer) */
|
||||||
|
typedef struct Recti {
|
||||||
|
_Alignas(16)
|
||||||
|
int32_t x;
|
||||||
|
int32_t y;
|
||||||
|
int32_t w;
|
||||||
|
int32_t h;
|
||||||
|
} Recti;
|
||||||
|
|
||||||
|
|
||||||
|
/* a rectangle with the origin at the upper left (floating point) */
|
||||||
|
typedef struct Rect {
|
||||||
|
_Alignas(16)
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float w;
|
||||||
|
float h;
|
||||||
|
} Rect;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Matrix4 {
|
||||||
|
Vec4 row[4];
|
||||||
|
} Matrix4;
|
||||||
|
|
||||||
|
#endif
|
@ -3,11 +3,29 @@
|
|||||||
|
|
||||||
#include "twn_vec.h"
|
#include "twn_vec.h"
|
||||||
#include "twn_engine_api.h"
|
#include "twn_engine_api.h"
|
||||||
|
#include "twn_types.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.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
|
||||||
|
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* multiply by these to convert degrees <---> radians */
|
||||||
|
#define DEG2RAD (M_PI / 180)
|
||||||
|
#define RAD2DEG (180 / M_PI)
|
||||||
|
|
||||||
|
|
||||||
/* */
|
/* */
|
||||||
/* GENERAL UTILITIES */
|
/* GENERAL UTILITIES */
|
||||||
/* */
|
/* */
|
||||||
@ -36,27 +54,12 @@ TWN_API void *crealloc(void *ptr, size_t size);
|
|||||||
TWN_API void *ccalloc(size_t num, size_t size);
|
TWN_API 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
|
|
||||||
#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: 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);
|
||||||
TWN_API int clampi(int i, int min, int 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. */
|
/* sets buf_out to a pointer to a byte buffer which must be freed. */
|
||||||
/* returns the size of this buffer. */
|
/* returns the size of this buffer. */
|
||||||
TWN_API int64_t file_to_bytes(const char *path, unsigned char **buf_out);
|
TWN_API int64_t file_to_bytes(const char *path, unsigned char **buf_out);
|
||||||
@ -67,6 +70,7 @@ TWN_API char *file_to_str(const char *path);
|
|||||||
/* returns true if the file exists in the filesystem */
|
/* returns true if the file exists in the filesystem */
|
||||||
TWN_API bool file_exists(const char *path);
|
TWN_API bool file_exists(const char *path);
|
||||||
|
|
||||||
|
|
||||||
/* saves all texture atlases as BMP files in the write directory */
|
/* saves all texture atlases as BMP files in the write directory */
|
||||||
TWN_API void textures_dump_atlases(void);
|
TWN_API void textures_dump_atlases(void);
|
||||||
|
|
||||||
@ -79,36 +83,6 @@ TWN_API TWN_API bool strends(const char *str, const char *suffix);
|
|||||||
/* GAME LOGIC UTILITIES */
|
/* GAME LOGIC UTILITIES */
|
||||||
/* */
|
/* */
|
||||||
|
|
||||||
/* 32-bit color data */
|
|
||||||
typedef struct Color {
|
|
||||||
_Alignas(4)
|
|
||||||
uint8_t r;
|
|
||||||
uint8_t g;
|
|
||||||
uint8_t b;
|
|
||||||
uint8_t a;
|
|
||||||
} Color;
|
|
||||||
|
|
||||||
|
|
||||||
/* a rectangle with the origin at the upper left (integer) */
|
|
||||||
typedef struct Recti {
|
|
||||||
_Alignas(16)
|
|
||||||
int32_t x;
|
|
||||||
int32_t y;
|
|
||||||
int32_t w;
|
|
||||||
int32_t h;
|
|
||||||
} Recti;
|
|
||||||
|
|
||||||
|
|
||||||
/* a rectangle with the origin at the upper left (floating point) */
|
|
||||||
typedef struct Rect {
|
|
||||||
_Alignas(16)
|
|
||||||
float x;
|
|
||||||
float y;
|
|
||||||
float w;
|
|
||||||
float h;
|
|
||||||
} Rect;
|
|
||||||
|
|
||||||
|
|
||||||
/* calculates the overlap of two rectangles and places it in result. */
|
/* calculates the overlap of two rectangles and places it in result. */
|
||||||
/* result may be NULL. if this is the case, it will simply be ignored. */
|
/* result may be NULL. if this is the case, it will simply be ignored. */
|
||||||
/* returns true if the rectangles are indeed intersecting. */
|
/* returns true if the rectangles are indeed intersecting. */
|
||||||
@ -125,11 +99,6 @@ TWN_API Rect to_frect(Recti rect);
|
|||||||
TWN_API Vec2 frect_center(Rect rect);
|
TWN_API Vec2 frect_center(Rect rect);
|
||||||
|
|
||||||
|
|
||||||
typedef struct Matrix4 {
|
|
||||||
Vec4 row[4];
|
|
||||||
} Matrix4;
|
|
||||||
|
|
||||||
|
|
||||||
/* decrements an lvalue (which should be an int), stopping at 0 */
|
/* decrements an lvalue (which should be an int), stopping at 0 */
|
||||||
/* meant for tick-based timers in game logic */
|
/* meant for tick-based timers in game logic */
|
||||||
/*
|
/*
|
||||||
@ -147,32 +116,4 @@ TWN_API void tick_ftimer(float *value);
|
|||||||
/* returns true if value was cycled */
|
/* returns true if value was cycled */
|
||||||
TWN_API bool repeat_ftimer(float *value, float at);
|
TWN_API bool repeat_ftimer(float *value, float at);
|
||||||
|
|
||||||
/* http://www.azillionmonkeys.com/qed/sqroot.html */
|
|
||||||
static inline float fast_sqrt(float x)
|
|
||||||
{
|
|
||||||
union {
|
|
||||||
float f;
|
|
||||||
uint32_t u;
|
|
||||||
} pun = {.f = x};
|
|
||||||
|
|
||||||
pun.u += 127 << 23;
|
|
||||||
pun.u >>= 1;
|
|
||||||
|
|
||||||
return pun.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static inline Vec2 fast_cossine(float a) {
|
|
||||||
const float s = 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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool is_power_of_two(int x)
|
|
||||||
{
|
|
||||||
return (x != 0) && ((x & (x - 1)) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "twn_rendering_c.h"
|
#include "twn_rendering_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 "twn_textures_c.h"
|
#include "twn_textures_c.h"
|
||||||
|
|
||||||
#include <stb_ds.h>
|
#include <stb_ds.h>
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "twn_textures_c.h"
|
#include "twn_textures_c.h"
|
||||||
#include "twn_config.h"
|
#include "twn_config.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 <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
@ -1,7 +1,37 @@
|
|||||||
#ifndef TWN_UTIL_C_H
|
#ifndef TWN_UTIL_C_H
|
||||||
#define TWN_UTIL_C_H
|
#define TWN_UTIL_C_H
|
||||||
|
|
||||||
|
#include "twn_types.h"
|
||||||
|
|
||||||
/* 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);
|
||||||
|
|
||||||
|
/* http://www.azillionmonkeys.com/qed/sqroot.html */
|
||||||
|
static inline float fast_sqrt(float x)
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
float f;
|
||||||
|
uint32_t u;
|
||||||
|
} pun = {.f = x};
|
||||||
|
|
||||||
|
pun.u += 127 << 23;
|
||||||
|
pun.u >>= 1;
|
||||||
|
|
||||||
|
return pun.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline Vec2 fast_cossine(float a) {
|
||||||
|
const float s = 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool is_power_of_two(int x)
|
||||||
|
{
|
||||||
|
return (x != 0) && ((x & (x - 1)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user