# Khaotic Engine Reborn: enginecustom/src/inc/system/ecs/components/render_component.h Source File

Loading...
Searching...
No Matches
render_component.h
#pragma once
#include "../component.h"
#include "model_class.h"
#include <memory>
#include <string>
#include <map>
#include <WICTextureLoader.h>
// Déclaration externe de la variable globale définie dans application_class.h
extern std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
namespace ecs {
enum class TextureType
{
Diffuse,
Normal,
Specular,
Alpha,
Reflection
};
class RenderComponent : public Component {
public:
RenderComponent() : m_model(nullptr), m_isVisible(true) {}
~RenderComponent() = default;
void Initialize() override {}
void Update(float deltaTime) override {}
bool InitializeWithModel(std::shared_ptr<model_class> model) {
if (!model) return false;
m_model = model;
return true;
}
bool InitializeFromFile(ID3D11Device* device, ID3D11DeviceContext* deviceContext,
const char* modelFilename, TextureContainer& textureContainer) {
// Vérifier si le modèle existe déjà dans le cache
std::string filename(modelFilename);
auto it = g_model_cache.find(filename);
if (it != g_model_cache.end()) {
m_model = it->second;
} else {
// Créer un nouveau modèle
auto new_model = std::make_shared<model_class>();
if (!new_model->Initialize(device, deviceContext, const_cast<char*>(modelFilename), textureContainer)) {
return false;
}
g_model_cache[filename] = new_model;
m_model = new_model;
}
m_modelFilePath = modelFilename;
return true;
}
bool LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
ID3D11Device* device, ID3D11DeviceContext* deviceContext) {
HRESULT result;
int i = 0;
for (const auto& texturePath : texturePaths) {
ID3D11ShaderResourceView* texture = nullptr;
result = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
if (FAILED(result)) {
return false;
}
texturesContainer.AssignTexture(texturesContainer, texture, texturePath, i);
i++;
}
return true;
}
std::shared_ptr<model_class> GetModel() const { return m_model; }
void SetModel(std::shared_ptr<model_class> model) { m_model = model; }
const std::string& GetModelFilePath() const { return m_modelFilePath; }
void SetModelFilePath(const std::string& path) { m_modelFilePath = path; }
bool IsVisible() const { return m_isVisible; }
void SetVisible(bool visible) { m_isVisible = visible; }
ID3D11ShaderResourceView* GetTexture(TextureType type, int index = 0) {
if (!m_model) return nullptr;
switch (type) {
case TextureType::Diffuse:
return m_model->GetTexture(::TextureType::Diffuse, index);
case TextureType::Normal:
return m_model->GetTexture(::TextureType::Normal, index);
case TextureType::Specular:
return m_model->GetTexture(::TextureType::Specular, index);
case TextureType::Alpha:
return m_model->GetTexture(::TextureType::Alpha, index);
default:
return nullptr;
}
}
int GetIndexCount() const {
return m_model ? m_model->GetIndexCount() : 0;
}
void Render(ID3D11DeviceContext* deviceContext) {
if (m_model && m_isVisible) {
m_model->Render(deviceContext);
}
}
private:
std::shared_ptr<model_class> m_model;
std::string m_modelFilePath;
bool m_isVisible;
};
} // namespace ecs
ecs::Component
Definition component.h:12
ecs::RenderComponent
Definition render_component.h:29
ecs::RenderComponent::InitializeFromFile
bool InitializeFromFile(ID3D11Device *device, ID3D11DeviceContext *deviceContext, const char *modelFilename, TextureContainer &textureContainer)
Definition render_component.h:61
ecs::RenderComponent::GetModel
std::shared_ptr< model_class > GetModel() const
Definition render_component.h:113
ecs::RenderComponent::InitializeWithModel
bool InitializeWithModel(std::shared_ptr< model_class > model)
Definition render_component.h:46
ecs::RenderComponent::Render
void Render(ID3D11DeviceContext *deviceContext)
Definition render_component.h:183
ecs::RenderComponent::LoadTexturesFromPath
bool LoadTexturesFromPath(std::vector< std::wstring > &texturePaths, TextureContainer &texturesContainer, ID3D11Device *device, ID3D11DeviceContext *deviceContext)
Definition render_component.h:91
ecs::RenderComponent::Update
void Update(float deltaTime) override
Definition render_component.h:38
ecs::RenderComponent::GetTexture
ID3D11ShaderResourceView * GetTexture(TextureType type, int index=0)
Definition render_component.h:152
ecs::RenderComponent::RenderComponent
RenderComponent()
Definition render_component.h:34
ecs::RenderComponent::Initialize
void Initialize() override
Definition render_component.h:37
ecs::RenderComponent::GetModelFilePath
const std::string & GetModelFilePath() const
Definition render_component.h:125
ecs::RenderComponent::SetModel
void SetModel(std::shared_ptr< model_class > model)
Definition render_component.h:119
ecs::RenderComponent::SetModelFilePath
void SetModelFilePath(const std::string &path)
Definition render_component.h:131
ecs::RenderComponent::IsVisible
bool IsVisible() const
Definition render_component.h:137
ecs::RenderComponent::GetIndexCount
int GetIndexCount() const
Definition render_component.h:174
ecs::RenderComponent::SetVisible
void SetVisible(bool visible)
Definition render_component.h:143
ecs
Definition component.h:9
ecs::TextureType
TextureType
Definition render_component.h:21
TextureContainer
Definition model_class.h:34