Loading [MathJax]/extensions/TeX/AMSsymbols.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
signalmanager.cpp
1#include <catch2/catch_test_macros.hpp>
2
3#include <Engine/RadiumEngine.hpp>
4#include <Engine/Scene/Component.hpp>
5#include <Engine/Scene/Entity.hpp>
6#include <Engine/Scene/EntityManager.hpp>
7#include <Engine/Scene/SignalManager.hpp>
8
9using namespace Ra::Engine::Scene;
10
11class Foo
12{
13 public:
14 Ra::Core::Utils::Index i { 0 };
15 void incr( const ItemEntry& /*entity*/ ) { ++i; }
16 void decr( const ItemEntry& /*entity*/ ) { --i; }
17};
18
19class Bar
20{
21 public:
22 explicit Bar( Foo& f ) : m_f { f } {};
23 void operator()( const ItemEntry& entity ) { m_f.incr( entity ); }
24
25 private:
26 Foo& m_f;
27};
28
29class FooBarComponent : public Component
30{
31 public:
33 void initialize() override {};
34};
35
36TEST_CASE( "Engine/Scene/SignalManager/ON",
37 "[unittests][Engine][Engine/Scene][SignalManager][ON]" ) {
38
39 auto engine = Ra::Engine::RadiumEngine::createInstance();
40 engine->initialize();
41
42 SignalManager signalmanager;
43 signalmanager.setOn( true );
44
45 Foo entitytest;
46 Foo componenttest;
47 Foo renderobjecttest;
48 Foo eoftest;
49
50 auto entity = engine->getEntityManager()->createEntity( "test entity" );
51
52 SECTION( "Entities signals" ) {
53 ItemEntry item { entity };
54 auto& addNotifier = signalmanager.getEntityCreatedNotifier();
55 auto& delNotifier = signalmanager.getEntityDestroyedNotifier();
56
57 Bar bar { entitytest };
58
59 int ia = addNotifier.attach( bar );
60 int id = delNotifier.attachMember( &entitytest, &Foo::decr );
61
62 REQUIRE( entitytest.i == 0 );
63
64 signalmanager.fireEntityCreated( item );
65 REQUIRE( entitytest.i == 1 );
66 REQUIRE( componenttest.i == 0 );
67 REQUIRE( renderobjecttest.i == 0 );
68 REQUIRE( eoftest.i == 0 );
69
70 signalmanager.fireEntityDestroyed( item );
71 REQUIRE( entitytest.i == 0 );
72 REQUIRE( componenttest.i == 0 );
73 REQUIRE( renderobjecttest.i == 0 );
74 REQUIRE( eoftest.i == 0 );
75
76 addNotifier.detach( ia );
77 signalmanager.fireEntityCreated( item );
78 REQUIRE( entitytest.i == 0 );
79 REQUIRE( componenttest.i == 0 );
80 REQUIRE( renderobjecttest.i == 0 );
81 REQUIRE( eoftest.i == 0 );
82
83 delNotifier.detach( id );
84 signalmanager.fireEntityDestroyed( item );
85 REQUIRE( entitytest.i == 0 );
86 REQUIRE( componenttest.i == 0 );
87 REQUIRE( renderobjecttest.i == 0 );
88 REQUIRE( eoftest.i == 0 );
89 }
90
91 SECTION( "Components signals" ) {
92
93 auto component = new FooBarComponent( "test component", entity );
94
95 ItemEntry item { entity, component };
96 auto& addNotifier = signalmanager.getComponentCreatedNotifier();
97 auto& delNotifier = signalmanager.getComponentDestroyedNotifier();
98
99 auto bar = [&componenttest]( const ItemEntry& e ) { componenttest.incr( e ); };
100
101 int ia = addNotifier.attach( bar );
102 int id = delNotifier.attachMember( &componenttest, &Foo::decr );
103
104 REQUIRE( componenttest.i == 0 );
105
106 signalmanager.fireComponentAdded( item );
107 REQUIRE( componenttest.i == 1 );
108 REQUIRE( entitytest.i == 0 );
109 REQUIRE( renderobjecttest.i == 0 );
110 REQUIRE( eoftest.i == 0 );
111
112 signalmanager.fireComponentRemoved( item );
113 REQUIRE( componenttest.i == 0 );
114 REQUIRE( entitytest.i == 0 );
115 REQUIRE( renderobjecttest.i == 0 );
116 REQUIRE( eoftest.i == 0 );
117
118 addNotifier.detach( ia );
119 signalmanager.fireComponentAdded( item );
120 REQUIRE( componenttest.i == 0 );
121 REQUIRE( entitytest.i == 0 );
122 REQUIRE( renderobjecttest.i == 0 );
123 REQUIRE( eoftest.i == 0 );
124
125 delNotifier.detach( id );
126 signalmanager.fireComponentRemoved( item );
127 REQUIRE( componenttest.i == 0 );
128 REQUIRE( entitytest.i == 0 );
129 REQUIRE( renderobjecttest.i == 0 );
130 REQUIRE( eoftest.i == 0 );
131 }
132 SECTION( "RenderObjects signals" ) {
133
134 auto component = new FooBarComponent( "test component", entity );
135
136 ItemEntry item { entity, component, 1 };
137 auto& addNotifier = signalmanager.getRenderObjectCreatedNotifier();
138 auto& delNotifier = signalmanager.getRenderObjectDestroyedNotifier();
139
140 int ia = addNotifier.attach(
141 [&renderobjecttest]( const ItemEntry& e ) { renderobjecttest.incr( e ); } );
142 int id = delNotifier.attachMember( &renderobjecttest, &Foo::decr );
143
144 REQUIRE( renderobjecttest.i == 0 );
145
146 signalmanager.fireRenderObjectAdded( item );
147 REQUIRE( renderobjecttest.i == 1 );
148 REQUIRE( componenttest.i == 0 );
149 REQUIRE( entitytest.i == 0 );
150 REQUIRE( eoftest.i == 0 );
151
152 signalmanager.fireRenderObjectRemoved( item );
153 REQUIRE( renderobjecttest.i == 0 );
154 REQUIRE( componenttest.i == 0 );
155 REQUIRE( entitytest.i == 0 );
156 REQUIRE( eoftest.i == 0 );
157
158 addNotifier.detach( ia );
159 signalmanager.fireRenderObjectAdded( item );
160 REQUIRE( renderobjecttest.i == 0 );
161 REQUIRE( componenttest.i == 0 );
162 REQUIRE( entitytest.i == 0 );
163 REQUIRE( eoftest.i == 0 );
164
165 delNotifier.detach( id );
166 signalmanager.fireRenderObjectRemoved( item );
167 REQUIRE( renderobjecttest.i == 0 );
168 REQUIRE( componenttest.i == 0 );
169 REQUIRE( entitytest.i == 0 );
170 REQUIRE( eoftest.i == 0 );
171 }
172
173 SECTION( "End of Frame signal" ) {
174
175 auto component = new FooBarComponent( "test component", entity );
176
177 // this one is just to check it compiles ... we can add some REQUIREs in a near futur.
178 auto& eofNotifier = signalmanager.getEndFrameNotifier();
179
180 ItemEntry item { entity, component, 1 };
181
182 int ia = eofNotifier.attach( [&eoftest, item]() { eoftest.incr( item ); } );
183
184 REQUIRE( eoftest.i == 0 );
185
186 signalmanager.fireFrameEnded();
187 REQUIRE( eoftest.i == 1 );
188 signalmanager.fireFrameEnded();
189 REQUIRE( eoftest.i == 2 );
190
191 eofNotifier.detach( ia );
192 signalmanager.fireFrameEnded();
193 REQUIRE( eoftest.i == 2 );
194 }
195 engine->cleanup();
196 Ra::Engine::RadiumEngine::destroyInstance();
197}
198
199TEST_CASE( "Engine/Scene/SignalManager/OFF/",
200 "[unittests][Engine][Engine/Scene][SignalManager][OFF]" ) {
201
202 auto engine = Ra::Engine::RadiumEngine::createInstance();
203 engine->initialize();
204
205 SignalManager signalmanager;
206 signalmanager.setOn( false );
207
208 auto entity = engine->getEntityManager()->createEntity( "test entity" );
209
210 Foo entitytest;
211 Foo componenttest;
212 Foo renderobjecttest;
213 Foo eoftest;
214
215 SECTION( "Entities signals" ) {
216 ItemEntry item { entity };
217 auto& addNotifier = signalmanager.getEntityCreatedNotifier();
218 auto& delNotifier = signalmanager.getEntityDestroyedNotifier();
219
220 Bar bar { entitytest };
221
222 addNotifier.attach( bar );
223 delNotifier.attachMember( &entitytest, &Foo::decr );
224
225 REQUIRE( entitytest.i == 0 );
226
227 signalmanager.fireEntityCreated( item );
228 REQUIRE( entitytest.i == 0 );
229 REQUIRE( componenttest.i == 0 );
230 REQUIRE( renderobjecttest.i == 0 );
231 REQUIRE( eoftest.i == 0 );
232
233 signalmanager.fireEntityDestroyed( item );
234 REQUIRE( entitytest.i == 0 );
235 REQUIRE( componenttest.i == 0 );
236 REQUIRE( renderobjecttest.i == 0 );
237 REQUIRE( eoftest.i == 0 );
238 }
239
240 SECTION( "Components signals" ) {
241 auto component = new FooBarComponent( "test component", entity );
242
243 ItemEntry item { entity, component };
244 auto& addNotifier = signalmanager.getComponentCreatedNotifier();
245 auto& delNotifier = signalmanager.getComponentDestroyedNotifier();
246
247 auto bar = [&componenttest]( const ItemEntry& e ) { componenttest.incr( e ); };
248
249 addNotifier.attach( bar );
250 delNotifier.attachMember( &componenttest, &Foo::decr );
251
252 REQUIRE( componenttest.i == 0 );
253
254 signalmanager.fireComponentAdded( item );
255 REQUIRE( componenttest.i == 0 );
256 REQUIRE( entitytest.i == 0 );
257 REQUIRE( renderobjecttest.i == 0 );
258 REQUIRE( eoftest.i == 0 );
259
260 signalmanager.fireComponentRemoved( item );
261 REQUIRE( componenttest.i == 0 );
262 REQUIRE( entitytest.i == 0 );
263 REQUIRE( renderobjecttest.i == 0 );
264 REQUIRE( eoftest.i == 0 );
265 }
266
267 SECTION( "RenderObjects signals" ) {
268
269 auto component = new FooBarComponent( "test component", entity );
270
271 ItemEntry item { entity, component, 1 };
272 auto& addNotifier = signalmanager.getRenderObjectCreatedNotifier();
273 auto& delNotifier = signalmanager.getRenderObjectDestroyedNotifier();
274
275 addNotifier.attach(
276 [&renderobjecttest]( const ItemEntry& e ) { renderobjecttest.incr( e ); } );
277 delNotifier.attachMember( &renderobjecttest, &Foo::decr );
278
279 REQUIRE( renderobjecttest.i == 0 );
280
281 signalmanager.fireRenderObjectAdded( item );
282 REQUIRE( renderobjecttest.i == 0 );
283 REQUIRE( componenttest.i == 0 );
284 REQUIRE( entitytest.i == 0 );
285 REQUIRE( eoftest.i == 0 );
286
287 signalmanager.fireRenderObjectRemoved( item );
288 REQUIRE( renderobjecttest.i == 0 );
289 REQUIRE( componenttest.i == 0 );
290 REQUIRE( entitytest.i == 0 );
291 REQUIRE( eoftest.i == 0 );
292 }
293
294 SECTION( "End of Frame signal" ) {
295 auto component = new FooBarComponent( "test component", entity );
296
297 // this one is just to check it compiles ... we can add some REQUIREs in a near futur.
298 auto& eofNotifier = signalmanager.getEndFrameNotifier();
299
300 ItemEntry item { entity, component, 1 };
301
302 eofNotifier.attach( [&eoftest, item]() { eoftest.incr( item ); } );
303
304 REQUIRE( eoftest.i == 0 );
305
306 signalmanager.fireFrameEnded();
307 REQUIRE( eoftest.i == 0 );
308 }
309 engine->cleanup();
310 Ra::Engine::RadiumEngine::destroyInstance();
311}
int attach(Observer observer)
A component is an element that can be updated by a system. It is also linked to some other components...
Definition Component.hpp:31
Component(const std::string &name, Entity *entity)
CONSTRUCTOR.
Definition Component.cpp:20
void fireEntityDestroyed(const ItemEntry &entity) const
Notifies all observers of an entity removal.
ItemObservable & getEntityCreatedNotifier()
void fireRenderObjectRemoved(const ItemEntry &ro) const
Notifies all observers of a render object removal.
void fireComponentAdded(const ItemEntry &component) const
Notifies all observers of a component creation.
void setOn(bool on)
Enable/disable the notification of observers.
void fireEntityCreated(const ItemEntry &entity) const
Notifies all observers of an entity creation.
void fireComponentRemoved(const ItemEntry &component) const
Notifies all observers of a component removal.
void fireRenderObjectAdded(const ItemEntry &ro) const
Notifies all observers of a render object creation.
void fireFrameEnded() const
Notifies all observers of a end of frame event.
Scene and how to communicate.