Flecs is a fast and lightweight Entity Component System that lets you build games and simulations with millions of entities (join the Discord!). Here are some of the framework's highlights:
- Fast and portable zero dependency C99 API
- Modern type-safe C++11 API that doesn't use STL containers
- First open source ECS with full support for Entity Relationships!
- Fast native support for hierarchies and prefabs
- Code base that builds in less than 5 seconds
- Runs in the browser without modifications with emscripten
- Cache friendly archetype/SoA storage that can process millions of entities every frame
- Supports entities with hundreds of components and applications with tens of thousands of archetypes
- Automatic component registration that works out of the box across shared libraries/DLLs
- Write free functions with queries or run code automatically in systems
- Run games on multiple CPU cores with a fast lockless scheduler
- Verified on all major compilers and platforms with CI running more than 6000 tests
- Integrated reflection framework with JSON serializer and support for runtime components
- Unit annotations for components
- Powerful query language with support for joins and inheritance
- Statistics addon for profiling ECS performance
- A web-based UI for monitoring & controlling your apps (demo, code):
To support the project, give it a star 🌟 !
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!
The Flecs playground lets you try Flecs without writing any C/C++ code!
To learn how to use the playground, check the Flecs Script Tutorial.
For a list of regularly tracked benchmarks, see the ECS Benchmark project.
C99 example:
typedef struct {
float x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
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_new_id(ecs);
ecs_set(ecs, e, Position, {10, 20});
ecs_set(ecs, e, Velocity, {1, 2});
while (ecs_progress(ecs, 0)) { }
}
Same example in C++11:
struct Position {
float x, y;
};
struct Velocity {
float x, y;
};
int main(int argc, char *argv[]) {
flecs::world ecs;
ecs.system<Position, const Velocity>()
.each([](Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
});
auto e = ecs.entity()
.set([](Position& p, Velocity& v) {
p = {10, 20};
v = {1, 2};
});
while (ecs.progress()) { }
}
If you have a project you'd like to share, let me know on Discord!
https://store.steampowered.com/app/1486920/Tempest_Rising/
https://store.steampowered.com/app/690290/Territory_Control_2/
https://github.com/ConfettiFX/The-Forge
https://nicok.itch.io/sol-survivor-demo
https://github.com/clibequilibrium/EquilibriumEngine
https://thepunkcollective.itch.io/gravitas
https://github.com/foxnne/aftersun
https://www.flecs.dev/tower_defense/etc (repository)
https://www.flecs.dev/city (repository)
- Unreal Minimum Viable Flecs Project
- Bgfx/Imgui module
- Tower defense example
- Unreal + Flecs example
- Building a space battle with Flecs in UE4
- Flecs + SDL + Web ASM example (live demo)
- Flecs + Raylib example
- Flecs + gunslinger example
- Flecs based 3D game engine with editor
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 | Component registration for cglm (math) types |
flecs.components.input | Components that describe keyboard and mouse input |
flecs.components.transform | Components that describe position, rotation and scale |
flecs.components.physics | Components that describe physics and movement |
flecs.components.geometry | Components that describe geometry |
flecs.components.graphics | Components used for computer graphics |
flecs.components.gui | Components used to describe GUI components |
flecs.systems.transform | Hierarchical transforms for scene graphs |
flecs.systems.physics | Systems for moving objects and collision detection |
flecs.systems.sokol | Sokol-based renderer |
flecs.game | Generic game systems, like a camera controller |
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!