Tylder / flecs

A fast entity component system (ECS) for C & C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

flecs

CI build codecov Discord Chat Try online Documentation

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:

Screen Shot 2022-04-13 at 11 43 43 AM

Flecs v3 is the latest, most stable and feature rich version of Flecs, and recommended for new projects.

Last v3 release: Flecs v3.0.1-alpha.

Last v2 release: Flecs v2.4.8.

What is an Entity Component System?

ECS is a new 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 example, a game has a Move system that has a query with two components, Position, Velocity. When the system is ran it is dynamically matched with all entities that have at least these two components.

For more info on ECS, check the ECS FAQ!

Getting Started

Show me the code!

C99 example:

typedef struct {
  float x, y;
} Position, Velocity;

void Move(ecs_iter_t *it) {
  Position *p = ecs_term(it, Position, 1);
  Velocity *v = ecs_term(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()) { }
}

Community content

Examples and tutorials contributed by the community ❤️

Addons

Flecs has a modular architecture that makes it easy to only build the features you really need. By default all addons are built. To customize a build, first define FLECS_CUSTOM_BUILD, then add defines for the addons you need. For example:

#define FLECS_CUSTOM_BUILD  // Don't build all addons
#define FLECS_SYSTEM        // Build FLECS_SYSTEM

Additionally, you can also specify addons to exclude from a build by adding NO to the define:

#define FLECS_NO_LOG

The following addons can be configured:

Addon Description Define
Cpp C++11 API FLECS_CPP
Module Organize game logic into reusable modules FLECS_MODULE
System Create & run systems FLECS_SYSTEM
Pipeline Automatically schedule & multithread systems FLECS_PIPELINE
Timer Run systems at time intervals or at a rate FLECS_TIMER
Meta Flecs reflection system FLECS_META
Units Builtin unit types FLECS_UNITS
Meta_C (C) Utilities for auto-inserting reflection data FLECS_META_C
Expr String format optimized for ECS data FLECS_EXPR
JSON JSON format FLECS_JSON
Doc Add documentation to components, systems & more FLECS_DOC
Coredoc Documentation for builtin components & modules FLECS_COREDOC
Http Tiny HTTP server for processing simple requests FLECS_HTTP
Rest REST API for showing entities in the browser FLECS_REST
Parser Create entities & queries from strings FLECS_PARSER
Plecs Small utility language for asset/scene loading FLECS_PLECS
Rules Powerful prolog-like query language FLECS_RULES
Snapshot Take snapshots of the world & restore them FLECS_SNAPSHOT
Stats See what's happening in a world with statistics FLECS_STATS
Log Extended tracing and error logging FLECS_LOG
App Flecs application framework FLECS_APP
OS API Impl Default OS API implementation for Posix/Win32 FLECS_OS_API_IMPL

Flecs Hub

Flecs Hub is a handy collection of repositories built with Flecs that showcase basic ways of how to build engine features like input handling, transformations 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.sdl2 SDL window creation & input management
flecs.systems.sokol Sokol-based renderer

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!

Links

Supporting Flecs ♥️

Supporting Flecs goes a long way towards keeping the project going and the community alive! If you like the project, consider:

About

A fast entity component system (ECS) for C & C++

License:MIT License


Languages

Language:C 80.1%Language:C++ 19.8%Language:CMake 0.1%Language:Starlark 0.0%Language:Meson 0.0%