Minor - Improves scene loading and audio component - V13.6.0
Refactors scene loading to properly initialize models and textures from the scene file. Adds audio component deserialization and file path handling. Fixes model loading and texture application in scene manager. Addresses potential issues with missing components during scene loading.
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include <imgui.h>
|
||||
#include "entity.h"
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
/**
|
||||
* namespace for the Entity-Component-System (ECS)
|
||||
*/
|
||||
|
||||
@@ -18,6 +18,14 @@ class AudioComponent : public Component {
|
||||
public:
|
||||
AudioComponent() : m_sound(nullptr), m_channel(nullptr) {}
|
||||
~AudioComponent() override {
|
||||
// stop the sound if it's playing
|
||||
if (m_channel) {
|
||||
bool isPlaying = false;
|
||||
m_channel->isPlaying(&isPlaying);
|
||||
if (isPlaying) {
|
||||
m_channel->stop();
|
||||
}
|
||||
}
|
||||
if (m_sound) m_sound->release();
|
||||
// Ne pas lib<69>rer m_system ici car il peut <20>tre partag<61>
|
||||
}
|
||||
@@ -51,10 +59,13 @@ public:
|
||||
if (!m_system) return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Loading audio file: " + path, __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
|
||||
m_soundPath = path;
|
||||
|
||||
if (!std::filesystem::exists(path)) {
|
||||
m_lastError = "Fichier non trouv<75>: " + path;
|
||||
Logger::Get().Log(m_lastError, __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -437,7 +448,7 @@ public:
|
||||
std::string Serialize() const override {
|
||||
std::stringstream ss;
|
||||
ss << "AudioComponent:"
|
||||
<< m_soundPath << ":"
|
||||
<< m_soundPath << ";"
|
||||
<< m_volume << ":"
|
||||
<< m_pan << ":"
|
||||
<< m_pitch << ":"
|
||||
@@ -465,7 +476,7 @@ public:
|
||||
|
||||
std::string s_volume, s_pan, s_pitch, s_looping, s_muted, s_paused, s_priority, s_spatialized, s_use_velocity;
|
||||
|
||||
std::getline(ss, m_soundPath, ':');
|
||||
std::getline(ss, m_soundPath, ';');
|
||||
std::getline(ss, s_volume, ':');
|
||||
std::getline(ss, s_pan, ':');
|
||||
std::getline(ss, s_pitch, ':');
|
||||
@@ -476,6 +487,8 @@ public:
|
||||
std::getline(ss, s_spatialized, ':');
|
||||
std::getline(ss, s_use_velocity, ':');
|
||||
|
||||
Logger::Get().Log("Deserializing AudioComponent: path=" + m_soundPath, __FILE__, __LINE__, Logger::LogLevel::Warning);
|
||||
|
||||
m_volume = std::stof(s_volume);
|
||||
m_pan = std::stof(s_pan);
|
||||
m_pitch = std::stof(s_pitch);
|
||||
|
||||
@@ -35,6 +35,9 @@ public:
|
||||
SetDevice(parent->GetDevice());
|
||||
SetContext(parent->GetContext());
|
||||
}
|
||||
|
||||
texture_container_buffer = TextureContainer();
|
||||
|
||||
}
|
||||
void Update(float deltaTime) override {}
|
||||
|
||||
@@ -194,12 +197,42 @@ public:
|
||||
*/
|
||||
std::string Serialize() const override {
|
||||
if (!m_model) return "RenderComponent:NoModel";
|
||||
|
||||
|
||||
TextureContainer texture_container_buffer = m_model->GetTextureContainer();
|
||||
|
||||
auto serializePaths = [](const std::vector<std::wstring> &paths) -> std::string {
|
||||
std::string result;
|
||||
for (size_t i = 0; i < paths.size(); ++i) {
|
||||
std::string p = std::string(paths[i].begin(), paths[i].end());
|
||||
result += p;
|
||||
if (i + 1 < paths.size()) {
|
||||
result += ","; // virgule entre chemins mais pas apr<70>s dernier
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
int diffuse_count = (int)texture_container_buffer.diffuse.size();
|
||||
int normal_count = (int)texture_container_buffer.normal.size();
|
||||
int specular_count = (int)texture_container_buffer.specular.size();
|
||||
int alpha_count = (int)texture_container_buffer.alpha.size();
|
||||
|
||||
std::string diffuse_paths_ = serializePaths(texture_container_buffer.diffusePaths);
|
||||
std::string normal_paths_ = serializePaths(texture_container_buffer.normalPaths);
|
||||
std::string specular_paths_ = serializePaths(texture_container_buffer.specularPaths);
|
||||
std::string alpha_paths_ = serializePaths(texture_container_buffer.alphaPaths);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "RenderComponent:HasModel";
|
||||
ss << "RenderComponent:HasModel:"
|
||||
<< diffuse_count << ":" << diffuse_paths_ << ":"
|
||||
<< normal_count << ":" << normal_paths_ << ":"
|
||||
<< specular_count << ":" << specular_paths_ << ":"
|
||||
<< alpha_count << ":" << alpha_paths_;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserialize the RenderComponent's state from a string.
|
||||
* This method parses the string and sets the component's properties accordingly.
|
||||
@@ -210,14 +243,81 @@ public:
|
||||
bool Deserialize(const std::string& data) override {
|
||||
std::stringstream ss(data);
|
||||
std::string type;
|
||||
std::getline(ss, type, ':');
|
||||
|
||||
if (type != "RenderComponent") return false;
|
||||
|
||||
std::string hasModel;
|
||||
std::getline(ss, hasModel);
|
||||
|
||||
// Le mod<6F>le sera charg<72> s<>par<61>ment
|
||||
|
||||
if (!std::getline(ss, type, ':') || type != "RenderComponent")
|
||||
return false;
|
||||
|
||||
std::string token;
|
||||
|
||||
if (!std::getline(ss, token, ':') || token != "HasModel")
|
||||
return false;
|
||||
|
||||
int diffuse_count = 0, normal_count = 0, specular_count = 0, alpha_count = 0;
|
||||
|
||||
std::vector<std::wstring> paths_diffuse, paths_normal, paths_specular, paths_alpha;
|
||||
|
||||
auto read_count = [&](int& count) -> bool {
|
||||
if (!std::getline(ss, token, ':'))
|
||||
return false;
|
||||
try {
|
||||
count = std::stoi(token);
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
auto clean_token = [](std::string& str) {
|
||||
// Retirer virgules et espaces
|
||||
str.erase(std::remove(str.begin(), str.end(), ','), str.end());
|
||||
str.erase(0, str.find_first_not_of(" \t\n\r"));
|
||||
str.erase(str.find_last_not_of(" \t\n\r") + 1);
|
||||
};
|
||||
|
||||
auto read_paths = [&](int count, std::vector<std::wstring>& paths) -> bool {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
if (!std::getline(ss, token, ':'))
|
||||
return false;
|
||||
|
||||
clean_token(token);
|
||||
|
||||
if (!token.empty()) {
|
||||
paths.emplace_back(token.begin(), token.end());
|
||||
Logger::Get().Log("Loaded path: " + std::string(token.begin(), token.end()), __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!read_count(diffuse_count)) return false;
|
||||
if (!read_paths(diffuse_count, paths_diffuse)) return false;
|
||||
|
||||
if (!read_count(normal_count)) return false;
|
||||
if (!read_paths(normal_count, paths_normal)) return false;
|
||||
|
||||
if (!read_count(specular_count)) return false;
|
||||
if (!read_paths(specular_count, paths_specular)) return false;
|
||||
|
||||
if (!read_count(alpha_count)) return false;
|
||||
if (!read_paths(alpha_count, paths_alpha)) return false;
|
||||
|
||||
// Lib<69>rer textures existantes
|
||||
for (auto& tex : texture_container_buffer.diffuse) if (tex) { tex->Release(); tex = nullptr; }
|
||||
for (auto& tex : texture_container_buffer.normal) if (tex) { tex->Release(); tex = nullptr; }
|
||||
for (auto& tex : texture_container_buffer.specular) if (tex) { tex->Release(); tex = nullptr; }
|
||||
for (auto& tex : texture_container_buffer.alpha) if (tex) { tex->Release(); tex = nullptr; }
|
||||
|
||||
texture_container_buffer.diffuse.clear();
|
||||
texture_container_buffer.normal.clear();
|
||||
texture_container_buffer.specular.clear();
|
||||
texture_container_buffer.alpha.clear();
|
||||
|
||||
texture_container_buffer.diffusePaths = std::move(paths_diffuse);
|
||||
texture_container_buffer.normalPaths = std::move(paths_normal);
|
||||
texture_container_buffer.specularPaths = std::move(paths_specular);
|
||||
texture_container_buffer.alphaPaths = std::move(paths_alpha);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -350,12 +450,16 @@ public:
|
||||
void SetDevice (ID3D11Device* dev) { device = dev; }
|
||||
void SetContext(ID3D11DeviceContext* ctx) { context = ctx; }
|
||||
|
||||
void SetTextureContainer(const TextureContainer& texContainer) { texture_container_buffer = texContainer; }
|
||||
const TextureContainer& GetTextureContainerBuffer() const { return texture_container_buffer; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<model_class> m_model;
|
||||
std::string m_modelFilePath;
|
||||
bool m_isVisible;
|
||||
ID3D11Device* device;
|
||||
ID3D11DeviceContext* context;
|
||||
TextureContainer texture_container_buffer;
|
||||
};
|
||||
|
||||
} // namespace ecs
|
||||
|
||||
Reference in New Issue
Block a user