-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
voxel_engine_updater.cpp
62 lines (52 loc) · 1.65 KB
/
voxel_engine_updater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "voxel_engine_updater.h"
#include "../util/io/log.h"
#include "voxel_engine.h"
// Needed for doing `Node *root = SceneTree::get_root()`, Window* is forward-declared
#include "../util/godot/classes/scene_tree.h"
#include "../util/godot/classes/window.h"
namespace zylann::voxel {
bool g_updater_created = false;
VoxelEngineUpdater::VoxelEngineUpdater() {
ZN_PRINT_VERBOSE("Creating VoxelEngineUpdater");
set_process(true);
// We don't want it to stop when the scene tree is paused
set_process_mode(PROCESS_MODE_ALWAYS);
g_updater_created = true;
}
VoxelEngineUpdater::~VoxelEngineUpdater() {
g_updater_created = false;
}
void VoxelEngineUpdater::ensure_existence(SceneTree *st) {
if (st == nullptr) {
return;
}
if (g_updater_created) {
return;
}
Node *root = st->get_root();
for (int i = 0; i < root->get_child_count(); ++i) {
VoxelEngineUpdater *u = Object::cast_to<VoxelEngineUpdater>(root->get_child(i));
if (u != nullptr) {
return;
}
}
VoxelEngineUpdater *u = memnew(VoxelEngineUpdater);
u->set_name("VoxelEngineUpdater_dont_touch_this");
// TODO This can fail (for example if `Node::data.blocked > 0` while in `_ready()`) but Godot offers no API to check
// anything. So if this fail, the node will leak.
root->add_child(u);
}
void VoxelEngineUpdater::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_PROCESS:
// To workaround the absence of API to have a custom server processing in the main loop
zylann::voxel::VoxelEngine::get_singleton().process();
break;
case NOTIFICATION_PREDELETE:
ZN_PRINT_VERBOSE("Deleting VoxelEngineUpdater");
break;
default:
break;
}
}
} // namespace zylann::voxel