]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/_lib.inc
Merge branch 'master' into terencehill/infomessages_panel_update
[xonotic/xonotic-data.pk3dir.git] / qcsrc / ecs / _lib.inc
1 /** Components always interpolate from the previous state */
2 #define COMPONENT(com) \
3         void com_##com##_interpolate(entity it, float a); \
4         .bool com_##com
5
6 #define FOREACH_COMPONENT(com, body) FOREACH_ENTITY_FLOAT(com_##com, true, body)
7
8
9 #define EVENT(T, args) .bool evt_##T##_listener; .void args evt_##T
10
11 #define emit(T, ...) \
12         MACRO_BEGIN \
13         FOREACH_ENTITY_FLOAT_ORDERED(evt_##T##_listener, true, it.evt_##T(__VA_ARGS__)); \
14         MACRO_END
15
16 #define subscribe(listener, T) \
17         MACRO_BEGIN \
18         listener.evt_##T##_listener = true; \
19         MACRO_END
20
21
22 /**
23  * framelimit 0 is no limit, interpolation does not apply
24  * framerate below minfps will result in less than 100% speed
25  */
26 #define SYSTEM(sys, frameLimit, minfps) \
27         void sys_##sys##_update(entity this, float dt); \
28         float autocvar_xon_sys_##sys##_dt = ((frameLimit) ? (1 / (frameLimit)) : 0); \
29         float autocvar_xon_sys_##sys##_minfps = (1 / (1 / (minfps)))
30
31 #define SYSTEM_UPDATE(sys) \
32         MACRO_BEGIN \
33         static float t = 0; \
34         float dt = autocvar_xon_sys_##sys##_dt; \
35         float minfps = autocvar_xon_sys_##sys##_minfps; \
36         static float accumulator = 0; \
37         float a = 0; \
38         if (dt) { \
39                 accumulator += min(frametime, 1 / (minfps)); \
40         } else { \
41                 accumulator += frametime; \
42                 dt = accumulator; \
43                 a = 1; \
44         } \
45         while (accumulator >= dt) \
46         { \
47                 time = t; \
48                 FOREACH_COMPONENT(sys, sys_##sys##_update(it, dt)); \
49                 t += dt; \
50                 accumulator -= dt; \
51         } \
52         if (!a) a = accumulator / dt; \
53         FOREACH_COMPONENT(sys, com_##sys##_interpolate(it, a)); \
54         MACRO_END
55
56
57 #include "_mod.inc"
58 #include "components/_mod.inc"
59 #include "events/_mod.inc"
60 #include "systems/_mod.inc"