ComponentManager增加自动释放功能
This commit is contained in:
parent
c1420e257d
commit
cfda1fceb2
@ -42,8 +42,6 @@ private:
|
||||
|
||||
Pipeline * pipeline =nullptr;
|
||||
|
||||
StaticMeshComponent *sm_component=nullptr;
|
||||
|
||||
private:
|
||||
|
||||
bool InitMaterial()
|
||||
@ -89,33 +87,16 @@ private:
|
||||
{VAN::Color, COLOR_DATA_FORMAT, color_data}
|
||||
});
|
||||
|
||||
return(mesh_triangle);
|
||||
}
|
||||
|
||||
bool InitScene()
|
||||
{
|
||||
if(!mesh_triangle)
|
||||
return(false);
|
||||
|
||||
SceneNode *scene_root=GetSceneRoot(); ///<取得场景根节点
|
||||
|
||||
sm_component=CreateComponent<StaticMeshComponent>(scene_root,mesh_triangle); //创建一个静态网格组件
|
||||
|
||||
if(!sm_component)
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
return CreateComponent<StaticMeshComponent>(GetSceneRoot(),mesh_triangle); //创建一个静态网格组件
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
using WorkObject::WorkObject;
|
||||
|
||||
~TestApp()
|
||||
{
|
||||
SAFE_CLEAR(sm_component);
|
||||
}
|
||||
|
||||
bool Init() override
|
||||
{
|
||||
if(!InitMaterial())
|
||||
@ -124,9 +105,6 @@ public:
|
||||
if(!InitVBO())
|
||||
return(false);
|
||||
|
||||
if(!InitScene())
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class TestApp:public WorkObject
|
||||
|
@ -58,10 +58,24 @@ public:
|
||||
*/
|
||||
class Component
|
||||
{
|
||||
static uint unique_id_count;
|
||||
|
||||
uint unique_id;
|
||||
|
||||
SceneNode * OwnerNode;
|
||||
ComponentManager * Manager;
|
||||
ComponentData * Data;
|
||||
|
||||
protected:
|
||||
|
||||
friend class ComponentManager;
|
||||
|
||||
virtual void OnDetachManager(ComponentManager *cm)
|
||||
{
|
||||
if(cm==Manager)
|
||||
Manager=nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
Component()=delete;
|
||||
@ -72,6 +86,8 @@ public:
|
||||
|
||||
public:
|
||||
|
||||
uint GetUniqueID ()const{return unique_id;}
|
||||
|
||||
SceneNode * GetOwnerNode()const{return OwnerNode;}
|
||||
ComponentManager * GetManager ()const{return Manager;}
|
||||
ComponentData * GetData ()const{return Data;}
|
||||
@ -83,7 +99,7 @@ public:
|
||||
public: //事件
|
||||
|
||||
virtual void OnAttach(SceneNode *node){if(node)OwnerNode=node;} ///<附加到节点事件
|
||||
virtual void OnDetach(SceneNode *node){OwnerNode=nullptr;} ///<从节点分离事件
|
||||
virtual void OnDetach(SceneNode *){OwnerNode=nullptr;} ///<从节点分离事件
|
||||
|
||||
virtual void OnFocusLost(){} ///<焦点丢失事件
|
||||
virtual void OnFocusGained(){} ///<焦点获得事件
|
||||
@ -108,17 +124,17 @@ public:
|
||||
virtual const size_t GetComponentHashCode()const=0;
|
||||
virtual const size_t GetHashCode()const=0;
|
||||
|
||||
virtual ~ComponentManager()=default;
|
||||
virtual ~ComponentManager();
|
||||
|
||||
public:
|
||||
|
||||
virtual Component * CreateComponent(ComponentData *)=0;
|
||||
|
||||
int GetComponentCount()const{return component_set.GetCount();}
|
||||
const size_t GetComponentCount()const{return component_set.GetCount();}
|
||||
|
||||
ComponentSet & GetComponents(){return component_set;}
|
||||
|
||||
int GetComponents(ArrayList<Component *> &comp_list,SceneNode *);
|
||||
int GetComponents(ComponentList &comp_list,SceneNode *);
|
||||
|
||||
virtual void UpdateComponents(const double delta_time);
|
||||
|
||||
|
@ -57,7 +57,7 @@ namespace hgl::graph
|
||||
|
||||
public:
|
||||
|
||||
virtual ~SceneNode()=default;
|
||||
virtual ~SceneNode();
|
||||
|
||||
void Clear() override
|
||||
{
|
||||
@ -106,8 +106,8 @@ namespace hgl::graph
|
||||
|
||||
public: //组件相关方法
|
||||
|
||||
bool ComponentIsEmpty ()const{return component_set.GetCount()==0;} ///<是否没有组件
|
||||
virtual int GetComponentCount ()const{return component_set.GetCount();} ///<取得组件数量
|
||||
bool ComponentIsEmpty ()const{return component_set.IsEmpty();} ///<是否没有组件
|
||||
virtual const int64 GetComponentCount ()const{return component_set.GetCount();} ///<取得组件数量
|
||||
virtual bool AttachComponent (Component *comp) ///<添加一个组件
|
||||
{
|
||||
if(!comp)return(false);
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef HGL_GRAPH_VULKAN_MATERIAL_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_MATERIAL_INCLUDE
|
||||
#pragma once
|
||||
|
||||
#include<hgl/graph/VK.h>
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/type/SortedSet.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/graph/VKShaderModuleMap.h>
|
||||
#include<hgl/graph/mtl/ShaderBufferSource.h>
|
||||
@ -106,6 +106,5 @@ public:
|
||||
MaterialInstance *CreateMI(const VILConfig *vil_cfg=nullptr);
|
||||
};//class Material
|
||||
|
||||
using MaterialSets=SortedSet<Material *>;
|
||||
using MaterialSet=SortedSet<Material *>;
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include<hgl/graph/VK.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/type/SortedSet.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
class VulkanPhyDevice
|
||||
|
@ -1,9 +1,7 @@
|
||||
#ifndef HGL_GRAPH_VULKAN_SHADER_MODULE_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_SHADER_MODULE_INCLUDE
|
||||
#pragma once
|
||||
|
||||
#include<hgl/graph/VK.h>
|
||||
#include<hgl/graph/VKVertexInputLayout.h>
|
||||
#include<hgl/type/SortedSet.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
|
||||
@ -46,4 +44,3 @@ public:
|
||||
operator VkShaderModule ()const{return stage_create_info->module;}
|
||||
};//class ShaderModule
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_VULKAN_SHADER_MODULE_INCLUDE
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include<hgl/graph/VK.h>
|
||||
#include<hgl/graph/VKDescriptorSetType.h>
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/type/SortedSet.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
struct PipelineLayoutData
|
||||
{
|
||||
@ -21,4 +21,4 @@ public:
|
||||
|
||||
~PipelineLayoutData();
|
||||
};//class PipelineLayoutData
|
||||
VK_NAMESPACE_END
|
||||
VK_NAMESPACE_END
|
||||
|
@ -1,26 +1,36 @@
|
||||
#include<hgl/component/Component.h>
|
||||
#include<hgl/graph/SceneNode.h>
|
||||
#include<hgl/log/LogInfo.h>
|
||||
|
||||
namespace hgl::graph
|
||||
{
|
||||
uint Component::unique_id_count=0;
|
||||
|
||||
Component::Component(ComponentData *cd,ComponentManager *cm)
|
||||
{
|
||||
unique_id=++unique_id_count;
|
||||
|
||||
OwnerNode=nullptr;
|
||||
Data=cd;
|
||||
Manager=cm;
|
||||
|
||||
Manager->AttachComponent(this);
|
||||
|
||||
LOG_INFO(AnsiString("Component::Component ")+AnsiString::numberOf(unique_id)+AnsiString(" ")+ToHexString<char>(this));
|
||||
}
|
||||
|
||||
Component::~Component()
|
||||
{
|
||||
LOG_INFO(AnsiString("Component::~Component ")+AnsiString::numberOf(unique_id)+AnsiString(" ")+ToHexString<char>(this));
|
||||
|
||||
if(OwnerNode)
|
||||
{
|
||||
OwnerNode->DetachComponent(this);
|
||||
OwnerNode=nullptr;
|
||||
}
|
||||
|
||||
Manager->DetachComponent(this);
|
||||
if(Manager)
|
||||
Manager->DetachComponent(this);
|
||||
|
||||
SAFE_CLEAR(Data);
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include<hgl/component/Component.h>
|
||||
#include<tsl/robin_map.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/log/LogInfo.h>
|
||||
|
||||
namespace hgl::graph
|
||||
{
|
||||
@ -59,20 +61,33 @@ namespace hgl::graph
|
||||
return component_manager_map->at(hash_code);
|
||||
}
|
||||
|
||||
int ComponentManager::GetComponents(ArrayList<Component *> &comp_list,SceneNode *node)
|
||||
ComponentManager::~ComponentManager()
|
||||
{
|
||||
for(auto *c:component_set)
|
||||
{
|
||||
c->OnDetach(nullptr);
|
||||
|
||||
//Component::~Component()函数会再次调用ComponentManager->DetachComponent,其中会执行component_set的删除,整个流程就会出现问题。
|
||||
//所以这里先OnDetachManager掉Component中的Manager属性,然后再执行删除
|
||||
c->OnDetachManager(this);
|
||||
|
||||
LOG_INFO(AnsiString("~ComponentManager delete ")+AnsiString::numberOf(c->GetUniqueID()));
|
||||
delete c;
|
||||
}
|
||||
}
|
||||
|
||||
int ComponentManager::GetComponents(ComponentList &comp_list,SceneNode *node)
|
||||
{
|
||||
if(!node)return(-1);
|
||||
if(comp_list.IsEmpty())return(-2);
|
||||
if(!component_manager_map)return(-3);
|
||||
|
||||
Component **cc=component_set.GetData();
|
||||
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<component_set.GetCount();i++)
|
||||
if(cc[i]->GetOwnerNode()==node)
|
||||
for(auto cc:component_set)
|
||||
if(cc->GetOwnerNode()==node)
|
||||
{
|
||||
comp_list.Add(cc[i]);
|
||||
comp_list.Add(cc);
|
||||
++result;
|
||||
}
|
||||
|
||||
|
@ -126,4 +126,12 @@ namespace hgl::graph
|
||||
|
||||
return(false);
|
||||
}
|
||||
|
||||
SceneNode::~SceneNode()
|
||||
{
|
||||
for(Component *c:component_set)
|
||||
{
|
||||
c->OnDetach(this);
|
||||
}
|
||||
}
|
||||
}//namespace hgl::graph
|
||||
|
Loading…
x
Reference in New Issue
Block a user