]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/deglobalization.qh
explain MR_ROTATE
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / deglobalization.qh
1 #include "lib/misc.qh"
2 #include "lib/static.qh"
3 #include "lib/vector.qh"
4
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.
8
9 // TODO stuff in the engine that uses the v_forward/v_right/v_up globals and is not wrapped:
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 //  - e.camera_transform / CL_VM_TransformView (in engine)
13 //  - adddynamiclight
14 //  - makestatic
15 //  - gettaginfo
16 //  - getentity
17 //  - skel_get_bonerel, skel_get_boneabs, skel_set_bone, skel_mul_bone, skel_mul_bones
18 //  - aim
19
20 #ifdef GAMEQC
21 STATIC_INIT(globals) {
22         // set to NaN to more easily detect uninitialized use
23     // TODO when all functions are wrapped and the raw functions are not used anymore,
24     // assert that the global vectors are NaN before calling the raw functions
25     // to make sure nobody (even builtins) is accidentally using them - NaN is the most likely value to expose remaining usages
26         v_forward = VEC_NAN;
27         v_right = VEC_NAN;
28         v_up = VEC_NAN;
29         // FIXME check this is actually NaN
30 }
31 #endif
32
33 /// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals.
34 /// Always use this instead of raw `makevectors` to make the data flow clear.
35 #define MAKE_VECTORS(angles, forward, right, up) MACRO_BEGIN { \
36         makevectors(angles); \
37         forward = v_forward; \
38         right = v_right; \
39         up = v_up; \
40         v_forward = VEC_NAN; \
41         v_right = VEC_NAN; \
42         v_up = VEC_NAN; \
43 } MACRO_END
44
45 /// Same as `MAKE_VECTORS` but also creates the locals for convenience.
46 #define MAKE_VECTORS_NEW(angles, forward, right, up) \
47         vector forward = '0 0 0'; \
48         vector right = '0 0 0'; \
49         vector up = '0 0 0'; \
50         MAKE_VECTORS(angles, forward, right, up);
51
52 #define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \
53         _vectorvectors_hidden(forward_in); \
54         forward = v_forward; \
55         right = v_right; \
56         up = v_up; \
57         v_forward = VEC_NAN; \
58         v_right = VEC_NAN; \
59         v_up = VEC_NAN; \
60 } MACRO_END
61
62 #define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \
63         vector forward = '0 0 0'; \
64         vector right = '0 0 0'; \
65         vector up = '0 0 0'; \
66         VECTOR_VECTORS(forward_in, forward, right, up);