Minor - Enhances ECS component functionality - V13.5.0
Adds serialization/deserialization to components, allowing saving and loading of entity states. Provides ImGui widgets for editing component properties, improving editor usability. Passes the D3D device and context to entities and components enabling resource creation within components.
This commit is contained in:
@@ -22,6 +22,10 @@ public:
|
||||
// Ne pas lib<69>rer m_system ici car il peut <20>tre partag<61>
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the audio component by setting up the FMOD system and camera from the parent entity.
|
||||
* This method is called when the component is added to an entity.
|
||||
*/
|
||||
void Initialize() override
|
||||
{
|
||||
|
||||
@@ -34,6 +38,13 @@ public:
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an audio file from the specified path.
|
||||
* If the FMOD system is not initialized, it will be initialized first.
|
||||
* Creates a sound object with appropriate settings based on the component's properties.
|
||||
* @param path The file path to load the audio from.
|
||||
* @return True if the audio file was loaded successfully, otherwise false.
|
||||
*/
|
||||
bool Load(const std::string& path) {
|
||||
if (!m_system) {
|
||||
Initialize();
|
||||
@@ -72,6 +83,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Play the loaded audio sound.
|
||||
* If the sound is already playing, it will be stopped and restarted.
|
||||
* The channel properties such as volume, pan, pitch, mute, priority, and paused state are applied.
|
||||
*/
|
||||
void Play() {
|
||||
if (m_system && m_sound) {
|
||||
bool isPlaying = false;
|
||||
@@ -96,6 +112,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the currently playing audio sound.
|
||||
* If the sound is not playing, this method has no effect.
|
||||
*/
|
||||
void Pause() {
|
||||
if (m_channel) {
|
||||
m_paused = true;
|
||||
@@ -103,6 +123,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume the paused audio sound.
|
||||
* If the sound is not paused, this method has no effect.
|
||||
*/
|
||||
void Resume() {
|
||||
if (m_channel) {
|
||||
m_paused = false;
|
||||
@@ -110,13 +134,26 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the currently playing audio sound.
|
||||
* If the sound is not playing, this method has no effect.
|
||||
*/
|
||||
void Stop() {
|
||||
if (m_channel)
|
||||
m_channel->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the camera to be used for 3D audio spatialization.
|
||||
* This camera is used to update the listener attributes in FMOD.
|
||||
* @param camera Pointer to the camera_class instance.
|
||||
*/
|
||||
void SetCamera(camera_class* camera) {m_camera = camera; }
|
||||
|
||||
/**
|
||||
* Update the audio component.
|
||||
* @param deltaTime Time since the last update.
|
||||
*/
|
||||
void Update(float deltaTime) override {
|
||||
if (!m_spatialized) return;
|
||||
|
||||
@@ -171,7 +208,12 @@ public:
|
||||
m_system->update();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ImGui Widget to control the audio component properties.
|
||||
* Allows loading/changing/removing audio files, and adjusting properties like volume, pan, pitch, looping, spatialization, etc.
|
||||
* Displays error messages if loading fails.
|
||||
* This method is called during the ImGui rendering phase.
|
||||
*/
|
||||
void OnImGuiRender() override {
|
||||
if (!m_sound) {
|
||||
ImGui::Text("No audio file loaded");
|
||||
@@ -377,10 +419,81 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the FMOD system to be used by this audio component.
|
||||
* This method allows sharing the FMOD system instance across multiple audio components.
|
||||
* @param system Pointer to the FMOD::System instance.
|
||||
*/
|
||||
void SetSoundSystem(FMOD::System* system) {
|
||||
m_system = system;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the audio component's state to a string.
|
||||
* This includes properties like sound path, volume, pan, pitch, looping, muted, paused, priority, spatialization, and use of velocity.
|
||||
* This method is useful for saving the component's state or debugging.
|
||||
* @return A string representation of the audio component's state.
|
||||
*/
|
||||
std::string Serialize() const override {
|
||||
std::stringstream ss;
|
||||
ss << "AudioComponent:"
|
||||
<< m_soundPath << ":"
|
||||
<< m_volume << ":"
|
||||
<< m_pan << ":"
|
||||
<< m_pitch << ":"
|
||||
<< m_looping << ":"
|
||||
<< m_muted << ":"
|
||||
<< m_paused << ":"
|
||||
<< m_priority << ":"
|
||||
<< m_spatialized << ":"
|
||||
<< m_use_velocity;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize the audio component's state from a string.
|
||||
* This method parses the string and sets the component's properties accordingly.
|
||||
* If the sound path is valid, it attempts to load the audio file.
|
||||
* @param data The string representation of the audio component's state.
|
||||
* @return True if deserialization was successful, otherwise false.
|
||||
*/
|
||||
bool Deserialize(const std::string& data) override {
|
||||
std::stringstream ss(data);
|
||||
std::string type;
|
||||
std::getline(ss, type, ':');
|
||||
if (type != "AudioComponent") return false;
|
||||
|
||||
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, s_volume, ':');
|
||||
std::getline(ss, s_pan, ':');
|
||||
std::getline(ss, s_pitch, ':');
|
||||
std::getline(ss, s_looping, ':');
|
||||
std::getline(ss, s_muted, ':');
|
||||
std::getline(ss, s_paused, ':');
|
||||
std::getline(ss, s_priority, ':');
|
||||
std::getline(ss, s_spatialized, ':');
|
||||
std::getline(ss, s_use_velocity, ':');
|
||||
|
||||
m_volume = std::stof(s_volume);
|
||||
m_pan = std::stof(s_pan);
|
||||
m_pitch = std::stof(s_pitch);
|
||||
m_looping = (s_looping == "1");
|
||||
m_muted = (s_muted == "1");
|
||||
m_paused = (s_paused == "1");
|
||||
m_priority = std::stoi(s_priority);
|
||||
m_spatialized = (s_spatialized == "1");
|
||||
m_use_velocity = (s_use_velocity == "1");
|
||||
|
||||
// Recharger le son si le chemin existe (optional: ou laisser le chargement manuel)
|
||||
if (!m_soundPath.empty() && m_system) {
|
||||
Load(m_soundPath);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
FMOD::System* m_system;
|
||||
FMOD::Sound* m_sound;
|
||||
|
||||
Reference in New Issue
Block a user