@@ -32,11 +32,17 @@ Engine::Engine() :
3232{
3333}
3434
35+ Engine::~Engine ()
36+ {
37+ m_clones.clear ();
38+ }
39+
3540void Engine::clear ()
3641{
3742 m_sections.clear ();
3843 m_targets.clear ();
3944 m_broadcasts.clear ();
45+ m_clones.clear ();
4046}
4147
4248// Resolves ID references and sets pointers of entities.
@@ -172,6 +178,11 @@ void Engine::frame()
172178
173179void Engine::start ()
174180{
181+ if (m_running)
182+ finalize ();
183+
184+ deleteClones ();
185+
175186 m_timer->reset ();
176187 m_running = true ;
177188
@@ -184,9 +195,8 @@ void Engine::start()
184195
185196void Engine::stop ()
186197{
187- m_runningScripts.clear ();
188- m_scriptsToRemove.clear ();
189- m_running = false ;
198+ finalize ();
199+ deleteClones ();
190200}
191201
192202void Engine::startScript (std::shared_ptr<Block> topLevelBlock, std::shared_ptr<Target> target)
@@ -328,6 +338,14 @@ void Engine::initClone(Sprite *clone)
328338 m_runningScripts.push_back (vm);
329339 }
330340 }
341+
342+ assert (std::find (m_clones.begin (), m_clones.end (), clone) == m_clones.end ());
343+ m_clones.push_back (clone);
344+ }
345+
346+ void Engine::deinitClone (Sprite *clone)
347+ {
348+ m_clones.erase (std::remove (m_clones.begin (), m_clones.end (), clone), m_clones.end ());
331349}
332350
333351void Engine::run ()
@@ -360,7 +378,7 @@ void Engine::run()
360378 lastFrameTime = currentTime;
361379 }
362380
363- stop ();
381+ finalize ();
364382}
365383
366384bool Engine::isRunning () const
@@ -879,6 +897,31 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
879897 return nullptr ;
880898}
881899
900+ void Engine::finalize ()
901+ {
902+ m_runningScripts.clear ();
903+ m_scriptsToRemove.clear ();
904+ m_running = false ;
905+ }
906+
907+ void Engine::deleteClones ()
908+ {
909+ m_clones.clear ();
910+
911+ for (auto target : m_targets) {
912+ Sprite *sprite = dynamic_cast <Sprite *>(target.get ());
913+
914+ if (sprite) {
915+ std::vector<std::shared_ptr<Sprite>> clones = sprite->children ();
916+
917+ for (auto clone : clones) {
918+ assert (clone);
919+ clone->~Sprite ();
920+ }
921+ }
922+ }
923+ }
924+
882925void Engine::updateFrameDuration ()
883926{
884927 m_frameDuration = std::chrono::milliseconds (static_cast <long >(1000 / m_fps));
0 commit comments