Nebula
Loading...
Searching...
No Matches
processor.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "timing/timer.h"
11#include "util/stringatom.h"
12#include "filter.h"
13#include "processorid.h"
14
15namespace Game
16{
17
18class World;
19
21
23{
24public:
28 int order = 100;
30 bool async = false;
34 std::function<void(World*, Dataset::View const&)> callback;
38 bool cacheValid = false;
40 bool active = true;
41#ifdef NEBULA_ENABLE_PROFILING
43 Timing::Timer timer;
44#endif
45#ifdef WITH_NEBULA_EDITOR
47 bool runInEditor = false;
48#endif
49
50private:
52
53 template <typename... TYPES, std::size_t... Is>
54 static void
55 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...>)
56 {
57 func(
58 world,
59 *((typename std::remove_const<typename std::remove_reference<TYPES>::type>::type*)view.buffers[Is] + instance)...
60 );
61 }
62
63 template <typename... COMPONENTS>
64 static std::function<void(World*, Dataset::View const&)>
65 ForEach(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
66 {
67 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
68 {
69 uint16_t i = 0;
70 while (i < view.numInstances)
71 {
72 // check validity of instances in sections of 64 instances
73 if (!view.validInstances.SectionIsNull(i / 64))
74 {
75 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
76 for (uint32_t instance = i; instance < end; ++instance)
77 {
78 // make sure the instance we're processing is valid
79 if (view.validInstances.IsSet(instance))
80 {
81 UpdateExpander<COMPONENTS...>(
82 world,
83 func,
84 view,
85 instance,
86 bufferStartOffset,
87 std::make_index_sequence<sizeof...(COMPONENTS)>()
88 );
89 }
90 }
91 }
92 // progress 64 instances, which corresponds to 1 section
93 i += 64;
94 }
95 };
96 }
97
98 template <typename... COMPONENTS>
99 static std::function<void(World*, Dataset::View const&)>
100 ForEachModified(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
101 {
102 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
103 {
104 uint16_t i = 0;
105 while (i < view.numInstances)
106 {
107
108 // check validity of instances in sections of 64 instances
109 uint64_t section = i / 64;
110 if (!view.validInstances.SectionIsNull(section) && !view.modifiedInstances.SectionIsNull(section))
111 {
112 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
113 for (uint32_t instance = i; instance < end; ++instance)
114 {
115 // make sure the instance we're processing is valid
116 if (view.validInstances.IsSet(instance) && view.modifiedInstances.IsSet(instance))
117 {
118 UpdateExpander<COMPONENTS...>(
119 world,
120 func,
121 view,
122 instance,
123 bufferStartOffset,
124 std::make_index_sequence<sizeof...(COMPONENTS)>()
125 );
126 }
127 }
128 }
129 // progress 64 instances, which corresponds to 1 section
130 i += 64;
131 }
132 };
133 }
134};
135
137{
138public:
141
143 template<typename LAMBDA>
144 ProcessorBuilder& Func(LAMBDA);
145
147 template<typename ...COMPONENTS>
148 ProcessorBuilder& Func(std::function<void(World*, COMPONENTS...)> func);
149
151 template<typename ... COMPONENTS>
153
155 template<typename ... COMPONENTS>
157
159 ProcessorBuilder& Excluding(std::initializer_list<ComponentId>);
160
163
166
169
172
175
177 Processor* Build();
178
179private:
183 std::function<void(World*, Dataset::View const&)> func = nullptr;
184 std::function<void(World*, Dataset::View const&)> funcModified = nullptr;
186 bool async = false;
187 bool onlyModified = false;
188 int order = 100;
189#ifdef WITH_NEBULA_EDITOR
190 bool runInEditor = false;
191#endif
192};
193
194//------------------------------------------------------------------------------
197template<typename LAMBDA>
199{
200 static_assert(std::is_invocable<LAMBDA()>());
201 return this->Func(std::function(lambda));
202}
203
204//------------------------------------------------------------------------------
207template<typename ...COMPONENTS>
208inline ProcessorBuilder&
209ProcessorBuilder::Func(std::function<void(World*, COMPONENTS...)> func)
210{
211 uint8_t const bufferStartOffset = this->filterBuilder.GetNumInclusive();
212 this->filterBuilder.Including<COMPONENTS...>();
213 this->func = Processor::ForEach(func, bufferStartOffset);
214 this->funcModified = Processor::ForEachModified(func, bufferStartOffset);
215 return *this;
216}
217
218//------------------------------------------------------------------------------
221template<typename ...COMPONENTS>
223{
224 this->filterBuilder.Including<COMPONENTS...>();
225 return *this;
226}
227
228//------------------------------------------------------------------------------
231template<typename ...COMPONENTS>
233{
234 this->filterBuilder.Excluding<COMPONENTS...>();
235 return *this;
236}
237
238} // namespace Game
Definition filter.h:45
Definition processor.h:137
bool async
Definition processor.h:186
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:185
std::function< void(World *, Dataset::View const &)> funcModified
Definition processor.h:184
std::function< void(World *, Dataset::View const &)> func
Definition processor.h:183
int order
Definition processor.h:188
Util::StringAtom name
Definition processor.h:181
ProcessorBuilder & Func(LAMBDA)
which function to run with the processor
Definition processor.h:198
ProcessorBuilder & Excluding()
entities must not have any of these components
Definition processor.h:232
ProcessorBuilder & Async()
processor should run async
Definition processor.cc:54
ProcessorBuilder & Including()
entities must have these components
Definition processor.h:222
bool onlyModified
Definition processor.h:187
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:180
Util::StringAtom onEvent
Definition processor.h:182
Definition processor.h:23
Util::String name
name of the processor
Definition processor.h:26
friend ProcessorBuilder
Definition processor.h:51
int order
sorting order within frame event (same as batch order).
Definition processor.h:28
Filter filter
filter used for creating the dataset
Definition processor.h:32
bool active
set to false if the processor shouldn't execute in the frame.
Definition processor.h:40
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:55
bool cacheValid
set to false if the cache is invalid
Definition processor.h:38
bool async
set if this processor should run as a job.
Definition processor.h:30
static std::function< void(World *, Dataset::View const &)> ForEachModified(std::function< void(World *, COMPONENTS...)> func, uint8_t bufferStartOffset)
Definition processor.h:100
std::function< void(World *, Dataset::View const &)> callback
function that this processor runs
Definition processor.h:34
static std::function< void(World *, Dataset::View const &)> ForEach(std::function< void(World *, COMPONENTS...)> func, uint8_t bufferStartOffset)
Definition processor.h:65
Util::Array< MemDb::TableId > cache
cached tables that fulfill the requirements of the filter
Definition processor.h:36
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