Khaotic Engine Reborn
Loading...
Searching...
No Matches
ecs::RenderComponent Class Reference
Inheritance diagram for ecs::RenderComponent:
ecs::Component

Public Member Functions

 RenderComponent ()
 
void Initialize () override
 
void Update (float deltaTime) override
 
bool InitializeWithModel (std::shared_ptr< model_class > model)
 
bool InitializeFromFile (ID3D11Device *device, ID3D11DeviceContext *deviceContext, const char *modelFilename, TextureContainer &textureContainer)
 
bool LoadTexturesFromPath (std::vector< std::wstring > &texturePaths, TextureContainer &texturesContainer, ID3D11Device *device, ID3D11DeviceContext *deviceContext)
 
std::shared_ptr< model_classGetModel () const
 
void SetModel (std::shared_ptr< model_class > model)
 
const std::string & GetModelFilePath () const
 
void SetModelFilePath (const std::string &path)
 
bool IsVisible () const
 
void SetVisible (bool visible)
 
ID3D11ShaderResourceView * GetTexture (TextureType type, int index=0)
 
int GetIndexCount () const
 
void Render (ID3D11DeviceContext *deviceContext)
 
- Public Member Functions inherited from ecs::Component
 Component (const Component &)=delete
 
Componentoperator= (const Component &)=delete
 
 Component (Component &&)=default
 
Componentoperator= (Component &&)=default
 

Detailed Description

Definition at line 29 of file render_component.h.

Constructor & Destructor Documentation

◆ RenderComponent()

ecs::RenderComponent::RenderComponent ( )
inline

Builder for the RenderComponent class.

Definition at line 34 of file render_component.h.

34: m_model(nullptr), m_isVisible(true) {}

Member Function Documentation

◆ GetIndexCount()

int ecs::RenderComponent::GetIndexCount ( ) const
inline

Get the number of vertices in the model. This method retrieves the vertex count from the model.

Returns
The number of vertices as an integer.

Definition at line 174 of file render_component.h.

174 {
175 return m_model ? m_model->GetIndexCount() : 0;
176 }

◆ GetModel()

std::shared_ptr< model_class > ecs::RenderComponent::GetModel ( ) const
inline

Get the model associated with this RenderComponent.

Returns
A shared pointer to the model_class instance.

Definition at line 113 of file render_component.h.

113{ return m_model; }

◆ GetModelFilePath()

const std::string & ecs::RenderComponent::GetModelFilePath ( ) const
inline

Get the file path of the model associated with this RenderComponent.

Returns
The file path as a string.

Definition at line 125 of file render_component.h.

125{ return m_modelFilePath; }

◆ GetTexture()

ID3D11ShaderResourceView * ecs::RenderComponent::GetTexture ( TextureType type,
int index = 0 )
inline

Get a texture of a specific type by index. This method retrieves the texture from the model based on the specified type and index.

Parameters
typeThe type of texture to retrieve (Diffuse, Normal, Specular, Alpha).
indexThe index of the texture to retrieve (default is 0).
Returns
A pointer to the ID3D11ShaderResourceView of the texture, or nullptr if not found.

Definition at line 152 of file render_component.h.

152 {
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 }

◆ Initialize()

void ecs::RenderComponent::Initialize ( )
inlineoverridevirtual

Virtual function to initialize the component.

Reimplemented from ecs::Component.

Definition at line 37 of file render_component.h.

37{}

◆ InitializeFromFile()

bool ecs::RenderComponent::InitializeFromFile ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
const char * modelFilename,
TextureContainer & textureContainer )
inline

Initialize the RenderComponent from a model file. This method checks if the model is already cached; if not, it loads the model from the specified file.

Parameters
deviceThe Direct3D device used for rendering.
deviceContextThe Direct3D device context used for rendering.
modelFilenameThe path to the model file to load.
textureContainerThe container for textures used by the model.
Returns
True if initialization was successful, false otherwise.

Definition at line 61 of file render_component.h.

62 {
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 }

◆ InitializeWithModel()

bool ecs::RenderComponent::InitializeWithModel ( std::shared_ptr< model_class > model)
inline

Initialize the RenderComponent with a model. This method allows the component to be initialized with an existing model instance.

Parameters
modelA shared pointer to the model_class instance to use.
Returns
True if initialization was successful, false otherwise.

Definition at line 46 of file render_component.h.

46 {
47 if (!model) return false;
48 m_model = model;
49 return true;
50 }

◆ IsVisible()

bool ecs::RenderComponent::IsVisible ( ) const
inline

Check if the model is currently visible.

Returns
True if the model is visible, false otherwise.

Definition at line 137 of file render_component.h.

137{ return m_isVisible; }

◆ LoadTexturesFromPath()

bool ecs::RenderComponent::LoadTexturesFromPath ( std::vector< std::wstring > & texturePaths,
TextureContainer & texturesContainer,
ID3D11Device * device,
ID3D11DeviceContext * deviceContext )
inline

Load textures from a list of file paths into the texture container. This method uses DirectX's WIC texture loader to load textures from the specified paths.

Parameters
texturePathsA vector of file paths to the textures to load.
texturesContainerThe container where the loaded textures will be stored.
deviceThe Direct3D device used for rendering.
deviceContextThe Direct3D device context used for rendering.
Returns
True if all textures were loaded successfully, false otherwise.

Definition at line 91 of file render_component.h.

92 {
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 }

◆ Render()

void ecs::RenderComponent::Render ( ID3D11DeviceContext * deviceContext)
inline

Render the model using the provided device context. This method calls the Render method of the model if it is initialized and visible.

Parameters
deviceContextThe Direct3D device context used for rendering.

Definition at line 183 of file render_component.h.

183 {
184 if (m_model && m_isVisible) {
185 m_model->Render(deviceContext);
186 }
187 }

◆ SetModel()

void ecs::RenderComponent::SetModel ( std::shared_ptr< model_class > model)
inline

Set the model for this RenderComponent. This method allows the component to be set with an existing model instance.

Parameters
modelA shared pointer to the model_class instance to set.

Definition at line 119 of file render_component.h.

119{ m_model = model; }

◆ SetModelFilePath()

void ecs::RenderComponent::SetModelFilePath ( const std::string & path)
inline

Set the file path of the model for this RenderComponent. This method allows the component to be set with a specific model file path.

Parameters
pathThe file path to set as a string.

Definition at line 131 of file render_component.h.

131{ m_modelFilePath = path; }

◆ SetVisible()

void ecs::RenderComponent::SetVisible ( bool visible)
inline

Set the visibility of the model. This method allows the component to control whether the model should be rendered or not.

Parameters
visibleTrue to make the model visible, false to hide it.

Definition at line 143 of file render_component.h.

143{ m_isVisible = visible; }

◆ Update()

void ecs::RenderComponent::Update ( float deltaTime)
inlineoverridevirtual

Virtual function to update the component.

Parameters
deltaTimeTime since the last update.

Reimplemented from ecs::Component.

Definition at line 38 of file render_component.h.

38{}

The documentation for this class was generated from the following file: