Flecs: include/flecs/addons/cpp/mixins/system/impl.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
13 struct system_runner_fluent {
14 system_runner_fluent(
15 world_t *world,
16 entity_t id,
17 int32_t stage_current,
18 int32_t stage_count,
19 ecs_ftime_t delta_time,
20 void *param)
21 : stage_(world)
22 , id_(id)
23 , delta_time_(delta_time)
24 , param_(param)
25 , stage_current_(stage_current)
26 , stage_count_(stage_count) { }
27
28 system_runner_fluent& offset(int32_t offset) {
29 offset_ = offset;
30 return *this;
31 }
32
33 system_runner_fluent& limit(int32_t limit) {
34 limit_ = limit;
35 return *this;
36 }
37
38 system_runner_fluent& stage(flecs::world& stage) {
39 stage_ = stage.c_ptr();
40 return *this;
41 }
42
43 ~system_runner_fluent() {
44 if (stage_count_) {
45 ecs_run_worker(
46 stage_, id_, stage_current_, stage_count_, delta_time_,
47 param_);
48 } else {
49 ecs_run(stage_, id_, delta_time_, param_);
50 }
51 }
52
53private:
54 world_t *stage_;
55 entity_t id_;
56 ecs_ftime_t delta_time_;
57 void *param_;
58 int32_t offset_;
59 int32_t limit_;
60 int32_t stage_current_;
61 int32_t stage_count_;
62};
63
64 struct system final : entity
65{
66 using entity::entity;
67
68 explicit system() {
69 id_ = 0;
70 world_ = nullptr;
71 }
72
73 explicit system(flecs::world_t *world, ecs_system_desc_t *desc) {
74 world_ = world;
75 id_ = ecs_system_init(world, desc);
76 }
77
78 void ctx(void *ctx) {
79 ecs_system_desc_t desc = {};
80 desc.entity = id_;
81 desc.ctx = ctx;
82 ecs_system_init(world_, &desc);
83 }
84
85 void* ctx() const {
86 return ecs_system_get(world_, id_)->ctx;
87 }
88
89 flecs::query<> query() const {
90 return flecs::query<>(ecs_system_get(world_, id_)->query);
91 }
92
93 system_runner_fluent run(ecs_ftime_t delta_time = 0.0f, void *param = nullptr) const {
94 return system_runner_fluent(world_, id_, 0, 0, delta_time, param);
95 }
96
97 system_runner_fluent run_worker(
98 int32_t stage_current,
99 int32_t stage_count,
100 ecs_ftime_t delta_time = 0.0f,
101 void *param = nullptr) const
102 {
103 return system_runner_fluent(
104 world_, id_, stage_current, stage_count, delta_time, param);
105 }
106
107# ifdef FLECS_TIMER
108# include "../timer/system_mixin.inl"
109# endif
110
111};
112
113// Mixin implementation
114 inline system world::system(flecs::entity e) const {
115 return flecs::system(world_, e);
116}
117
118template <typename... Comps, typename... Args>
119inline system_builder<Comps...> world::system(Args &&... args) const {
120 return flecs::system_builder<Comps...>(world_, FLECS_FWD(args)...);
121}
122
123namespace _ {
124
125inline void system_init(flecs::world& world) {
126 world.component<TickSource>("flecs::system::TickSource");
127}
128
129} // namespace _
130
131template <typename ... Components>
132template <typename Func>
133inline system system_builder<Components...>::each(Func&& func) {
134 // Faster version of each() that iterates the query on the C++ side.
135 return this->run_each(FLECS_FWD(func));
136}
137
138} // namespace flecs
ecs_system_get
FLECS_API const ecs_system_t * ecs_system_get(const ecs_world_t *world, ecs_entity_t system)
Get system object.
ecs_run
FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
Run a specific system manually.
ecs_system_init
FLECS_API ecs_entity_t ecs_system_init(ecs_world_t *world, const ecs_system_desc_t *desc)
Create a system.
ecs_run_worker
FLECS_API ecs_entity_t ecs_run_worker(ecs_world_t *world, ecs_entity_t system, int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time, void *param)
Same as ecs_run(), but subdivides entities across number of provided stages.
flecs::world::system
flecs::system system(flecs::entity e) const
Upcast entity to a system.
Definition impl.hpp:114
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register component.
Definition impl.hpp:11
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
builder.hpp
System builder.
ecs_system_desc_t
Use with ecs_system_init() to create or update a system.
Definition system.h:38
ecs_system_desc_t::ctx
void * ctx
Context to be passed to callback (as ecs_iter_t::param)
Definition system.h:66
ecs_system_desc_t::entity
ecs_entity_t entity
Existing entity to associate with system (optional)
Definition system.h:42
ecs_system_t::ctx
void * ctx
Userdata for system.
Definition system.h:132
flecs::entity
Entity.
Definition entity.hpp:30
flecs::query
Definition impl.hpp:196
flecs::system_builder
System builder.
Definition builder.hpp:24
flecs::system_runner_fluent
Definition impl.hpp:13
flecs::system
Definition impl.hpp:65
flecs::world
The world.
Definition world.hpp:158
system_mixin.inl
Timer module system mixin.