Files
khaotic-engine-Reborn/enginecustom/src/inc/system/ecs/components/audio_component.h
CatChow0 5ee88ff932 Minor - Adds basic audio component functionality - V13.2.0
Adds basic audio component and loads default sound for testing

Adds default sounds to the assets folder and includes them in the project.
2025-09-15 23:18:52 +02:00

125 lines
4.0 KiB
C++

// AudioComponent.h
#pragma once
#include "../component.h"
#include <Fmod/core/inc/fmod.hpp>
#include <string>
#include <filesystem>
namespace ecs {
class AudioComponent : public Component {
public:
AudioComponent() : m_system(nullptr), m_sound(nullptr), m_channel(nullptr) {}
~AudioComponent() override {
if (m_sound) m_sound->release();
// Ne pas libérer m_system ici car il peut être partagé
}
void Initialize() override
{
// create FMOD system if not already created
if (!m_system)
{
FMOD_RESULT result = FMOD::System_Create(&m_system);
if (result != FMOD_OK) {
m_lastError = "Échec de la création du système FMOD: " + std::to_string(result);
return;
}
result = m_system->init(512, FMOD_INIT_NORMAL, nullptr);
if (result != FMOD_OK) {
m_lastError = "Échec de l'initialisation du système FMOD: " + std::to_string(result);
m_system->release();
m_system = nullptr;
}
}
}
bool Load(const std::string& path) {
if (!m_system) {
Initialize();
if (!m_system) return false; // L'erreur est déjà définie dans Initialize()
}
m_soundPath = path;
// Vérifier si le fichier existe
if (!std::filesystem::exists(path)) {
m_lastError = "Fichier non trouvé: " + path;
return false;
}
// Libérer le son précédent s'il existe
if (m_sound) {
m_sound->release();
m_sound = nullptr;
}
// Essayer de charger avec le chemin absolu
std::filesystem::path absolutePath = std::filesystem::absolute(path);
FMOD_RESULT result = m_system->createSound(absolutePath.string().c_str(), FMOD_DEFAULT, nullptr, &m_sound);
if (result != FMOD_OK) {
m_lastError = "Échec du chargement du son: " + std::to_string(result) +
" (chemin: " + absolutePath.string() + ")";
return false;
}
return true;
}
void Play() {
if (m_system && m_sound)
m_system->playSound(m_sound, nullptr, false, &m_channel);
}
void Stop() {
if (m_channel)
m_channel->stop();
}
void OnImGuiRender() override {
// Afficher le répertoire de travail actuel
std::string currentDir = std::filesystem::current_path().string();
ImGui::Text("Répertoire actuel: %s", currentDir.c_str());
if (m_sound) {
ImGui::Text("Son chargé: %s", m_soundPath.c_str());
if (ImGui::Button("Jouer")) {
Play();
}
ImGui::SameLine();
if (ImGui::Button("Arrêter")) {
Stop();
}
} else {
ImGui::Text("Aucun son chargé");
// Montrer l'erreur s'il y en a une
if (!m_lastError.empty()) {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Erreur: %s", m_lastError.c_str());
}
// exe path + assets/sounds/default.mp3
std::filesystem::path defaultSoundPath = std::filesystem::current_path() / "assets" / "sounds" / "default.mp3";
std::string defaultSoundPathStr = defaultSoundPath.string();// Input text with default path
char path[260];
strncpy_s(path, m_soundPath.empty() ? defaultSoundPathStr.c_str() : m_soundPath.c_str(), sizeof(path));
path[sizeof(path) - 1] = '\0'; // Assurer la termination nulle
ImGui::InputText("Chemin du fichier", path, sizeof(path));
if (ImGui::Button("Charger le son")) {
if (!Load(path)) {
// L'erreur est déjà définie dans Load()
}
}
}
}
private:
FMOD::System* m_system;
FMOD::Sound* m_sound;
FMOD::Channel* m_channel;
std::string m_soundPath;
std::string m_lastError;
};
}