https://github.com/SanderMertens/flecs
A fast entity component system (ECS) for C & C++
https://github.com/SanderMertens/flecs
c99 cpp11 cpp14 cpp17 data-oriented data-oriented-design ecs entity-component-system entity-framework flecs game-development game-engine game-engine-framework gamedev modern-cpp no-dependencies portable production-ready
Last synced: 10 months ago
JSON representation
A fast entity component system (ECS) for C & C++
- Host: GitHub
- URL: https://github.com/SanderMertens/flecs
- Owner: SanderMertens
- License: other
- Created: 2018-08-26T05:53:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-19T01:09:34.000Z (about 1 year ago)
- Last Synced: 2024-10-29T14:56:20.382Z (about 1 year ago)
- Topics: c99, cpp11, cpp14, cpp17, data-oriented, data-oriented-design, ecs, entity-component-system, entity-framework, flecs, game-development, game-engine, game-engine-framework, gamedev, modern-cpp, no-dependencies, portable, production-ready
- Language: C
- Homepage: https://www.flecs.dev
- Size: 131 MB
- Stars: 6,410
- Watchers: 82
- Forks: 445
- Open Issues: 34
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-c - Flecs - A Multithreaded Entity Component System written for C89 & C99 [MIT](https://spdx.org/licenses/MIT.html) (Game Programming)
- anything_about_game - github
- programming-awesome-list - flecs - A fast entity component system (ECS) for C & C++ (Libraries & Frameworks: / ECS)
- StarryDivineSky - SanderMertens/flecs - 组件-系统架构模式,通过分离数据(组件)与逻辑(系统)实现灵活高效的数据管理。ECS 是一种组织代码和数据的方式,它允许你构建更大、更复杂且更易于扩展的游戏。项目以内存池优化为核心,通过预分配内存块减少动态内存分配开销,结合无锁多线程支持实现高并发性能,特别适合游戏开发、物理仿真等对实时性要求高的场景。其核心特性包括基于宏的组件定义系统,允许开发者通过简单注解自动生成组件结构和管理代码,同时支持C++模板实现类型安全的组件操作。框架提供跨平台兼容性,支持Windows、Linux和macOS系统,且无需依赖第三方库即可编译运行。通过分离实体标识与组件数据,系统能实现高效的实体-组件映射,配合基于事件的系统调度机制,使复杂逻辑模块化。开发者可自定义组件类型和系统行为,结合反射机制实现运行时组件管理,同时通过内存池优化和缓存友好数据布局,显著提升CPU缓存利用率。项目特别强调性能与灵活性的平衡,既可通过C语言实现轻量级使用,也支持C++11及以上版本的面向对象特性扩展,适用于需要高性能数据处理的多种应用场景。 (C/C++程序设计 / 资源传输下载)
- awesome-c-zh - Flecs - 为C89和C99编写的多线程实体组件系统[](https://spdx.org/licenses/MIT.html) (游戏编程)
- awesome-c - Flecs - A Multithreaded Entity Component System written for C89 & C99 [MIT](https://spdx.org/licenses/MIT.html) (Game Programming)
- awesome-game-engine-dev - Flecs - Fast and lightweight entity component system in C99. (Libraries / C)
- awesomecpp - flecs - - fast and lightweight Entity Component System with a focus on high performance game development. (Containers and Algorithms)
- AwesomeCppGameDev - flecs
README

[](https://github.com/SanderMertens/flecs/releases)
[](https://github.com/SanderMertens/flecs/blob/master/LICENSE)
[](https://www.flecs.dev/flecs/md_docs_2Docs.html)
[](https://github.com/SanderMertens/flecs/actions?query=workflow%3ACI)
[](https://discord.gg/BEzP5Rgrrp)
Flecs is a fast and lightweight Entity Component System that lets you build games and simulations with millions of entities ([join the Discord!](https://discord.gg/BEzP5Rgrrp)). Here are some of the framework's highlights:
- Fast and [portable](#language-bindings) zero dependency [C99 API](https://www.flecs.dev/flecs/group__c.html)
- Modern type-safe [C++11 API](https://www.flecs.dev/flecs/group__cpp.html) that doesn't use STL containers
- First open source ECS with full support for [Entity Relationships](https://www.flecs.dev/flecs/md_docs_2Relationships.html)!
- Fast native support for [hierarchies](https://www.flecs.dev/flecs/md_docs_2Relationships.html#the-childof-relationship) and [prefabs](https://www.flecs.dev/flecs/md_docs_2Relationships.html#the-isa-relationship)
- Code base that builds in less than 5 seconds
- Runs [in the browser](https://flecs.dev/city) without modifications with emscripten
- Cache friendly [archetype/SoA storage](https://ajmmertens.medium.com/building-an-ecs-2-archetypes-and-vectorization-fe21690805f9) that can process millions of entities every frame
- Automatic component registration that works out of the box across shared libraries/DLLs
- Write free functions with [queries](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/basics) or run code automatically in [systems](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/systems/pipeline)
- Run games on multiple CPU cores with a fast lockless scheduler
- Verified on all major compilers and platforms with [CI](https://github.com/SanderMertens/flecs/actions) running more than 8000 tests
- Integrated [reflection framework](https://www.flecs.dev/flecs/group__c__addons__meta.html) with [JSON serializer](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/basics_json) and support for [runtime components](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/runtime_component)
- [Unit annotations](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/reflection/units) for components
- Powerful [query language](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries) with support for [joins](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/setting_variables) and [inheritance](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/queries/component_inheritance)
- [Statistics addon](https://www.flecs.dev/flecs/group__c__addons__stats.html) for profiling ECS performance
- A web-based UI for monitoring & controlling your apps:
[](https://flecs.dev/explorer)
To support the project, give it a star 🌟 !
## What is an Entity Component System?
ECS is a way of organizing code and data that lets you build games that are larger, more complex and are easier to extend. Something is called an ECS when it:
- Has _entities_ that uniquely identify objects in a game
- Has _components_ which are datatypes that can be added to entities
- Has _systems_ which are functions that run for all entities matching a component _query_
For more information, check the [ECS FAQ](https://github.com/SanderMertens/ecs-faq)!
## Show me the code!
C99 example:
```c
typedef struct {
float x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);
for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init();
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, Velocity);
ecs_entity_t e = ecs_insert(ecs,
ecs_value(Position, {10, 20}),
ecs_value(Velocity, {1, 2}));
while (ecs_progress(ecs, 0)) { }
}
```
Same example in C++11:
```cpp
struct Position {
float x, y;
};
struct Velocity {
float x, y;
};
int main(int argc, char *argv[]) {
flecs::world ecs;
ecs.system()
.each([](Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
});
auto e = ecs.entity()
.insert([](Position& p, Velocity& v) {
p = {10, 20};
v = {1, 2};
});
while (ecs.progress()) { }
}
```
## Projects using Flecs
If you have a project you'd like to share, let me know on [Discord](https://discord.gg/BEzP5Rgrrp)!
### [Hytale](https://hytale.com/)
[](https://hytale.com/)
> We knew that we wanted to build Hytale around an Entity-Component-System (ECS). When we analyzed the options, FLECS rose to the top. FLECS provides the backbone of the Hytale Game Engine. Its flexibility has allowed us to build highly varied gameplay while supporting our vision for empowering Creators.
-- Dann Webster, Hypixel studios
### Tempest Rising
[](https://store.steampowered.com/app/1486920/Tempest_Rising/)
### Territory Control 2
[](https://store.steampowered.com/app/690290/Territory_Control_2/)
### The Forge
[](https://github.com/ConfettiFX/The-Forge)
### Extermination Shock
[](https://store.steampowered.com/app/2510820/Extermination_Shock/)
### Tome Tumble Tournament
[](https://terzalo.itch.io/tome-tumble-tournament)
### Sol Survivor
[](https://nicok.itch.io/sol-survivor-demo)
### Equilibrium Engine
[](https://github.com/clibequilibrium/EquilibriumEngine)
### After Sun
[](https://github.com/foxnne/aftersun)
### Flecs Demo's
https://github.com/SanderMertens/tower_defense
[](https://www.flecs.dev/tower_defense/etc)
https://github.com/flecs-hub/city
[](https://www.flecs.dev/city)
## Flecs Hub
[Flecs Hub](https://github.com/flecs-hub) is a collection of repositories that show how Flecs can be used to build game systems like input handling, hierarchical transforms and rendering.
Module | Description
------------|------------------
[flecs.components.cglm](https://github.com/flecs-hub/flecs-components-cglm) | Component registration for cglm (math) types
[flecs.components.input](https://github.com/flecs-hub/flecs-components-input) | Components that describe keyboard and mouse input
[flecs.components.transform](https://github.com/flecs-hub/flecs-components-transform) | Components that describe position, rotation and scale
[flecs.components.physics](https://github.com/flecs-hub/flecs-components-physics) | Components that describe physics and movement
[flecs.components.geometry](https://github.com/flecs-hub/flecs-components-geometry) | Components that describe geometry
[flecs.components.graphics](https://github.com/flecs-hub/flecs-components-graphics) | Components used for computer graphics
[flecs.components.gui](https://github.com/flecs-hub/flecs-components-gui) | Components used to describe GUI components
[flecs.systems.transform](https://github.com/flecs-hub/flecs-systems-transform) | Hierarchical transforms for scene graphs
[flecs.systems.physics](https://github.com/flecs-hub/flecs-systems-physics) | Systems for moving objects and collision detection
[flecs.systems.sokol](https://github.com/flecs-hub/flecs-systems-sokol) | Sokol-based renderer
[flecs.game](https://github.com/flecs-hub/flecs-game) | Generic game systems, like a camera controller
## Language bindings
The following language bindings have been developed with Flecs! Note that these are projects built and maintained by helpful community members, and may not always be up to date with the latest commit from master!
- C#:
- [BeanCheeseBurrito/Flecs.NET](https://github.com/BeanCheeseBurrito/Flecs.NET)
- Rust:
- [Flecs-Rust](https://github.com/Indra-db/Flecs-Rust)
- [flecs-polyglot](https://github.com/flecs-hub/flecs-polyglot)
- [flecs-rs](https://github.com/jazzay/flecs-rs)
- Zig:
- [michal-z/zig-gamedev](https://github.com/michal-z/zig-gamedev/tree/main/libs/zflecs)
- [foxnne/zig-flecs](https://github.com/foxnne/zig-flecs)
- [prime31/zig-flecs](https://github.com/prime31/zig-flecs)
- Lua:
- [sro5h/flecs-luajit](https://github.com/sro5h/flecs-luajit)
- [flecs-hub/flecs-lua](https://github.com/flecs-hub/flecs-lua)