Skip to content

Commit 31e8ef1

Browse files
committed
add math helper macros
1 parent 0f19aa9 commit 31e8ef1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

d_math.h

+34
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,40 @@ int d_mapi(int, int, int, int, int);
219219
void d_swapi(int*, int*);
220220
void d_swapf(float*, float*);
221221

222+
// https://stackoverflow.com/a/28074198/4584387
223+
#define V2_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
224+
#define V2_FUNC_RECOMPOSER(args) V2_FUNC_CHOOSER args
225+
#define V2_CHOOSE_FROM_ARG_COUNT(...) V2_FUNC_RECOMPOSER((__VA_ARGS__, V2_2, V2_1, ))
226+
#define V2_NO_ARG_EXPANDER() ,,V2_0
227+
#define V2_MACRO_CHOOSER(...) V2_CHOOSE_FROM_ARG_COUNT(V2_NO_ARG_EXPANDER __VA_ARGS__ ())
228+
#define V2_2(x, y) ((d_vec2) { (x), (y) })
229+
#define V2_1(x) V2_2(x, x)
230+
#define V2_0() V2_2(0, 0)
231+
#define V2(...) V2_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
232+
233+
#define V3_FUNC_CHOOSER(_f1, _f2, _f3, _f4, ...) _f4
234+
#define V3_FUNC_RECOMPOSER(args) V3_FUNC_CHOOSER args
235+
#define V3_CHOOSE_FROM_ARG_COUNT(...) V3_FUNC_RECOMPOSER((__VA_ARGS__, V3_3, V3_2, V3_1, ))
236+
#define V3_NO_ARG_EXPANDER() ,,V3_0
237+
#define V3_MACRO_CHOOSER(...) V3_CHOOSE_FROM_ARG_COUNT(V3_NO_ARG_EXPANDER __VA_ARGS__ ())
238+
#define V3_3(x, y, z) ((d_vec3) { (x), (y), (z) })
239+
#define V3_2(x, y) ((d_vec3) { (x), (y), (0) })
240+
#define V3_1(x) V3_3(x, x, x)
241+
#define V3_0() V3_3(0, 0, 0)
242+
#define V3(...) V3_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
243+
244+
#define C_FUNC_CHOOSER(_f1, _f2, _f3, _f4, _f5, ...) _f5
245+
#define C_FUNC_RECOMPOSER(args) C_FUNC_CHOOSER args
246+
#define C_CHOOSE_FROM_ARG_COUNT(...) C_FUNC_RECOMPOSER((__VA_ARGS__, C_4, C_3, C_2, C_1, ))
247+
#define C_NO_ARG_EXPANDER() ,,C_0
248+
#define C_MACRO_CHOOSER(...) C_CHOOSE_FROM_ARG_COUNT(C_NO_ARG_EXPANDER __VA_ARGS__ ())
249+
#define C_4(r, g, b, a) ((d_color) { (r), (g), (b), (a) })
250+
#define C_3(r, g, b) ((d_color) { (r), (g), (b), (255) })
251+
#define C_2(g, a) ((d_color) { (g), (g), (g), (a) })
252+
#define C_1(g) ((d_color) { (g), (g), (g), (255) })
253+
#define C_0() C_1(255)
254+
#define C(...) C_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
255+
222256
#endif
223257

224258
#if defined(D_MATH_IMPL) || defined(D_IMPL)

0 commit comments

Comments
 (0)