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#define NUM_FRAME_TIMES 60
104#define DRAW_FPS_COUNTER(font) \
105 do \
106 { \
107 static uint32_t frameTimesIdx = 0; \
108 static uint32_t frameTimes[NUM_FRAME_TIMES] = {0}; \
109 \
110 int32_t startIdx = (frameTimesIdx + 1) % NUM_FRAME_TIMES; \
111 uint32_t tElapsed = frameTimes[frameTimesIdx] - frameTimes[startIdx]; \
112 if (0 != tElapsed) \
113 { \
114 uint32_t fps = (1000000 * NUM_FRAME_TIMES) / tElapsed; \
115 char tmp[16]; \
116 snprintf(tmp, sizeof(tmp) - 1, "%" PRIu32, fps); \
117 drawText(&font, c555, tmp, 35, 2); \
118 } \
119 frameTimesIdx = (frameTimesIdx + 1) % NUM_FRAME_TIMES; \
120 frameTimes[frameTimesIdx] = esp_timer_get_time(); \
121 } while (0)
122
123#endif