From ac87c90d621051ca935f0034b8bbdfa2e6bf3112 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 17 Nov 2014 15:53:03 -0800 Subject: [PATCH] Remove Changing state, now keep QSet of changes --- libraries/entities/src/EntityTree.cpp | 64 ++++++-------------- libraries/entities/src/EntityTree.h | 12 ++-- libraries/entities/src/EntityTreeElement.cpp | 10 +-- 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 967d45b5af..d19dda8e6c 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -42,8 +42,8 @@ void EntityTree::eraseAllOctreeElements(bool createNewRoot) { _entityToElementMap.clear(); Octree::eraseAllOctreeElements(createNewRoot); _movingEntities.clear(); - _changingEntities.clear(); _mortalEntities.clear(); + _changedEntities.clear(); } bool EntityTree::handlesEditPacketType(PacketType packetType) const { @@ -91,7 +91,7 @@ void EntityTree::addEntityItem(EntityItem* entity) { recurseTreeWithOperator(&theOperator); // check to see if we need to simulate this entity.. - changeEntityState(entity, EntityItem::Static, entity->getSimulationState()); + changeEntityState(entity, EntityItem::Static, entity->computeSimulationState()); if (_physicsWorld && !entity->getMotionState()) { addEntityToPhysicsWorld(entity); @@ -129,14 +129,14 @@ bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProp } } else { // check to see if we need to simulate this entity... - EntityItem::SimulationState oldState = existingEntity->getSimulationState(); + EntityItem::SimulationState oldState = existingEntity->computeSimulationState(); QString entityScriptBefore = existingEntity->getScript(); UpdateEntityOperator theOperator(this, containingElement, existingEntity, properties); recurseTreeWithOperator(&theOperator); _isDirty = true; - EntityItem::SimulationState newState = existingEntity->getSimulationState(); + EntityItem::SimulationState newState = existingEntity->computeSimulationState(); if (newState != oldState) { changeEntityState(existingEntity, oldState, newState); } @@ -242,12 +242,8 @@ void EntityTree::removeEntityFromSimulationLists(const EntityItemID& entityID) { if (theEntity) { // make sure to remove it from any of our simulation lists - EntityItem::SimulationState theState = theEntity->getSimulationState(); + EntityItem::SimulationState theState = theEntity->computeSimulationState(); switch (theState) { - case EntityItem::Changing: - _changingEntities.removeAll(theEntity); - break; - case EntityItem::Moving: _movingEntities.removeAll(theEntity); break; @@ -259,6 +255,7 @@ void EntityTree::removeEntityFromSimulationLists(const EntityItemID& entityID) { default: break; } + _changedEntities.remove(theEntity); } } @@ -600,10 +597,6 @@ void EntityTree::changeEntityState(EntityItem* const entity, // TODO: can we short circuit this if the state isn't changing? switch (oldState) { - case EntityItem::Changing: - _changingEntities.removeAll(entity); - break; - case EntityItem::Moving: _movingEntities.removeAll(entity); break; @@ -618,10 +611,6 @@ void EntityTree::changeEntityState(EntityItem* const entity, switch (newState) { - case EntityItem::Changing: - _changingEntities.push_back(entity); - break; - case EntityItem::Moving: _movingEntities.push_back(entity); break; @@ -635,6 +624,10 @@ void EntityTree::changeEntityState(EntityItem* const entity, } } +void EntityTree::entityChanged(EntityItem* entity) { + _changedEntities.insert(entity); +} + void EntityTree::addEntityToPhysicsWorld(EntityItem* entity) { EntityMotionState* motionState = entity->createMotionState(); if (!_physicsWorld->addEntity(static_cast(motionState))) { @@ -665,7 +658,7 @@ void EntityTree::update() { lockForWrite(); quint64 now = usecTimestampNow(); QSet entitiesToDelete; - updateChangingEntities(now, entitiesToDelete); + updateChangedEntities(now, entitiesToDelete); updateMovingEntities(now, entitiesToDelete); updateMortalEntities(now, entitiesToDelete); @@ -675,27 +668,21 @@ void EntityTree::update() { unlock(); } -void EntityTree::updateChangingEntities(quint64 now, QSet& entitiesToDelete) { +void EntityTree::updateChangedEntities(quint64 now, QSet& entitiesToDelete) { QSet entitiesBecomingStatic; QSet entitiesBecomingMortal; QSet entitiesBecomingMoving; // TODO: switch these to iterators so we can remove items that get deleted - for (int i = 0; i < _changingEntities.size(); i++) { - EntityItem* thisEntity = _changingEntities[i]; - EntityMotionState* motionState = thisEntity->getMotionState(); - if (!motionState) { - thisEntity->update(now); - } - - // always check to see if the lifetime has expired, for immortal entities this is always false + foreach (EntityItem* thisEntity, _changedEntities) { + // check to see if the lifetime has expired, for immortal entities this is always false if (thisEntity->lifetimeHasExpired()) { qDebug() << "Lifetime has expired for entity:" << thisEntity->getEntityItemID(); entitiesToDelete << thisEntity->getEntityItemID(); entitiesBecomingStatic << thisEntity; } else { - // check to see if this entity is no longer moving - EntityItem::SimulationState newState = thisEntity->getSimulationState(); + // TODO: Andrew to push changes to physics engine (when we know how to sort the changes) + EntityItem::SimulationState newState = thisEntity->computeSimulationState(); if (newState == EntityItem::Static) { entitiesBecomingStatic << thisEntity; @@ -706,6 +693,7 @@ void EntityTree::updateChangingEntities(quint64 now, QSet& entitie } } } + _changedEntities.clear(); // change state for any entities that were changing but are now either static, mortal, or moving foreach(EntityItem* entity, entitiesBecomingStatic) { @@ -726,7 +714,6 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT QSet entitiesBecomingStatic; QSet entitiesBecomingMortal; - QSet entitiesBecomingChanging; { PerformanceTimer perfTimer("_movingEntities"); @@ -761,10 +748,8 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT moveOperator.addEntityToMoveList(thisEntity, oldCube, newCube); // check to see if this entity is no longer moving - EntityItem::SimulationState newState = thisEntity->getSimulationState(); - if (newState == EntityItem::Changing) { - entitiesBecomingChanging << thisEntity; - } else if (newState == EntityItem::Mortal) { + EntityItem::SimulationState newState = thisEntity->computeSimulationState(); + if (newState == EntityItem::Mortal) { entitiesBecomingMortal << thisEntity; } else if (newState == EntityItem::Static) { entitiesBecomingStatic << thisEntity; @@ -785,15 +770,11 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT foreach(EntityItem* entity, entitiesBecomingMortal) { changeEntityState(entity, EntityItem::Moving, EntityItem::Mortal); } - foreach(EntityItem* entity, entitiesBecomingChanging) { - changeEntityState(entity, EntityItem::Moving, EntityItem::Changing); - } } } void EntityTree::updateMortalEntities(quint64 now, QSet& entitiesToDelete) { QSet entitiesBecomingStatic; - QSet entitiesBecomingChanging; QSet entitiesBecomingMoving; // TODO: switch these to iterators so we can remove items that get deleted @@ -807,12 +788,10 @@ void EntityTree::updateMortalEntities(quint64 now, QSet& entitiesT entitiesBecomingStatic << thisEntity; } else { // check to see if this entity is no longer moving - EntityItem::SimulationState newState = thisEntity->getSimulationState(); + EntityItem::SimulationState newState = thisEntity->computeSimulationState(); if (newState == EntityItem::Static) { entitiesBecomingStatic << thisEntity; - } else if (newState == EntityItem::Changing) { - entitiesBecomingChanging << thisEntity; } else if (newState == EntityItem::Moving) { entitiesBecomingMoving << thisEntity; } @@ -823,9 +802,6 @@ void EntityTree::updateMortalEntities(quint64 now, QSet& entitiesT foreach(EntityItem* entity, entitiesBecomingStatic) { changeEntityState(entity, EntityItem::Mortal, EntityItem::Static); } - foreach(EntityItem* entity, entitiesBecomingChanging) { - changeEntityState(entity, EntityItem::Mortal, EntityItem::Changing); - } foreach(EntityItem* entity, entitiesBecomingMoving) { changeEntityState(entity, EntityItem::Mortal, EntityItem::Moving); } diff --git a/libraries/entities/src/EntityTree.h b/libraries/entities/src/EntityTree.h index e32f9a9f47..9ce1e6b94a 100644 --- a/libraries/entities/src/EntityTree.h +++ b/libraries/entities/src/EntityTree.h @@ -12,6 +12,8 @@ #ifndef hifi_EntityTree_h #define hifi_EntityTree_h +#include + #include #include "EntityTreeElement.h" @@ -139,6 +141,7 @@ public: void changeEntityState(EntityItem* const entity, EntityItem::SimulationState oldState, EntityItem::SimulationState newState); + void entityChanged(EntityItem* entity); void addEntityToPhysicsWorld(EntityItem* entity); void removeEntityFromPhysicsWorld(EntityItem* entity); @@ -159,7 +162,7 @@ signals: private: - void updateChangingEntities(quint64 now, QSet& entitiesToDelete); + void updateChangedEntities(quint64 now, QSet& entitiesToDelete); void updateMovingEntities(quint64 now, QSet& entitiesToDelete); void updateMortalEntities(quint64 now, QSet& entitiesToDelete); @@ -179,9 +182,10 @@ private: QHash _entityToElementMap; - QList _movingEntities; // entities that are moving as part of update - QList _changingEntities; // entities that are changing (like animating), but not moving - QList _mortalEntities; // entities that are mortal (have lifetime), but not moving or changing + QList _movingEntities; // entities that need to be updated + QList _mortalEntities; // entities that need to be checked for expiry + + QSet _changedEntities; // entities that have changed in the last frame PhysicsWorld* _physicsWorld; }; diff --git a/libraries/entities/src/EntityTreeElement.cpp b/libraries/entities/src/EntityTreeElement.cpp index b26db16f89..f3fd8fc494 100644 --- a/libraries/entities/src/EntityTreeElement.cpp +++ b/libraries/entities/src/EntityTreeElement.cpp @@ -745,14 +745,10 @@ int EntityTreeElement::readElementDataFromBuffer(const unsigned char* data, int QString entityScriptBefore = entityItem->getScript(); bool bestFitBefore = bestFitEntityBounds(entityItem); EntityTreeElement* currentContainingElement = _myTree->getContainingElement(entityItemID); - EntityItem::SimulationState oldState = entityItem->getSimulationState(); bytesForThisEntity = entityItem->readEntityDataFromBuffer(dataAt, bytesLeftToRead, args); - - EntityItem::SimulationState newState = entityItem->getSimulationState(); - if (oldState != newState) { - _myTree->changeEntityState(entityItem, oldState, newState); - } + // TODO: Andrew to only set changed if something has actually changed + _myTree->entityChanged(entityItem); bool bestFitAfter = bestFitEntityBounds(entityItem); if (bestFitBefore != bestFitAfter) { @@ -780,7 +776,7 @@ int EntityTreeElement::readElementDataFromBuffer(const unsigned char* data, int entityItemID = entityItem->getEntityItemID(); _myTree->setContainingElement(entityItemID, this); _myTree->emitAddingEntity(entityItemID); // we just added an entity - EntityItem::SimulationState newState = entityItem->getSimulationState(); + EntityItem::SimulationState newState = entityItem->computeSimulationState(); _myTree->changeEntityState(entityItem, EntityItem::Static, newState); } }