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
19class ProcessorBuilder;
20
22{
23public:
27 int order = 100;
29 bool async = false;
33 std::function<void(World*, Dataset::View const&)> callback;
37 bool cacheValid = false;
38#ifdef WITH_NEBULA_EDITOR
40 bool runInEditor = false;
41#endif
42
43private:
45
46 template <typename... TYPES, std::size_t... Is>
47 static void
48 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...>)
49 {
50 func(
51 world,
52 *((typename std::remove_const<typename std::remove_reference<TYPES>::type>::type*)view.buffers[Is] + instance)...
53 );
54 }
55
56 template <typename... COMPONENTS>
57 static std::function<void(World*, Dataset::View const&)>
58 ForEach(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
59 {
60 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
61 {
62 uint16_t i = 0;
63 while (i < view.numInstances)
64 {
65 // check validity of instances in sections of 64 instances
66 if (!view.validInstances.SectionIsNull(i / 64))
67 {
68 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
69 for (uint32_t instance = i; instance < end; ++instance)
70 {
71 // make sure the instance we're processing is valid
72 if (view.validInstances.IsSet(instance))
73 {
74 UpdateExpander<COMPONENTS...>(
75 world,
76 func,
77 view,
78 instance,
79 bufferStartOffset,
80 std::make_index_sequence<sizeof...(COMPONENTS)>()
81 );
82 }
83 }
84 }
85 // progress 64 instances, which corresponds to 1 section
86 i += 64;
87 }
88 };
89 }
90
91 template <typename... COMPONENTS>
92 static std::function<void(World*, Dataset::View const&)>
93 ForEachModified(std::function<void(World*, COMPONENTS...)> func, uint8_t bufferStartOffset)
94 {
95 return [func, bufferStartOffset](World* world, Game::Dataset::View const& view)
96 {
97 uint16_t i = 0;
98 while (i < view.numInstances)
99 {
100
101 // check validity of instances in sections of 64 instances
102 uint64_t section = i / 64;
103 if (!view.validInstances.SectionIsNull(section) && !view.modifiedInstances.SectionIsNull(section))
104 {
105 uint16_t const end = Math::min<uint16_t>(i + uint16_t(64), view.numInstances);
106 for (uint32_t instance = i; instance < end; ++instance)
107 {
108 // make sure the instance we're processing is valid
109 if (view.validInstances.IsSet(instance) && view.modifiedInstances.IsSet(instance))
110 {
111 UpdateExpander<COMPONENTS...>(
112 world,
113 func,
114 view,
115 instance,
116 bufferStartOffset,
117 std::make_index_sequence<sizeof...(COMPONENTS)>()
118 );
119 }
120 }
121 }
122 // progress 64 instances, which corresponds to 1 section
123 i += 64;
124 }
125 };
126 }
127};
128
130{
131public:
134
136 template<typename LAMBDA>
137 ProcessorBuilder& Func(LAMBDA);
138
140 template<typename ...COMPONENTS>
141 ProcessorBuilder& Func(std::function<void(World*, COMPONENTS...)> func);
142
144 template<typename ... COMPONENTS>
146
148 template<typename ... COMPONENTS>
150
152 ProcessorBuilder& Excluding(std::initializer_list<ComponentId>);
153
156
159
162
165
168
170 Processor* Build();
171
172private:
176 std::function<void(World*, Dataset::View const&)> func = nullptr;
177 std::function<void(World*, Dataset::View const&)> funcModified = nullptr;
179 bool async = false;
180 bool onlyModified = false;
181 int order = 100;
182#ifdef WITH_NEBULA_EDITOR
183 bool runInEditor = false;
184#endif
185};
186
187//------------------------------------------------------------------------------
190template<typename LAMBDA>
192{
193 static_assert(std::is_invocable<LAMBDA()>());
194 return this->Func(std::function(lambda));
195}
196
197//------------------------------------------------------------------------------
200template<typename ...COMPONENTS>
201inline ProcessorBuilder&
202ProcessorBuilder::Func(std::function<void(World*, COMPONENTS...)> func)
203{
204 uint8_t const bufferStartOffset = this->filterBuilder.GetNumInclusive();
205 this->filterBuilder.Including<COMPONENTS...>();
206 this->func = Processor::ForEach(func, bufferStartOffset);
207 this->funcModified = Processor::ForEachModified(func, bufferStartOffset);
208 return *this;
209}
210
211//------------------------------------------------------------------------------
214template<typename ...COMPONENTS>
216{
217 this->filterBuilder.Including<COMPONENTS...>();
218 return *this;
219}
220
221//------------------------------------------------------------------------------
224template<typename ...COMPONENTS>
226{
227 this->filterBuilder.Excluding<COMPONENTS...>();
228 return *this;
229}
230
231} // namespace Game
Definition filter.h:45
uint8_t GetNumInclusive()
Definition filter.h:149
FilterBuilder & Excluding()
Definition filter.h:139
FilterBuilder & Including()
Definition filter.h:128
Definition processor.h:130
bool async
Definition processor.h:179
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:178
std::function< void(World *, Dataset::View const &)> funcModified
Definition processor.h:177
std::function< void(World *, Dataset::View const &)> func
Definition processor.h:176
int order
Definition processor.h:181
Util::StringAtom name
Definition processor.h:174
ProcessorBuilder & Func(LAMBDA)
which function to run with the processor
Definition processor.h:191
ProcessorBuilder & Excluding()
entities must not have any of these components
Definition processor.h:225
ProcessorBuilder & Async()
processor should run async
Definition processor.cc:54
ProcessorBuilder & Including()
entities must have these components
Definition processor.h:215
bool onlyModified
Definition processor.h:180
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:173
Util::StringAtom onEvent
Definition processor.h:175
Definition processor.h:22
Util::String name
name of the processor
Definition processor.h:25
friend ProcessorBuilder
Definition processor.h:44
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
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:48
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:93
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:58
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.
Nebula's dynamic array class.
Definition array.h:60
A StringAtom.
Definition stringatom.h:22
Game::EditorState.
Definition graphicsmanager.h:60
uint32_t Filter
Opaque filter identifier.
Definition filter.h:26
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:390
This represents a "view" into an entity table.
Nebula's universal string class.
Definition string.h:50
int IndexT
Definition types.h:48