改进Component/Manager设计,在WorkObject封装Component相关模板函数,以减化使用部分。当前版本可运行,下一版迁移旧的Renderable渲染到StaticMeshComponent.
This commit is contained in:
parent
edaf5aa3ca
commit
92f612f675
@ -91,11 +91,7 @@ private:
|
|||||||
|
|
||||||
SceneNode *scene_root=GetSceneRoot(); ///<取得场景根节点
|
SceneNode *scene_root=GetSceneRoot(); ///<取得场景根节点
|
||||||
|
|
||||||
auto manager=StaticMeshComponentManager::GetDefaultManager();
|
sm_component=CreateComponent<StaticMeshComponent>(scene_root,mesh_triangle); //创建一个静态网格组件
|
||||||
|
|
||||||
sm_component=manager->CreateStaticMeshComponent(mesh_triangle);
|
|
||||||
|
|
||||||
scene_root->AttachComponent(sm_component);
|
|
||||||
|
|
||||||
scene_root->Add(new SceneNode(mesh_triangle));
|
scene_root->Add(new SceneNode(mesh_triangle));
|
||||||
|
|
||||||
|
@ -122,6 +122,44 @@ namespace hgl
|
|||||||
graph::MaterialInstance *mi,
|
graph::MaterialInstance *mi,
|
||||||
graph::Pipeline *pipeline,
|
graph::Pipeline *pipeline,
|
||||||
const std::initializer_list<graph::VertexAttribDataPtr> &vad_list);
|
const std::initializer_list<graph::VertexAttribDataPtr> &vad_list);
|
||||||
|
|
||||||
|
public: //Component 相关
|
||||||
|
|
||||||
|
template<typename C,typename ...ARGS>
|
||||||
|
inline C *CreateComponent(ARGS...args)
|
||||||
|
{
|
||||||
|
auto manager=C::GetDefaultManager(); //取得默认管理器
|
||||||
|
|
||||||
|
if(!manager)
|
||||||
|
{
|
||||||
|
// LOG_ERROR(OS_TEXT("CreateComponent failed, no default manager!"));
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return manager->CreateComponent(args...); //创建组件
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename C,typename ...ARGS>
|
||||||
|
inline C *CreateComponent(graph::SceneNode *parent_node,ARGS...args)
|
||||||
|
{
|
||||||
|
if(!parent_node)
|
||||||
|
{
|
||||||
|
// LOG_ERROR(OS_TEXT("CreateComponent failed, parent node is null!"));
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
C *c=this->CreateComponent<C>(args...); //创建组件
|
||||||
|
|
||||||
|
if(!c)
|
||||||
|
{
|
||||||
|
// LOG_ERROR(OS_TEXT("CreateComponent failed, create component failed!"));
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent_node->AttachComponent(c); //将组件附加到父节点
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
};//class WorkObject
|
};//class WorkObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,13 +45,13 @@ public:
|
|||||||
|
|
||||||
StaticMeshComponentManager()=default;
|
StaticMeshComponentManager()=default;
|
||||||
|
|
||||||
StaticMeshComponent *CreateStaticMeshComponent(StaticMeshComponentData *data);
|
StaticMeshComponent *CreateComponent(StaticMeshComponentData *data);
|
||||||
|
|
||||||
StaticMeshComponent *CreateStaticMeshComponent(Mesh *m)
|
StaticMeshComponent *CreateComponent(Mesh *m)
|
||||||
{
|
{
|
||||||
auto sm_cd=new StaticMeshComponentData(m);
|
auto sm_cd=new StaticMeshComponentData(m);
|
||||||
|
|
||||||
return CreateStaticMeshComponent(sm_cd);
|
return CreateComponent(sm_cd);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Component *CreateComponent(ComponentData *data) override;
|
virtual Component *CreateComponent(ComponentData *data) override;
|
||||||
@ -71,6 +71,11 @@ public:
|
|||||||
|
|
||||||
virtual ~StaticMeshComponent()=default;
|
virtual ~StaticMeshComponent()=default;
|
||||||
|
|
||||||
|
static StaticMeshComponentManager *GetDefaultManager()
|
||||||
|
{
|
||||||
|
return StaticMeshComponentManager::GetDefaultManager();
|
||||||
|
}
|
||||||
|
|
||||||
static constexpr const size_t StaticHashCode()
|
static constexpr const size_t StaticHashCode()
|
||||||
{
|
{
|
||||||
return hgl::GetTypeHash<StaticMeshComponent>();
|
return hgl::GetTypeHash<StaticMeshComponent>();
|
||||||
|
@ -55,7 +55,7 @@ namespace hgl::graph
|
|||||||
if(!component_manager_map->contains(hash_code))
|
if(!component_manager_map->contains(hash_code))
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
//[]对于不存的会自行插入一个,所以不要把下面的.at改成[]
|
//tsl::robin_map的[]对于不存在的会自行插入一个,所以不要把下面的.at改成[]
|
||||||
return component_manager_map->at(hash_code);
|
return component_manager_map->at(hash_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,10 +17,10 @@ Component *StaticMeshComponentManager::CreateComponent(ComponentData *data)
|
|||||||
{
|
{
|
||||||
if(!data)return(nullptr);
|
if(!data)return(nullptr);
|
||||||
|
|
||||||
return CreateStaticMeshComponent(reinterpret_cast<StaticMeshComponentData *>(data));
|
return CreateComponent(reinterpret_cast<StaticMeshComponentData *>(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticMeshComponent *StaticMeshComponentManager::CreateStaticMeshComponent(StaticMeshComponentData *data)
|
StaticMeshComponent *StaticMeshComponentManager::CreateComponent(StaticMeshComponentData *data)
|
||||||
{
|
{
|
||||||
if(!data)return(nullptr);
|
if(!data)return(nullptr);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user