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