Flecs v4.0
A fast entity component system (ECS) for C & C++
|
Functions | |
bool | ecs_readonly_begin (ecs_world_t *world, bool multi_threaded) |
Begin readonly mode. | |
void | ecs_readonly_end (ecs_world_t *world) |
End readonly mode. | |
void | ecs_merge (ecs_world_t *world) |
Merge world or stage. | |
bool | ecs_defer_begin (ecs_world_t *world) |
Defer operations until end of frame. | |
bool | ecs_is_deferred (const ecs_world_t *world) |
Test if deferring is enabled for current stage. | |
bool | ecs_defer_end (ecs_world_t *world) |
End block of operations to defer. | |
void | ecs_defer_suspend (ecs_world_t *world) |
Suspend deferring but do not flush queue. | |
void | ecs_defer_resume (ecs_world_t *world) |
Resume deferring. | |
void | ecs_set_stage_count (ecs_world_t *world, int32_t stages) |
Configure world to have N stages. | |
int32_t | ecs_get_stage_count (const ecs_world_t *world) |
Get number of configured stages. | |
ecs_world_t * | ecs_get_stage (const ecs_world_t *world, int32_t stage_id) |
Get stage-specific world pointer. | |
bool | ecs_stage_is_readonly (const ecs_world_t *world) |
Test whether the current world is readonly. | |
ecs_world_t * | ecs_stage_new (ecs_world_t *world) |
Create unmanaged stage. | |
void | ecs_stage_free (ecs_world_t *stage) |
Free unmanaged stage. | |
int32_t | ecs_stage_get_id (const ecs_world_t *world) |
Get stage id. | |
bool ecs_defer_begin | ( | ecs_world_t * | world | ) |
Defer operations until end of frame.
When this operation is invoked while iterating, operations inbetween the ecs_defer_begin() and ecs_defer_end() operations are executed at the end of the frame.
This operation is thread safe.
world | The world. |
bool ecs_defer_end | ( | ecs_world_t * | world | ) |
End block of operations to defer.
See ecs_defer_begin().
This operation is thread safe.
world | The world. |
void ecs_defer_resume | ( | ecs_world_t * | world | ) |
Resume deferring.
See ecs_defer_suspend().
world | The world. |
void ecs_defer_suspend | ( | ecs_world_t * | world | ) |
Suspend deferring but do not flush queue.
This operation can be used to do an undeferred operation while not flushing the operations in the queue.
An application should invoke ecs_defer_resume() before ecs_defer_end() is called. The operation may only be called when deferring is enabled.
world | The world. |
ecs_world_t * ecs_get_stage | ( | const ecs_world_t * | world, |
int32_t | stage_id ) |
Get stage-specific world pointer.
Flecs threads can safely invoke the API as long as they have a private context to write to, also referred to as the stage. This function returns a pointer to a stage, disguised as a world pointer.
Note that this function does not(!) create a new world. It simply wraps the existing world in a thread-specific context, which the API knows how to unwrap. The reason the stage is returned as an ecs_world_t is so that it can be passed transparently to the existing API functions, vs. having to create a dedicated API for threading.
world | The world. |
stage_id | The index of the stage to retrieve. |
int32_t ecs_get_stage_count | ( | const ecs_world_t * | world | ) |
Get number of configured stages.
Return number of stages set by ecs_set_stage_count().
world | The world. |
bool ecs_is_deferred | ( | const ecs_world_t * | world | ) |
Test if deferring is enabled for current stage.
world | The world. |
void ecs_merge | ( | ecs_world_t * | world | ) |
Merge world or stage.
When automatic merging is disabled, an application can call this operation on either an individual stage, or on the world which will merge all stages. This operation may only be called when staging is not enabled (either after ecs_progress() or after ecs_readonly_end()).
This operation may be called on an already merged stage or world.
world | The world. |
bool ecs_readonly_begin | ( | ecs_world_t * | world, |
bool | multi_threaded ) |
Begin readonly mode.
This operation puts the world in readonly mode, which disallows mutations on the world. Readonly mode exists so that internal mechanisms can implement optimizations that certain aspects of the world to not change, while also providing a mechanism for applications to prevent accidental mutations in, for example, multithreaded applications.
Readonly mode is a stronger version of deferred mode. In deferred mode ECS operations such as add/remove/set/delete etc. are added to a command queue to be executed later. In readonly mode, operations that could break scheduler logic (such as creating systems, queries) are also disallowed.
Readonly mode itself has a single threaded and a multi threaded mode. In single threaded mode certain mutations on the world are still allowed, for example:
These mutations are safe in a single threaded applications, but for multithreaded applications the world needs to be entirely immutable. For this purpose multi threaded readonly mode exists, which disallows all mutations on the world. This means that in multi threaded applications, entity liveliness operations, implicit component registration, and on-the-fly query creation are not guaranteed to work.
While in readonly mode, applications can still enqueue ECS operations on a stage. Stages are managed automatically when using the pipeline addon and ecs_progress(), but they can also be configured manually as shown here:
When an attempt is made to perform an operation on a world in readonly mode, the code will throw an assert saying that the world is in readonly mode.
A call to ecs_readonly_begin() must be followed up with ecs_readonly_end(). When ecs_readonly_end() is called, all enqueued commands from configured stages are merged back into the world. Calls to ecs_readonly_begin() and ecs_readonly_end() should always happen from a context where the code has exclusive access to the world. The functions themselves are not thread safe.
In a typical application, a (non-exhaustive) call stack that uses ecs_readonly_begin() and ecs_readonly_end() will look like this:
world | The world |
multi_threaded | Whether to enable readonly/multi threaded mode. |
void ecs_readonly_end | ( | ecs_world_t * | world | ) |
End readonly mode.
This operation ends readonly mode, and must be called after ecs_readonly_begin(). Operations that were deferred while the world was in readonly mode will be flushed.
world | The world |
void ecs_set_stage_count | ( | ecs_world_t * | world, |
int32_t | stages ) |
Configure world to have N stages.
This initializes N stages, which allows applications to defer operations to multiple isolated defer queues. This is typically used for applications with multiple threads, where each thread gets its own queue, and commands are merged when threads are synchronized.
Note that the ecs_set_threads() function already creates the appropriate number of stages. The ecs_set_stage_count() operation is useful for applications that want to manage their own stages and/or threads.
world | The world. |
stages | The number of stages. |
void ecs_stage_free | ( | ecs_world_t * | stage | ) |
Free unmanaged stage.
stage | The stage to free. |
int32_t ecs_stage_get_id | ( | const ecs_world_t * | world | ) |
Get stage id.
The stage id can be used by an application to learn about which stage it is using, which typically corresponds with the worker thread id.
world | The world. |
bool ecs_stage_is_readonly | ( | const ecs_world_t * | world | ) |
Test whether the current world is readonly.
This function allows the code to test whether the currently used world is readonly or whether it allows for writing.
world | A pointer to a stage or the world. |
ecs_world_t * ecs_stage_new | ( | ecs_world_t * | world | ) |
Create unmanaged stage.
Create a stage whose lifecycle is not managed by the world. Must be freed with ecs_stage_free().
world | The world. |