Skip to content

Commit

Permalink
UPBGE: Use std::unique_ptr in KX_GameObject.
Browse files Browse the repository at this point in the history
Some pointer in KX_GameObject corresponded to data owned by the
game object or inexistant. In this case std::unique_ptr could be used
to ensure the free of data.

The only drawback is the requierment of a copy constructor to duplicate
the data or set they to nullptr.

Also m_clientInfo is now not anymore a pointer as it is never nullptr
and owned by the game object.
  • Loading branch information
panzergame committed Dec 30, 2017
1 parent cf48458 commit 5a546f6
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 190 deletions.
4 changes: 2 additions & 2 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static void BL_CreateGraphicObjectNew(KX_GameObject *gameobj, KX_Scene *kxscene,
PHY_IMotionState *motionstate = new KX_MotionState(gameobj->GetSGNode());
CcdGraphicController *ctrl = new CcdGraphicController(env, motionstate);
gameobj->SetGraphicController(ctrl);
ctrl->SetNewClientInfo(gameobj->getClientInfo());
ctrl->SetNewClientInfo(&gameobj->GetClientInfo());
if (isActive) {
// add first, this will create the proxy handle, only if the object is visible
if (gameobj->GetVisible()) {
Expand Down Expand Up @@ -684,7 +684,7 @@ static void BL_CreatePhysicsObjectNew(KX_GameObject *gameobj, Object *blenderobj

bool isActor = (blenderobject->gameflag & OB_ACTOR) != 0;
bool isSensor = (blenderobject->gameflag & OB_SENSOR) != 0;
gameobj->getClientInfo()->m_type =
gameobj->GetClientInfo().m_type =
(isSensor) ? ((isActor) ? KX_ClientObjectInfo::OBACTORSENSOR : KX_ClientObjectInfo::OBSENSOR) :
(isActor) ? KX_ClientObjectInfo::ACTOR : KX_ClientObjectInfo::STATIC;
}
Expand Down
18 changes: 14 additions & 4 deletions source/gameengine/GameLogic/SCA_IObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ SCA_IObject::SCA_IObject()
{
}

SCA_IObject::SCA_IObject(const SCA_IObject& other)
:EXP_Value(other),
m_sensors(other.m_sensors),
m_controllers(other.m_controllers),
m_actuators(other.m_actuators),
m_suspended(other.m_suspended),
m_initState(other.m_initState),
m_state(0),
m_firstState(other.m_firstState)
{
/* Registered objects and actuator are intentionally left empty.
* A new object cannot be client of any actuator. */
}

SCA_IObject::~SCA_IObject()
{
for (SCA_ISensor *sensor : m_sensors) {
Expand Down Expand Up @@ -201,10 +215,6 @@ void SCA_IObject::ReParentLogic()
newsensor->ClrLink();
oldsensors[i] = newsensor;
}

// A new object cannot be client of any actuator.
m_registeredActuators.clear();
m_registeredObjects.clear();
}

SCA_ISensor *SCA_IObject::FindSensor(const std::string& sensorname)
Expand Down
1 change: 1 addition & 0 deletions source/gameengine/GameLogic/SCA_IObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class SCA_IObject : public EXP_Value

public:
SCA_IObject();
SCA_IObject(const SCA_IObject& other);
virtual ~SCA_IObject();

SCA_ControllerList& GetControllers();
Expand Down
12 changes: 1 addition & 11 deletions source/gameengine/Ketsji/KX_Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
KX_Camera::KX_Camera(void* sgReplicationInfo,
SG_Callbacks callbacks,
const RAS_CameraData& camdata,
bool frustum_culling,
bool delete_node)
bool frustum_culling)
:
KX_GameObject(sgReplicationInfo,callbacks),
m_camdata(camdata),
Expand All @@ -56,7 +55,6 @@ KX_Camera::KX_Camera(void* sgReplicationInfo,
m_normalized(false),
m_frustum_culling(frustum_culling),
m_set_projection_matrix(false),
m_delete_node(delete_node),
m_lodDistanceFactor(1.0f),
m_activityCulling(false),
m_showDebugCameraFrustum(false)
Expand All @@ -68,12 +66,6 @@ KX_Camera::KX_Camera(void* sgReplicationInfo,

KX_Camera::~KX_Camera()
{
if (m_delete_node && m_sgNode)
{
// for shadow camera, avoids memleak
delete m_sgNode;
m_sgNode = nullptr;
}
}


Expand All @@ -90,8 +82,6 @@ EXP_Value* KX_Camera::GetReplica()
void KX_Camera::ProcessReplica()
{
KX_GameObject::ProcessReplica();
// replicated camera are always registered in the scene
m_delete_node = false;
}

mt::mat3x4 KX_Camera::GetWorldToCamera() const
Expand Down
8 changes: 1 addition & 7 deletions source/gameengine/Ketsji/KX_Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ class KX_Camera : public KX_GameObject
*/
bool m_set_projection_matrix;

/**
* whether the camera should delete the node itself (only for shadow camera)
*/
bool m_delete_node;


/** Distance factor for level of detail*/
float m_lodDistanceFactor;

Expand All @@ -118,7 +112,7 @@ class KX_Camera : public KX_GameObject

enum { INSIDE, INTERSECT, OUTSIDE };

KX_Camera(void* sgReplicationInfo,SG_Callbacks callbacks,const RAS_CameraData& camdata, bool frustum_culling = true, bool delete_node = false);
KX_Camera(void* sgReplicationInfo,SG_Callbacks callbacks,const RAS_CameraData& camdata, bool frustum_culling = true);
virtual ~KX_Camera();

/**
Expand Down
8 changes: 4 additions & 4 deletions source/gameengine/Ketsji/KX_CollisionSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ KX_CollisionSensor::KX_CollisionSensor(SCA_EventManager *eventmgr, KX_GameObject
{
m_colliders = new EXP_ListValue<KX_GameObject>();

KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
client_info->m_sensors.push_back(this);
KX_ClientObjectInfo& clientInfo = gameobj->GetClientInfo();
clientInfo.m_sensors.push_back(this);

m_physCtrl = gameobj->GetPhysicsController();
BLI_assert(!gameobj->GetPhysicsController() || m_physCtrl);
Expand Down Expand Up @@ -155,9 +155,9 @@ void KX_CollisionSensor::ReParent(SCA_IObject *parent)
m_physCtrl = sphy;
}

KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
KX_ClientObjectInfo& clientInfo = gameobj->GetClientInfo();

client_info->m_sensors.push_back(this);
clientInfo.m_sensors.push_back(this);
SCA_ISensor::ReParent(parent);
}

Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_FontObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void KX_FontObject::ProcessReplica()

void KX_FontObject::AddMeshUser()
{
m_meshUser = new RAS_TextUser(m_client_info, m_boundingBox);
m_meshUser = new RAS_TextUser(&m_clientInfo, m_boundingBox);

// Make sure the mesh user get the matrix even if the object doesn't move.
NodeGetWorldTransform().PackFromAffineTransform(m_meshUser->GetMatrix());
Expand Down
Loading

0 comments on commit 5a546f6

Please sign in to comment.