Nebula
Loading...
Searching...
No Matches
processor.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "util/stringatom.h"
11#include "filter.h"
12#include "processorid.h"
13
14namespace Game
15{
16
17class World;
18
20
22{
23public:
27 int order = 100;
29 bool async = false;
33 std::function<void(World*, Dataset::View const&)> callback;
37 bool cacheValid = false;
39 bool active = true;
40#ifdef WITH_NEBULA_EDITOR
42 bool runInEditor = false;
43#endif
44
45private:
47
48 template <typename... TYPES, std::size_t... Is>
49 static void
50 UpdateExpander(World* world, std::function<void(World*, TYPES...)> const& func, Game::Dataset::View const& view, const IndexT instance, uint8_t const bufferStartOffset, std::index_sequence<Is...>)
51 {
52 func(
53 world,
54 *((typename std::remove_const<typename std::remove_reference<TYPES>::type>::type*)view.buffers[Is] + instance)...
55 );
56 }
57
58 template <typename... COMPONENTS>
59 static std::function<void(World*, Dataset::View const&)>
60 ForEach(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
61 {
62 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
63 {
64 uint16_t i = 0;
65 while (i < view.numInstances)
66 {
67 // check validity of instances in sections of 64 instances
68 if (!view.validInstances.SectionIsNull(i / 64))
69 {
70 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
71 for (uint32_t instance = i; instance < end; ++instance)
72 {
73 // make sure the instance we're processing is valid
74 if (view.validInstances.IsSet(instance))
75 {
76 UpdateExpander<COMPONENTS...>(
77 world,
78 func,
79 view,
80 instance,
81 bufferStartOffset,
82 std::make_index_sequence<sizeof...(COMPONENTS)>()
83 );
84 }
85 }
86 }
87 // progress 64 instances, which corresponds to 1 section
88 i += 64;
89 }
90 };
91 }
92
93 template <typename... COMPONENTS>
94 static std::function<void(World*, Dataset::View const&)>
95 ForEachModified(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
96 {
97 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
98 {
99 uint16_t i = 0;
100 while (i < view.numInstances)
101 {
102
103 // check validity of instances in sections of 64 instances
104 uint64_t section = i / 64;
105 if (!view.validInstances.SectionIsNull(section) && !view.modifiedInstances.SectionIsNull(section))
106 {
107 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
108 for (uint32_t instance = i; instance < end; ++instance)
109 {
110 // make sure the instance we're processing is valid
111 if (view.validInstances.IsSet(instance) && view.modifiedInstances.IsSet(instance))
112 {
113 UpdateExpander<COMPONENTS...>(
114 world,
115 func,
116 view,
117 instance,
118 bufferStartOffset,
119 std::make_index_sequence<sizeof...(COMPONENTS)>()
120 );
121 }
122 }
123 }
124 // progress 64 instances, which corresponds to 1 section
125 i += 64;
126 }
127 };
128 }
129};
130
132{
133public:
136
138 template<typename LAMBDA>
139 ProcessorBuilder& Func(LAMBDA);
140
142 template<typename ...COMPONENTS>
143 ProcessorBuilder& Func(std::function<void(World*, COMPONENTS...)> func);
144
146 template<typename ... COMPONENTS>
148
150 template<typename ... COMPONENTS>
152
154 ProcessorBuilder& Excluding(std::initializer_list<ComponentId>);
155
158
161
164
167
170
172 Processor* Build();
173
174private:
178 std::function<void(World*, Dataset::View const&)> func = nullptr;
179 std::function<void(World*, Dataset::View const&)> funcModified = nullptr;
181 bool async = false;
182 bool onlyModified = false;
183 int order = 100;
184#ifdef WITH_NEBULA_EDITOR
185 bool runInEditor = false;
186#endif
187};
188
189//------------------------------------------------------------------------------
192template<typename LAMBDA>
194{
195 static_assert(std::is_invocable<LAMBDA()>());
196 return this->Func(std::function(lambda));
197}
198
199//------------------------------------------------------------------------------
202template<typename ...COMPONENTS>
203inline ProcessorBuilder&
204ProcessorBuilder::Func(std::function<void(World*, COMPONENTS...)> func)
205{
206 uint8_t const bufferStartOffset = this->filterBuilder.GetNumInclusive();
207 this->filterBuilder.Including<COMPONENTS...>();
208 this->func = Processor::ForEach(func, bufferStartOffset);
209 this->funcModified = Processor::ForEachModified(func, bufferStartOffset);
210 return *this;
211}
212
213//------------------------------------------------------------------------------
216template<typename ...COMPONENTS>
218{
219 this->filterBuilder.Including<COMPONENTS...>();
220 return *this;
221}
222
223//------------------------------------------------------------------------------
226template<typename ...COMPONENTS>
228{
229 this->filterBuilder.Excluding<COMPONENTS...>();
230 return *this;
231}
232
233} // namespace Game
Definition filter.h:45
Definition processor.h:132
bool async
Definition processor.h:181
ProcessorBuilder & Order(int order)
Set the sorting order for the processor.
Definition processor.cc:74
Processor * Build()
Build the processor and attach it to the world.
Definition processor.cc:96
ProcessorBuilder & OnlyModified()
entities must be marked as modified for them to actually be processed
Definition processor.cc:64
FilterBuilder filterBuilder
Definition processor.h:180
std::function< void(World *, Dataset::View const &)> funcModified
Definition processor.h:179
std::function< void(World *, Dataset::View const &)> func
Definition processor.h:178
int order
Definition processor.h:183
Util::StringAtom name
Definition processor.h:176
ProcessorBuilder & Func(LAMBDA)
which function to run with the processor
Definition processor.h:193
ProcessorBuilder & Excluding()
entities must not have any of these components
Definition processor.h:227
ProcessorBuilder & Async()
processor should run async
Definition processor.cc:54
ProcessorBuilder & Including()
entities must have these components
Definition processor.h:217
bool onlyModified
Definition processor.h:182
ProcessorBuilder & RunInEditor()
Processor should always run, even in editor; when the game is paused.
Definition processor.cc:84
ProcessorBuilder & On(Util::StringAtom eventName)
select on which event the processor is executed
Definition processor.cc:38
World * world
Definition processor.h:175
Util::StringAtom onEvent
Definition processor.h:177
Definition processor.h:22
Util::String name
name of the processor
Definition processor.h:25
friend ProcessorBuilder
Definition processor.h:46
int order
sorting order within frame event (same as batch order).
Definition processor.h:27
Filter filter
filter used for creating the dataset
Definition processor.h:31
bool active
set to false if the processor shouldn't execute in the frame.
Definition processor.h:39
static void UpdateExpander(World *world, std::function< void(World *, TYPES...)> const &func, Game::Dataset::View const &view, const IndexT instance, uint8_t const bufferStartOffset, std::index_sequence< Is... >)
Definition processor.h:50
bool cacheValid
set to false if the cache is invalid
Definition processor.h:37
bool async
set if this processor should run as a job.
Definition processor.h:29
static std::function< void(World *, Dataset::View const &)> ForEachModified(std::function< void(World *, COMPONENTS...)> func, uint8_t bufferStartOffset)
Definition processor.h:95
std::function< void(World *, Dataset::View const &)> callback
function that this processor runs
Definition processor.h:33
static std::function< void(World *, Dataset::View const &)> ForEach(std::function< void(World *, COMPONENTS...)> func, uint8_t bufferStartOffset)
Definition processor.h:60
Util::Array< MemDb::TableId > cache
cached tables that fulfill the requirements of the filter
Definition processor.h:35
A container of entities, their components, and processors.
Definition world.h:81
Nebula's dynamic array class.
Definition array.h:60
A StringAtom.
Definition stringatom.h:22
Game::EditorState.
Definition graphicsmanager.h:64
uint32_t Filter
Opaque filter identifier.
Definition filter.h:26
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:399
This represents a "view" into an entity table.
Definition dataset.h:35
Nebula's universal string class.
Definition String.cs:8
int IndexT
Definition types.h:39