Khaotic Engine Reborn
Loading...
Searching...
No Matches
render_component.h
1#pragma once
2#include "../component.h"
3#include "model_class.h"
4#include <memory>
5#include <string>
6#include <map>
7#include <WICTextureLoader.h>
8
9// Déclaration externe de la variable globale définie dans application_class.h
14extern std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
15
16namespace ecs {
20 enum class TextureType
21 {
22 Diffuse,
23 Normal,
24 Specular,
25 Alpha,
26 Reflection
27 };
28
29class RenderComponent : public Component {
30public:
34 RenderComponent() : m_model(nullptr), m_isVisible(true) {}
35 ~RenderComponent() = default;
36
37 void Initialize() override {}
38 void Update(float deltaTime) override {}
39
46 bool InitializeWithModel(std::shared_ptr<model_class> model) {
47 if (!model) return false;
48 m_model = model;
49 return true;
50 }
51
61 bool InitializeFromFile(ID3D11Device* device, ID3D11DeviceContext* deviceContext,
62 const char* modelFilename, TextureContainer& textureContainer) {
63 // Vérifier si le modèle existe déjà dans le cache
64 std::string filename(modelFilename);
65 auto it = g_model_cache.find(filename);
66 if (it != g_model_cache.end()) {
67 m_model = it->second;
68 } else {
69 // Créer un nouveau modèle
70 auto new_model = std::make_shared<model_class>();
71 if (!new_model->Initialize(device, deviceContext, const_cast<char*>(modelFilename), textureContainer)) {
72 return false;
73 }
74 g_model_cache[filename] = new_model;
75 m_model = new_model;
76 }
77
78 m_modelFilePath = modelFilename;
79 return true;
80 }
81
91 bool LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
92 ID3D11Device* device, ID3D11DeviceContext* deviceContext) {
93 HRESULT result;
94
95 int i = 0;
96 for (const auto& texturePath : texturePaths) {
97 ID3D11ShaderResourceView* texture = nullptr;
98 result = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
99 if (FAILED(result)) {
100 return false;
101 }
102 texturesContainer.AssignTexture(texturesContainer, texture, texturePath, i);
103 i++;
104 }
105
106 return true;
107 }
108
113 std::shared_ptr<model_class> GetModel() const { return m_model; }
119 void SetModel(std::shared_ptr<model_class> model) { m_model = model; }
120
125 const std::string& GetModelFilePath() const { return m_modelFilePath; }
131 void SetModelFilePath(const std::string& path) { m_modelFilePath = path; }
132
137 bool IsVisible() const { return m_isVisible; }
143 void SetVisible(bool visible) { m_isVisible = visible; }
144
152 ID3D11ShaderResourceView* GetTexture(TextureType type, int index = 0) {
153 if (!m_model) return nullptr;
154
155 switch (type) {
156 case TextureType::Diffuse:
157 return m_model->GetTexture(::TextureType::Diffuse, index);
158 case TextureType::Normal:
159 return m_model->GetTexture(::TextureType::Normal, index);
160 case TextureType::Specular:
161 return m_model->GetTexture(::TextureType::Specular, index);
162 case TextureType::Alpha:
163 return m_model->GetTexture(::TextureType::Alpha, index);
164 default:
165 return nullptr;
166 }
167 }
168
174 int GetIndexCount() const {
175 return m_model ? m_model->GetIndexCount() : 0;
176 }
177
183 void Render(ID3D11DeviceContext* deviceContext) {
184 if (m_model && m_isVisible) {
185 m_model->Render(deviceContext);
186 }
187 }
188
189private:
190 std::shared_ptr<model_class> m_model;
191 std::string m_modelFilePath;
192 bool m_isVisible;
193};
194
195} // namespace ecs
bool InitializeFromFile(ID3D11Device *device, ID3D11DeviceContext *deviceContext, const char *modelFilename, TextureContainer &textureContainer)
std::shared_ptr< model_class > GetModel() const
bool InitializeWithModel(std::shared_ptr< model_class > model)
void Render(ID3D11DeviceContext *deviceContext)
bool LoadTexturesFromPath(std::vector< std::wstring > &texturePaths, TextureContainer &texturesContainer, ID3D11Device *device, ID3D11DeviceContext *deviceContext)
void Update(float deltaTime) override
ID3D11ShaderResourceView * GetTexture(TextureType type, int index=0)
void Initialize() override
const std::string & GetModelFilePath() const
void SetModel(std::shared_ptr< model_class > model)
void SetModelFilePath(const std::string &path)
void SetVisible(bool visible)
Definition component.h:9