Files
EVEngine/src/modules/scene/SceneLink.cpp
T
Cursor Agentand西风逍遥游 d80df444aa refactor(scene): register link kinds instead of hard-coding them
A scene link keeps an external object in step with the node it hangs off. The
kinds were a closed enum with a switch in TransformSystem, so scene had to
include graphics, physics and audio to do the casts, and no other module could
ever attach to the tree without editing SceneHost.h -- particles, voxel,
spritestack, avatar and weather all had to stay outside it.

Kinds are registered now (scene/SceneLink.h). The module owning the target type
supplies pushWorld / pullWorld / alive, keeps the casts on its own side where
the type is complete, and scene never learns what a Renderable3D is. Dispatch is
a function-pointer table rather than virtual calls, since link sync runs for
every dirty node every frame.

The six typed helpers (linkRenderable2D and friends) stay as conveniences and
now resolve their kind by name, so they return false rather than crash when the
module that registers it was trimmed out. The script API already named kinds
with strings, which is exactly the registry key, so it is unchanged.

Direction is now the right way round: graphics, physics and audio depend on
scene rather than scene on them, and scene drops from L5 to L1. What remains is
scene -> graphics from pickScreenAt / collectFrustumIdsAt, which take a Camera3D
and read its projection; that is recorded in the back-edge whitelist as the next
thing to move.

Each provider lives in a SceneLinks.cpp, sharing the basename so the three come
out of the build together when scene is disabled.

Co-authored-by: 西风逍遥游 <sunxfancy@gmail.com>
2026-08-18 13:46:25 +00:00

59 lines
1.4 KiB
C++

#include "scene/SceneLink.h"
#include <cstring>
#include <string>
#include <vector>
namespace eve::scene {
namespace {
struct Entry {
std::string name;
LinkOps ops;
};
/**
* Function-local so registration from another module's static initialiser
* cannot run before the table exists. Ids are indices and never reused, so a
* SceneLink's kind stays valid for the life of the process.
*/
std::vector<Entry>& table() {
static std::vector<Entry> t;
return t;
}
} // namespace
int registerLinkKind(const LinkOps& ops) {
if (!ops.kind || !ops.pushWorld) return -1;
const int existing = findLinkKind(ops.kind);
if (existing >= 0) return existing;
table().push_back({ops.kind, ops});
// Point the stored ops at our own copy of the name so a caller may pass a
// temporary.
Entry& e = table().back();
e.ops.kind = e.name.c_str();
return int(table().size()) - 1;
}
int findLinkKind(const char* kind) {
if (!kind) return -1;
const auto& t = table();
for (size_t i = 0; i < t.size(); ++i)
if (t[i].name == kind) return int(i);
return -1;
}
const LinkOps* linkOps(int kindId) {
const auto& t = table();
if (kindId < 0 || size_t(kindId) >= t.size()) return nullptr;
return &t[size_t(kindId)].ops;
}
const char* linkKindName(int kindId) {
const LinkOps* ops = linkOps(kindId);
return ops ? ops->kind : "";
}
} // namespace eve::scene