Skip to content

Commit

Permalink
#150 Optimize fps by cache the list child objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jul 25, 2022
1 parent db943aa commit a8304d0
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 116 deletions.
64 changes: 40 additions & 24 deletions Projects/Skylicht/Engine/Source/GameObject/CContainerObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ namespace Skylicht
CContainerObject::CContainerObject(CGameObject* parent, CZone* zone) :
CGameObject(parent, zone),
m_updateRemoveAdd(true),
m_updateListChild(true),
m_lastGenerateID(-1)
{
m_isContainer = true;
}

CContainerObject::~CContainerObject()
Expand Down Expand Up @@ -76,32 +78,37 @@ namespace Skylicht

core::array<CGameObject*>& CContainerObject::getArrayChilds(bool addThis)
{
m_arrayChildObjects.set_used(0);
std::queue<CGameObject*> queueObjs;

if (addThis == true)
queueObjs.push(this);
else
if (m_updateListChild)
{
for (CGameObject*& obj : m_childs)
queueObjs.push(obj);
}
m_arrayChildObjects.set_used(0);

while (queueObjs.size() != 0)
{
CGameObject* obj = queueObjs.front();
queueObjs.pop();
std::queue<CGameObject*> queueObjs;

m_arrayChildObjects.push_back(obj);
if (addThis == true)
queueObjs.push(this);
else
{
for (CGameObject*& obj : m_childs)
queueObjs.push(obj);
}

CContainerObject* container = dynamic_cast<CContainerObject*>(obj);
if (container != NULL)
while (queueObjs.size() != 0)
{
for (CGameObject*& child : container->m_childs)
queueObjs.push(child);
CGameObject* obj = queueObjs.front();
queueObjs.pop();

m_arrayChildObjects.push_back(obj);

CContainerObject* container = dynamic_cast<CContainerObject*>(obj);
if (container != NULL)
{
for (CGameObject*& child : container->m_childs)
queueObjs.push(child);
}
}
}

m_updateListChild = false;
}
return m_arrayChildObjects;
}

Expand Down Expand Up @@ -206,7 +213,8 @@ namespace Skylicht
addChild(p);

p->createEntity();
p->addComponent<CTransformEuler>();
p->setupEulerTransform();

return p;
}

Expand All @@ -223,7 +231,8 @@ namespace Skylicht
addChild(container);

container->createEntity();
container->addComponent<CTransformEuler>();
container->setupEulerTransform();

return container;
}

Expand Down Expand Up @@ -509,10 +518,13 @@ namespace Skylicht
// update in children
for (CGameObject*& obj : m_childs)
{
CContainerObject* container = dynamic_cast<CContainerObject*>(obj);
if (container != NULL)
if (obj->isContainer())
{
container->updateAddRemoveObject(force);
CContainerObject* container = (CContainerObject*)obj;
if (container != NULL)
{
container->updateAddRemoveObject(force);
}
}
}
}
Expand All @@ -522,11 +534,15 @@ namespace Skylicht
p->setParent(this);
m_add.push_back(p);
m_updateRemoveAdd = true;

getZone()->notifyUpdateListChild();
}

void CContainerObject::removeObject(CGameObject* pObj)
{
m_remove.push_back(pObj);
m_updateRemoveAdd = true;

getZone()->notifyUpdateListChild();
}
}
7 changes: 7 additions & 0 deletions Projects/Skylicht/Engine/Source/GameObject/CContainerObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace Skylicht
core::map<std::string, CGameObject*> m_objectByID;

bool m_updateRemoveAdd;
bool m_updateListChild;

int m_lastGenerateID;

public:
Expand All @@ -56,6 +58,11 @@ namespace Skylicht

void updateAddRemoveObject(bool force = false);

inline void notifyUpdateListChild()
{
m_updateListChild = true;
}

int getNumberObjects();

void updateIndexSearchObject();
Expand Down
40 changes: 21 additions & 19 deletions Projects/Skylicht/Engine/Source/GameObject/CGameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace Skylicht
m_enable = true;
m_visible = true;
m_static = false;
m_isContainer = false;

m_editorObject = false;
m_enableEditorChange = true;
Expand All @@ -63,6 +64,10 @@ namespace Skylicht
m_zone = NULL;
m_entity = NULL;

m_transform = NULL;
m_transformEuler = NULL;
m_transformMatrix = NULL;

m_tagData = NULL;
m_tagDataInt = 0;
m_cullingLayer = 1;
Expand Down Expand Up @@ -156,50 +161,47 @@ namespace Skylicht
return m_zone->getEntityManager();
}

CTransform* CGameObject::getTransform()
{
return getComponent<CTransform>();
}

CTransformEuler* CGameObject::getTransformEuler()
{
return getComponent<CTransformEuler>();
}

CTransformMatrix* CGameObject::getTransformMatrix()
{
return getComponent<CTransformMatrix>();
}

void CGameObject::setupMatrixTransform()
{
if (getComponent<CTransformMatrix>() == NULL)
{
core::matrix4 relative = getTransform()->getRelativeTransform();
core::matrix4 relative;
if (getTransform())
relative = getTransform()->getRelativeTransform();

removeComponent<CTransformEuler>();
CTransform* t = addComponent<CTransformMatrix>();
CTransformMatrix* t = addComponent<CTransformMatrix>();
t->setRelativeTransform(relative);

CTransformComponentData* componentData = GET_ENTITY_DATA(m_entity, CTransformComponentData);
componentData->TransformComponent = t;
componentData->TransformComponent->setChanged(true);

m_transformEuler = NULL;
m_transformMatrix = t;
m_transform = t;
}
}

void CGameObject::setupEulerTransform()
{
if (getComponent<CTransformEuler>() == NULL)
{
core::matrix4 relative = getTransform()->getRelativeTransform();
core::matrix4 relative;
if (getTransform())
relative = getTransform()->getRelativeTransform();

removeComponent<CTransformMatrix>();
CTransform* t = addComponent<CTransformEuler>();
CTransformEuler* t = addComponent<CTransformEuler>();
t->setRelativeTransform(relative);

CTransformComponentData* componentData = GET_ENTITY_DATA(m_entity, CTransformComponentData);
componentData->TransformComponent = t;
componentData->TransformComponent->setChanged(true);

m_transformMatrix = NULL;
m_transformEuler = t;
m_transform = t;
}
}

Expand Down
25 changes: 22 additions & 3 deletions Projects/Skylicht/Engine/Source/GameObject/CGameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Skylicht
bool m_static;
bool m_enable;
bool m_visible;
bool m_isContainer;

bool m_editorObject;
bool m_enableEditorChange;
Expand All @@ -70,6 +71,10 @@ namespace Skylicht

std::vector<CComponentSystem*> m_components;

CTransform* m_transform;
CTransformEuler* m_transformEuler;
CTransformMatrix* m_transformMatrix;

public:
CGameObject(CGameObject* parent, CZone* zone);

Expand Down Expand Up @@ -141,11 +146,20 @@ namespace Skylicht

CEntityManager* getEntityManager();

CTransform* getTransform();
inline CTransform* getTransform()
{
return m_transform;
}

CTransformEuler* getTransformEuler();
inline CTransformEuler* getTransformEuler()
{
return m_transformEuler;
}

CTransformMatrix* getTransformMatrix();
inline CTransformMatrix* getTransformMatrix()
{
return m_transformMatrix;
}

void setupMatrixTransform();

Expand Down Expand Up @@ -182,6 +196,11 @@ namespace Skylicht

virtual void setVisible(bool b);

inline bool isContainer()
{
return m_isContainer;
}

void setCullingLayer(u32 layer);

inline u32 getCullingLayer()
Expand Down
2 changes: 1 addition & 1 deletion Projects/Skylicht/Engine/Source/Scene/CScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace Skylicht
zone->setName(name.c_str());
zone->setID(zone->generateRandomID().c_str());
zone->createEntity();
zone->addComponent<CTransformEuler>();
zone->setupEulerTransform();

m_zones.push_back(zone);
return zone;
Expand Down
103 changes: 103 additions & 0 deletions Projects/Skylicht/Engine/Source/Transform/CGroupTransform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
!@
MIT License
Copyright (c) 2022 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CGroupTransform.h"
#include "Entity/CEntityManager.h"

namespace Skylicht
{
CGroupTransform::CGroupTransform(CEntityGroup* parent) :
CEntityGroup(NULL, 0)
{
m_parentGroup = parent;
m_dataTypes.push_back(CWorldTransformData::DataTypeIndex);
}

CGroupTransform::~CGroupTransform()
{

}

void CGroupTransform::onQuery(CEntityManager* entityManager, CEntity** entities, int numEntity)
{
CEntity** allEntities = entityManager->getEntities();

if (m_parentGroup)
{
numEntity = m_parentGroup->getEntityCount();
entities = m_parentGroup->getEntities();
}

m_entities.reset();
m_transforms.reset();
m_roots.reset();
m_childs.reset();

for (int i = 0; i < numEntity; i++)
{
CEntity* entity = entities[i];
CWorldTransformData* transform = GET_ENTITY_DATA(entity, CWorldTransformData);

// set disable flag
transform->NeedValidate = false;

int parentID = transform->AttachParentIndex >= 0 ?
transform->AttachParentIndex :
transform->ParentIndex;

if (parentID != -1)
{
transform->Parent = GET_ENTITY_DATA(allEntities[parentID], CWorldTransformData);

// this transform changed because parent is changed
if (transform->Parent->NeedValidate)
transform->HasChanged = true;
}
else
{
transform->Parent = NULL;
}

// tag to update list
if (transform->HasChanged)
{
// set enable flag for another system
transform->NeedValidate = true;

m_entities.push(entity);

if (transform->Depth == 0)
m_roots.push(transform);
else
m_childs.push(transform);

transform->HasChanged = false;
}
}

// notify alway update this group
m_needQuery = true;
}
}
Loading

0 comments on commit a8304d0

Please sign in to comment.