Skip to content

Commit

Permalink
UPBGE: Implement python component manager.
Browse files Browse the repository at this point in the history
Previously the inexistance of a python component manager forced
in KX_Scene to iterate over all the object to ask for a component
update. The implementation of a manager with a registering
mechanism help to avoid this iteration and optimize as much as
possible the case where a scene doesn't update any components.
  • Loading branch information
panzergame committed Oct 20, 2017
1 parent 904ca9b commit 76aff7a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
7 changes: 7 additions & 0 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,13 @@ void BL_ConvertBlenderObjects(struct Main *maggie,
BL_ConvertComponentsObject(gameobj, blenderobj);
}

for (KX_GameObject *gameobj : objectlist) {
if (gameobj->GetComponents()) {
// Register object for component update.
kxscene->GetPythonComponentManager().RegisterObject(gameobj);
}
}

// Cleanup converted set of group objects.
convertedlist->Release();
sumolist->Release();
Expand Down
2 changes: 2 additions & 0 deletions source/gameengine/Ketsji/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ set(SRC
KX_PyConstraintBinding.cpp
KX_PyMath.cpp
KX_PythonComponent.cpp
KX_PythonComponentManager.cpp
KX_PythonInit.cpp
KX_PythonInitTypes.cpp
KX_PythonMain.cpp
Expand Down Expand Up @@ -211,6 +212,7 @@ set(SRC
KX_PyConstraintBinding.h
KX_PyMath.h
KX_PythonComponent.h
KX_PythonComponentManager.h
KX_PythonInit.h
KX_PythonInitTypes.h
KX_PythonMain.h
Expand Down
37 changes: 37 additions & 0 deletions source/gameengine/Ketsji/KX_PythonComponentManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "KX_PythonComponentManager.h"
#include "KX_PythonComponent.h"
#include "KX_GameObject.h"

KX_PythonComponentManager::KX_PythonComponentManager()
{
}

KX_PythonComponentManager::~KX_PythonComponentManager()
{
}

void KX_PythonComponentManager::RegisterObject(KX_GameObject *gameobj)
{
// Always register only once an object.
m_objects.push_back(gameobj);
}

void KX_PythonComponentManager::UnregisterObject(KX_GameObject *gameobj)
{
std::vector<KX_GameObject *>::iterator it = std::find(m_objects.begin(), m_objects.end(), gameobj);
if (it != m_objects.end()) {
m_objects.erase(it);
}
}

void KX_PythonComponentManager::UpdateComponents()
{
/* Update object components, we copy the object pointer in a second list to make
* sure that we iterate on a list which will not be modified, indeed components
* can add objects in theirs update.
*/
const std::vector<KX_GameObject *> objects = m_objects;
for (KX_GameObject *gameobj : objects) {
gameobj->UpdateComponents();
}
}
23 changes: 23 additions & 0 deletions source/gameengine/Ketsji/KX_PythonComponentManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __KX_PYTHON_COMPONENT_H__
#define __KX_PYTHON_COMPONENT_H__

#include <vector>

class KX_GameObject;

class KX_PythonComponentManager
{
private:
std::vector<KX_GameObject *> m_objects;

public:
KX_PythonComponentManager();
~KX_PythonComponentManager();

void RegisterObject(KX_GameObject *gameobj);
void UnregisterObject(KX_GameObject *gameobj);

void UpdateComponents();
};

#endif // __KX_PYTHON_COMPONENT_H__
25 changes: 13 additions & 12 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ SCA_TimeEventManager* KX_Scene::GetTimeEventManager() const
return m_timemgr;
}

KX_PythonComponentManager& KX_Scene::GetPythonComponentManager()
{
return m_componentManager;
}

EXP_ListValue<KX_Camera> *KX_Scene::GetCameraList() const
{
return m_cameralist;
Expand Down Expand Up @@ -486,6 +491,11 @@ KX_GameObject* KX_Scene::AddNodeReplicaObject(SG_Node* node, KX_GameObject *game
m_obstacleSimulation->AddObstacleForObj(newobj);
}

// Register object for component update.
if (gameobj->GetComponents()) {
m_componentManager.RegisterObject(newobj);
}

replicanode->SetSGClientObject(newobj);

// this is the list of object that are send to the graphics pipeline
Expand Down Expand Up @@ -1019,6 +1029,8 @@ bool KX_Scene::NewRemoveObject(KX_GameObject *gameobj)
m_obstacleSimulation->DestroyObstacleForObj(gameobj);
}

m_componentManager.UnregisterObject(gameobj);

gameobj->RemoveMeshes();

m_rendererManager->InvalidateViewpoint(gameobj);
Expand Down Expand Up @@ -1387,18 +1399,7 @@ void KX_Scene::UpdateAnimations(double curtime)

void KX_Scene::LogicUpdateFrame(double curtime)
{
/* Update object components, we copy the object pointer in a second list to make sure that we iterate on a list
* which will not be modified, indeed components can add objects in theirs initialization.
*/

std::vector<KX_GameObject *> objects;
for (KX_GameObject *gameobj : m_objectlist) {
objects.push_back(gameobj);
}

for (KX_GameObject *gameobj : objects) {
gameobj->UpdateComponents();
}
m_componentManager.UpdateComponents();

m_logicmgr->UpdateFrame(curtime);
}
Expand Down
5 changes: 5 additions & 0 deletions source/gameengine/Ketsji/KX_Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "KX_PhysicsEngineEnums.h"
#include "KX_TextureRendererManager.h" // For KX_TextureRendererManager::RendererCategory.
#include "KX_CullingNode.h" // For KX_CullingNodeList.
#include "KX_PythonComponentManager.h"

#include <vector>
#include <set>
Expand Down Expand Up @@ -178,6 +179,8 @@ class KX_Scene : public EXP_Value, public SCA_IScene
SCA_MouseManager* m_mousemgr;
SCA_TimeEventManager* m_timemgr;

KX_PythonComponentManager m_componentManager;

/**
* physics engine abstraction
*/
Expand Down Expand Up @@ -378,6 +381,8 @@ class KX_Scene : public EXP_Value, public SCA_IScene

SCA_TimeEventManager *GetTimeEventManager() const;

KX_PythonComponentManager& GetPythonComponentManager();

EXP_ListValue<KX_Camera> *GetCameraList() const;
EXP_ListValue<KX_FontObject> *GetFontList() const;

Expand Down

0 comments on commit 76aff7a

Please sign in to comment.