Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1#ifndef _MACROS_H_
2#define _MACROS_H_
3
12#define CLAMP(a, l, u) ((a) < l ? l : ((a) > u ? u : (a)))
13
21#define MIN(a, b) (((a) < (b)) ? (a) : (b))
22
30#define MAX(a, b) (((a) > (b)) ? (a) : (b))
31
38#define ABS(a) (((a) < (0)) ? -(a) : (a))
39
48#ifdef __APPLE__
49 /* Force a compilation error if condition is true, but also produce a
50 result (of value 0 and type size_t), so the expression can be used
51 e.g. in a structure initializer (or where-ever else comma expressions
52 aren't permitted). */
53 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int : -!!(e); }))
54
56 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&(a)[0])))
57
58 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
59#else
61 #define IS_ARRAY(arr) ((void*)&(arr) == &(arr)[0])
62
64 #define STATIC_EXP(e) (0 * sizeof(struct { int ARRAY_SIZE_FAILED : (2 * (e)-1); }))
65
66 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + STATIC_EXP(IS_ARRAY(arr)))
67#endif
68
80#define POS_MODULO_ADD(a, b, d) ((a + (b % d) + d) % d)
81
90#define RUN_TIMER_EVERY(timer, period, elapsed, timer_code) \
91 do \
92 { \
93 timer += elapsed; \
94 while (timer > period) \
95 { \
96 timer -= period; \
97 { \
98 timer_code \
99 } \
100 } \
101 } while (0)
102
103#endif