2 #include "lib/static.qh"
3 #include "lib/vector.qh"
5 // These macros wrap functions which use globals so mutation only occurs inside them and is not visible from outside.
6 // Functions for which all usages are replaced with these macros can be hidden inside our `*defs.qh` files
7 // to prevent anyone from using them accidentally.
9 // TODO stuff in the engine that uses the v_forward/v_right/v_up globals and is not wrapped yet:
10 // - RF_USEAXIS, addentities, predraw,
11 // - CL_GetEntityMatrix (in engine but is called from other functions so transitively any of them can use the globals - e.g. V_CalcRefdef, maybe others)
12 // - however RF_USEAXIS is only used if MF_ROTATE is used which is only set in one place
13 // - e.camera_transform / CL_VM_TransformView (in engine)
17 #define NEW_VECS(...) EVAL(OVERLOAD(NEW_VECS, __VA_ARGS__))
18 #define NEW_VECS_3(forward, right, up) vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
19 #define NEW_VECS_4(forward, right, up, origin) NEW_VECS_3(forward, right, up); vector origin = '0 0 0';
21 #define CLEAR_V_GLOBALS() v_forward = VEC_NAN; v_right = VEC_NAN; v_up = VEC_NAN;
22 #define GET_V_GLOBALS(forward, right, up) forward = v_forward; right = v_right; up = v_up;
23 #define SET_V_GLOBALS(forward, right, up) v_forward = forward; v_right = right; v_up = up;
26 STATIC_INIT(globals) {
27 // set to NaN to more easily detect uninitialized use
28 // TODO when all functions are wrapped and the raw functions are not used anymore,
29 // uncomment the defines in *progs.qh files that hide the raw functions
30 // and assert that the global vectors are NaN before calling the raw functions here
31 // to make sure nobody (even builtins) is accidentally using them - NaN is the most likely value to expose remaining usages
33 // TODO make sure `isnan` actually works - potential compiler bug:
34 //LOG_INFOF("%f\n", 0.0/0.0 == 0.0/0.0);
35 //LOG_INFOF("%f\n", 0.0/0.0 != 0.0/0.0);
37 //LOG_INFOF("%f\n", x == x);
38 //LOG_INFOF("%f\n", x != x);
44 /// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals.
45 /// Always use this instead of raw `makevectors` to make the data flow clear.
46 #define MAKE_VECTORS(angles, forward, right, up) MACRO_BEGIN { \
47 _makevectors_hidden(angles); \
48 GET_V_GLOBALS(forward, right, up); \
52 /// Same as `MAKE_VECTORS` but also creates the locals for convenience.
53 #define MAKE_VECTORS_NEW(angles, forward, right, up) \
54 NEW_VECS(forward, right, up); \
55 MAKE_VECTORS(angles, forward, right, up);
57 /// Returns all 4 vectors by assigning to them (instead of returning a value) for consistency (and sanity)
58 #define SKEL_GET_BONE_ABS(skel, bonenum, forward, right, up, origin) MACRO_BEGIN { \
59 origin = _skel_get_boneabs_hidden(skel, bonenum) \
60 GET_V_GLOBALS(forward, right, up); \
64 #define SKEL_GET_BONE_ABS_NEW(skel, bonenum, forward, right, up, origin) \
65 NEW_VECS(forward, right, up, origin); \
66 SKEL_GET_BONE_ABS(skel, bonenum, forward, right, up, origin)
68 #define SKEL_SET_BONE(skel, bonenum, org, forward, right, up) MACRO_BEGIN { \
69 SET_V_GLOBALS(forward, right, up); \
70 _skel_set_bone_hidden(skel, bonenum, org); \
74 #define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \
75 _vectorvectors_hidden(forward_in); \
76 GET_V_GLOBALS(forward, right, up); \
80 #define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \
81 NEW_VECS(forward, right, up); \
82 VECTOR_VECTORS(forward_in, forward, right, up);
84 /// Note that this only avoids the v_* globals, not the gettaginfo_* ones
85 #define GET_TAG_INFO(ent, tagindex, forward, right, up, origin) MACRO_BEGIN { \
86 origin = _gettaginfo_hidden(ent, tagindex); \
87 GET_V_GLOBALS(forward, right, up); \
91 #define GET_TAG_INFO_NEW(ent, tagindex, forward, right, up, origin) \
92 NEW_VECS(forward, right, up, origin); \
93 GET_TAG_INFO(ent, tagindex, forward, right, up, origin);