Major - ECS - V12.0.0
This commit is contained in:
124
enginecustom/src/inc/system/ecs/components/render_component.h
Normal file
124
enginecustom/src/inc/system/ecs/components/render_component.h
Normal file
@@ -0,0 +1,124 @@
|
||||
#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 {}
|
||||
|
||||
// Initialisation avec un mod<6F>le existant
|
||||
bool InitializeWithModel(std::shared_ptr<model_class> model) {
|
||||
if (!model) return false;
|
||||
m_model = model;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Initialisation avec un chemin de fichier
|
||||
bool InitializeFromFile(ID3D11Device* device, ID3D11DeviceContext* deviceContext,
|
||||
const char* modelFilename, TextureContainer& textureContainer) {
|
||||
// V<>rifier si le mod<6F>le existe d<>j<EFBFBD> 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<43>er un nouveau mod<6F>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;
|
||||
}
|
||||
|
||||
// Charger des textures depuis un chemin
|
||||
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;
|
||||
}
|
||||
|
||||
// Getters et setters
|
||||
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; }
|
||||
|
||||
// Acc<63>s aux textures
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Pour le rendu
|
||||
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
|
||||
Reference in New Issue
Block a user