entt Documentation
介绍
本项目最初是一个 ECS 系统。随着时间推移,越来越多的类和功能被不断加入,代码库也随之持续扩展。
以下是它目前所提供功能的简要(且不完整)列表:
- 内置 RTTI 系统,与标准系统大体相似。
- 用于人类可读资源名称的 constexpr 工具。
- 基于单态模式构建的最小化配置系统。
- 极速的实体-组件系统,秉持“按需付费“原则,支持不受约束的组件类型,可选指针稳定性,以及用于存储自定义的钩子机制。
- 视图与分组,用于迭代实体和组件,支持从完美 SoA 到完全随机的多种访问模式。
- 大量构建于实体-组件系统之上的实用工具,助力用户开发,避免重复造轮子。
- 通用执行图构建器,用于最优调度。
- 有史以来最小、最基础的服务定位器实现。
- 内置的、非侵入式且无宏的运行时反射系统。
- 化繁为简的静态多态,人人触手可及。
- 若干自制容器,例如基于稀疏集的哈希映射。
- 用于任意类型进程的协作式调度器。
- 资源管理所需的一切(缓存、加载器、句柄)。
- 委托、信号处理器与轻量级事件分发器。
- 基于 CRTP 惯用法的通用事件发射器类模板。
- 以及更多内容!请查阅 wiki。
请将此列表与本项目一同视为持续演进的成果。所有 API 均已在代码中完整记录,供有胆识阅读的人参考。
另请注意,目前所有工具均已对 DLL 友好,并可在边界间平滑运行。
众所周知,EnTT 也被应用于《我的世界》(Minecraft)。鉴于该游戏几乎无处不在,我可以自信地说,这个库已经在所有能想到的平台上经过了充分测试。
Code Example
#include <cmath>
#include <iostream>
auto magnitude(auto const x, auto const y, auto const z) -> double {
return std::sqrt(x * x + y * y + z * z);
}
auto main() -> int {
auto const x = 2.;
auto const y = 3.;
auto const z = 5.;
std::cout << "The magnitude of the vector is "
<< magnitude(x, y, z)
<< "units.\n";
return 0;
}
#include <entt/entt.hpp>
struct position {
float x;
float y;
};
struct velocity {
float dx;
float dy;
};
void update(entt::registry ®istry) {
auto view = registry.view<const position, velocity>();
// use a callback
view.each([](const auto &pos, auto &vel) { /* ... */ });
// use an extended callback
view.each([](const auto entity, const auto &pos, auto &vel) { /* ... */ });
// use a range-for
for(auto [entity, pos, vel]: view.each()) {
// ...
}
// use forward iterators and get only the components of interest
for(auto entity: view) {
auto &vel = view.get<velocity>(entity);
// ...
}
}
int main() {
entt::registry registry;
for(auto i = 0u; i < 10u; ++i) {
const auto entity = registry.create();
registry.emplace<position>(entity, i * 1.f, i * 1.f);
if(i % 2 == 0) { registry.emplace<velocity>(entity, i * .1f, i * .1f); }
}
update(registry);
}