]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/deglobalization.qh
deglob wzl/anglestransform.qh
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / deglobalization.qh
1 #include "lib/float.qh"
2 #include "lib/misc.qh"
3 #include "lib/static.qh"
4 #include "lib/vector.qh"
5
6 // These macros wrap functions which use globals so mutation only occurs inside them and is not visible from outside.
7 // Functions for which all usages are replaced with these macros can be hidden by #defines inside our `*defs.qh` files
8 // to prevent anyone from using them accidentally in the future
9
10 // TODO stuff in the engine that uses the v_forward/v_right/v_up globals and is not wrapped yet:
11 //  - RF_USEAXIS, addentities, predraw,
12 //    - 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)
13 //    - however RF_USEAXIS is only used if MF_ROTATE is used which is only set in one place
14 //  - e.camera_transform / CL_VM_TransformView (in engine
15 //    - this is the only used function that both sets and gets the globals (aim does too but isn't used in our code)
16
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';
20
21 // convenience for deglobalized code - don't use these just to hide that globals are still used
22 #define CLEAR_V_GLOBALS() v_forward = VEC_NAN; v_right = VEC_NAN; v_up = VEC_NAN;
23 #define GET_V_GLOBALS(forward, right, up) forward = v_forward; right = v_right; up = v_up;
24 #define SET_V_GLOBALS(forward, right, up) v_forward = forward; v_right = right; v_up = up;
25
26 #ifdef GAMEQC
27 STATIC_INIT(globals) {
28         // set to NaN to more easily detect uninitialized use
29         // TODO when all functions are wrapped and the raw functions are not used anymore,
30         // uncomment the defines in *progs.qh files that hide the raw functions
31         // and assert that the global vectors are NaN before calling the raw functions here
32         // to make sure nobody (even builtins) is accidentally using them - NaN is the most likely value to expose remaining usages
33
34         // TODO make sure `isnan` actually works - potential compiler bug:
35         //LOG_INFOF("%f\n", 0.0/0.0 == 0.0/0.0);
36         //LOG_INFOF("%f\n", 0.0/0.0 != 0.0/0.0);
37         //float x = 0.0/0.0;
38         //LOG_INFOF("%f\n", x == x);
39         //LOG_INFOF("%f\n", x != x);
40
41         //float y = __builtin_nan();
42         //LOG_INFOF("%f\n", y);
43         //LOG_INFOF("%f\n", y == y);
44         //LOG_INFOF("%f\n", __builtin_isnan(y));
45
46         CLEAR_V_GLOBALS();
47 }
48 #endif
49
50 /// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals.
51 /// Always use this instead of raw `makevectors` to make the data flow clear.
52 #define MAKE_VECTORS(angles, forward, right, up) MACRO_BEGIN { \
53         _makevectors_hidden(angles); \
54         GET_V_GLOBALS(forward, right, up); \
55         CLEAR_V_GLOBALS(); \
56 } MACRO_END
57
58 /// Same as `MAKE_VECTORS` but also creates the locals for convenience.
59 #define MAKE_VECTORS_NEW(angles, forward, right, up) \
60         NEW_VECS(forward, right, up); \
61         MAKE_VECTORS(angles, forward, right, up);
62
63 /// Returns all 4 vectors by assigning to them (instead of returning a value) for consistency (and sanity)
64 #define SKEL_GET_BONE_ABS(skel, bonenum, forward, right, up, origin) MACRO_BEGIN { \
65         origin = _skel_get_boneabs_hidden(skel, bonenum) \
66         GET_V_GLOBALS(forward, right, up); \
67         CLEAR_V_GLOBALS(); \
68 } MACRO_END
69
70 #define SKEL_GET_BONE_ABS_NEW(skel, bonenum, forward, right, up, origin) \
71         NEW_VECS(forward, right, up, origin); \
72         SKEL_GET_BONE_ABS(skel, bonenum, forward, right, up, origin)
73
74 #define SKEL_SET_BONE(skel, bonenum, org, forward, right, up) MACRO_BEGIN { \
75         SET_V_GLOBALS(forward, right, up); \
76         _skel_set_bone_hidden(skel, bonenum, org); \
77         CLEAR_V_GLOBALS(); \
78 } MACRO_END
79
80 #define ADD_DYNAMIC_LIGHT(org, radius, lightcolours, forward, right, up) MACRO_BEGIN { \
81         SET_V_GLOBALS(forward, right, up); \
82         _adddynamiclight_hidden(org, radius, lightcolours); \
83         CLEAR_V_GLOBALS(); \
84 } MACRO_END
85
86 #define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \
87         _vectorvectors_hidden(forward_in); \
88         GET_V_GLOBALS(forward, right, up); \
89         CLEAR_V_GLOBALS(); \
90 } MACRO_END
91
92 #define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \
93         NEW_VECS(forward, right, up); \
94         VECTOR_VECTORS(forward_in, forward, right, up);
95
96 /// Note that this only avoids the v_* globals, not the gettaginfo_* ones
97 #define GET_TAG_INFO(ent, tagindex, forward, right, up, origin) MACRO_BEGIN {  \
98         origin = _gettaginfo_hidden(ent, tagindex); \
99         GET_V_GLOBALS(forward, right, up); \
100         CLEAR_V_GLOBALS(); \
101 } MACRO_END
102
103 #define GET_TAG_INFO_NEW(ent, tagindex, forward, right, up, origin) \
104         NEW_VECS(forward, right, up, origin); \
105         GET_TAG_INFO(ent, tagindex, forward, right, up, origin);