Skip to content

Commit

Permalink
Traktor: World update defer add/remove to after updating all entities…
Browse files Browse the repository at this point in the history
…. EffectComponent cleanup looping effects properly.
  • Loading branch information
apistol78 committed Feb 27, 2024
1 parent e89d250 commit 3f6f2e5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
28 changes: 20 additions & 8 deletions code/Spray/EffectComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ T_IMPLEMENT_RTTI_CLASS(L"traktor.spray.EffectComponent", EffectComponent, world:
EffectComponent::EffectComponent(const resource::Proxy< Effect >& effect)
: m_transform(Transform::identity())
, m_effect(effect)
, m_counter(0)
, m_enable(true)
{
m_effectInstance = m_effect->createInstance();
T_FATAL_ASSERT(m_effectInstance != nullptr);

m_context.deltaTime = 0.0f;
m_context.random = RandomGeometry(g_randomSeed.next());

updateTechniques();
}

EffectComponent::EffectComponent(const resource::Proxy< Effect >& effect, EffectInstance* effectInstance, const Context& context)
: m_transform(Transform::identity())
, m_effect(effect)
, m_effectInstance(effectInstance)
, m_context(context)
, m_counter(0)
, m_enable(true)
{
// Do not recreate instance if we've been provided one.
if (effectInstance != nullptr)
Expand Down Expand Up @@ -96,11 +97,17 @@ void EffectComponent::update(const world::UpdateParams& update)
if ((m_counter++ % c_updateDenom) != 0)
return;

if (m_effect.changed() || !m_effectInstance)
if (m_effect.changed() && m_effectInstance != nullptr)
{
const bool loopEnable = m_effectInstance->getLoopEnable();

m_effectInstance = m_effect->createInstance();
if (m_effectInstance)
{
m_effectInstance->setLoopEnable(loopEnable);
updateTechniques();
}

m_effect.consume();
}

Expand Down Expand Up @@ -142,7 +149,12 @@ Aabb3 EffectComponent::getWorldBoundingBox() const

void EffectComponent::reset()
{
m_effectInstance = m_effect->createInstance();
if (m_effectInstance != nullptr)
{
const bool loopEnable = m_effectInstance->getLoopEnable();
m_effectInstance = m_effect->createInstance();
m_effectInstance->setLoopEnable(loopEnable);
}
}

void EffectComponent::setLoopEnable(bool loopEnable)
Expand All @@ -158,10 +170,10 @@ bool EffectComponent::getLoopEnable() const

bool EffectComponent::isFinished() const
{
if (!m_effect)
if (!m_effect || !m_effectInstance)
return true;

if (!m_effectInstance || m_effectInstance->getLoopEnable())
if (m_effectInstance->getLoopEnable())
return false;

return m_effectInstance->getTime() >= m_effect->getDuration();
Expand Down
4 changes: 2 additions & 2 deletions code/Spray/EffectComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class T_DLLCLASS EffectComponent : public world::IEntityComponent
Ref< EffectInstance > m_effectInstance;
SmallSet< render::handle_t > m_techniques;
Context m_context;
uint32_t m_counter;
bool m_enable;
uint32_t m_counter = 0;
bool m_enable = true;

void updateTechniques();
};
Expand Down
1 change: 0 additions & 1 deletion code/Spray/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ struct Context
float deltaTime = 0.0f;
RandomGeometry random;
world::Entity* owner = nullptr;
//world::EntityEventManager* eventManager = nullptr;
VirtualSourceCallback* virtualSourceCallback = nullptr;
};

Expand Down
43 changes: 37 additions & 6 deletions code/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ World::World()

void World::destroy()
{
T_FATAL_ASSERT(m_deferredAdd.empty());
T_FATAL_ASSERT(m_deferredRemove.empty());

for (auto component : m_components)
component->destroy();
m_components.clear();
Expand Down Expand Up @@ -66,15 +69,23 @@ IWorldComponent* World::getComponent(const TypeInfo& componentType) const
void World::addEntity(Entity* entity)
{
T_FATAL_ASSERT(entity->m_world == nullptr);
m_entities.push_back(entity);
if (m_update)
m_deferredAdd.push_back(entity);
else
m_entities.push_back(entity);
entity->m_world = this;
}

void World::removeEntity(Entity* entity)
{
T_FATAL_ASSERT(entity->m_world == this);
const bool removed = m_entities.remove(entity);
T_FATAL_ASSERT(removed);
if (m_update)
m_deferredRemove.push_back(entity);
else
{
const bool removed = m_entities.remove(entity);
T_FATAL_ASSERT(removed);
}
entity->m_world = nullptr;
}

Expand Down Expand Up @@ -127,9 +138,29 @@ void World::update(const UpdateParams& update)
for (auto component : m_components)
component->update(this, update);

RefArray< Entity > entities = m_entities;
for (auto entity : entities)
entity->update(update);
m_update = true;
for (auto entity : m_entities)
{
if (entity->m_world != nullptr)
entity->update(update);
}
m_update = false;

if (!m_deferredAdd.empty())
{
m_entities.insert(m_entities.end(), m_deferredAdd.begin(), m_deferredAdd.end());
m_deferredAdd.resize(0);
}

if (!m_deferredRemove.empty())
{
for (auto entity : m_deferredRemove)
{
const bool removed = m_entities.remove(entity);
T_FATAL_ASSERT(removed);
}
m_deferredRemove.resize(0);
}
}

}
3 changes: 3 additions & 0 deletions code/World/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class T_DLLCLASS World : public Object
private:
RefArray< IWorldComponent > m_components;
RefArray< Entity > m_entities;
RefArray< Entity > m_deferredAdd;
RefArray< Entity > m_deferredRemove;
bool m_update = false;
};

}

0 comments on commit 3f6f2e5

Please sign in to comment.