]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/deglobalization.qh
37fd7e101e2c4d48fa3c2afcf95861fe11a6a185
[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 }
30 #endif
31
32 /// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals.
33 /// Always use this instead of raw `makevectors` to make the data flow clear.
34 #define MAKE_VECTORS(angles, forward, right, up) MACRO_BEGIN { \
35         makevectors(angles); \
36         forward = v_forward; \
37         right = v_right; \
38         up = v_up; \
39         v_forward = VEC_NAN; \
40         v_right = VEC_NAN; \
41         v_up = VEC_NAN; \
42 } MACRO_END
43
44 /// Same as `MAKE_VECTORS` but also creates the locals for convenience.
45 #define MAKE_VECTORS_NEW(angles, forward, right, up) \
46         vector forward = '0 0 0'; \
47         vector right = '0 0 0'; \
48         vector up = '0 0 0'; \
49         MAKE_VECTORS(angles, forward, right, up);
50
51 #define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \
52         _vectorvectors_hidden(forward_in); \
53         forward = v_forward; \
54         right = v_right; \
55         up = v_up; \
56         v_forward = VEC_NAN; \
57         v_right = VEC_NAN; \
58         v_up = VEC_NAN; \
59 } MACRO_END
60
61 #define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \
62         vector forward = '0 0 0'; \
63         vector right = '0 0 0'; \
64         vector up = '0 0 0'; \
65         VECTOR_VECTORS(forward_in, forward, right, up);