Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
macros.h File Reference

Go to the source code of this file.

Macros

#define CLAMP(a, l, u)   ((a) < l ? l : ((a) > u ? u : (a)))
 Clamp a number between an upper and lower bound.
 
#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 Find the smaller of two numbers.
 
#define MAX(a, b)   (((a) > (b)) ? (a) : (b))
 Find the larger of two numbers.
 
#define ABS(a)   (((a) < (0)) ? -(a) : (a))
 Find the absolute value of a number.
 
#define IS_ARRAY(arr)   ((void*)&(arr) == &(arr)[0])
 Return the number of elements in a fixed length array. This does not work for pointers.
 
#define STATIC_EXP(e)   (0 * sizeof(struct { int ARRAY_SIZE_FAILED : (2 * (e)-1); }))
 Helper macro to determine the number of elements in an array. Should not be used directly.
 
#define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]) + STATIC_EXP(IS_ARRAY(arr)))
 
#define POS_MODULO_ADD(a, b, d)   ((a + (b % d) + d) % d)
 Returns (a + b) % d, but with negative values converted to equivalent positive values. The resulting value will always be in the range [0, d), assuming d > 0.
 
#define RUN_TIMER_EVERY(timer, period, elapsed, timer_code)
 Run timer_code every period, using tracking it with timer.
 
#define NUM_FRAME_TIMES   60
 
#define DRAW_FPS_COUNTER(font)
 

Macro Definition Documentation

◆ CLAMP

#define CLAMP ( a,
l,
u )   ((a) < l ? l : ((a) > u ? u : (a)))

Clamp a number between an upper and lower bound.

Parameters
aA number to clamp between an upper and lower bound
lThe lower bound, inclusive
uThe upper bound, inclusive
Returns
The clamped number

◆ MIN

#define MIN ( a,
b )   (((a) < (b)) ? (a) : (b))

Find the smaller of two numbers.

Parameters
aA number to compare
bAnother number to compare
Returns
The smaller of the two numbers

◆ MAX

#define MAX ( a,
b )   (((a) > (b)) ? (a) : (b))

Find the larger of two numbers.

Parameters
aA number to compare
bAnother number to compare
Returns
The larger of the two numbers

◆ ABS

#define ABS ( a)    (((a) < (0)) ? -(a) : (a))

Find the absolute value of a number.

Parameters
aA number to find the absolute value of
Returns
The absolute value fo the number

◆ IS_ARRAY

#define IS_ARRAY ( arr)    ((void*)&(arr) == &(arr)[0])

Return the number of elements in a fixed length array. This does not work for pointers.

See https://stackoverflow.com/a/19455169

Parameters
arrAn array to find the number of elements in
Returns
The the number of elements in an array (not the byte size!) Helper macro to determine the number of elements in an array. Should not be used directly

◆ STATIC_EXP

#define STATIC_EXP ( e)    (0 * sizeof(struct { int ARRAY_SIZE_FAILED : (2 * (e)-1); }))

Helper macro to determine the number of elements in an array. Should not be used directly.

◆ ARRAY_SIZE

#define ARRAY_SIZE ( arr)    (sizeof(arr) / sizeof((arr)[0]) + STATIC_EXP(IS_ARRAY(arr)))

◆ POS_MODULO_ADD

#define POS_MODULO_ADD ( a,
b,
d )   ((a + (b % d) + d) % d)

Returns (a + b) % d, but with negative values converted to equivalent positive values. The resulting value will always be in the range [0, d), assuming d > 0.

The first modulo, (b % d) will return e.g. -90 for (-270 % 360)

Parameters
aOne number to sum
bAnother number to sum
dThe number to mod the sum by
Returns
(a + b) % d

◆ RUN_TIMER_EVERY

#define RUN_TIMER_EVERY ( timer,
period,
elapsed,
timer_code )
Value:
do \
{ \
timer += (elapsed); \
while (timer > (period)) \
{ \
timer -= (period); \
{ \
timer_code \
} \
} \
} while (0)

Run timer_code every period, using tracking it with timer.

Parameters
timerThe accumulator variable, must persist between calls
periodThe period at which timer_code should be run
elapsedThe time elapsed since this was last called
timer_codeThe code to execute every period

◆ NUM_FRAME_TIMES

#define NUM_FRAME_TIMES   60

◆ DRAW_FPS_COUNTER

#define DRAW_FPS_COUNTER ( font)
Value:
do \
{ \
static uint32_t frameTimesIdx = 0; \
static uint32_t frameTimes[NUM_FRAME_TIMES] = {0}; \
\
int32_t startIdx = (frameTimesIdx + 1) % NUM_FRAME_TIMES; \
uint32_t tElapsed = frameTimes[frameTimesIdx] - frameTimes[startIdx]; \
if (0 != tElapsed) \
{ \
uint32_t fps = (1000000 * NUM_FRAME_TIMES) / tElapsed; \
char tmp[16]; \
snprintf(tmp, sizeof(tmp) - 1, "%" PRIu32, fps); \
drawText(&font, c555, tmp, 35, 2); \
} \
frameTimesIdx = (frameTimesIdx + 1) % NUM_FRAME_TIMES; \
frameTimes[frameTimesIdx] = esp_timer_get_time(); \
} while (0)
#define NUM_FRAME_TIMES
Definition macros.h:103
@ c555
r = 5, g = 5, b = 5
Definition palette.h:239