diff --git a/SQCSim2021/audio.cpp b/SQCSim2021/audio.cpp index 69537a5..97bed1c 100644 --- a/SQCSim2021/audio.cpp +++ b/SQCSim2021/audio.cpp @@ -29,8 +29,11 @@ Audio::~Audio() { if (m_engine) m_engine->drop(); } -void Audio::playSound(const char* sound) { - m_engine->play2D(sound); +void Audio::playSound(const char* name, float volume = 1.) { + irrklang::ISound* sfx = m_engine->play2D(name, false, true); + sfx->setVolume(volume); + sfx->setIsPaused(false); + m_sfxes.push_back(sfx); } void Audio::Update3DAudio(Vector3f pos, Vector3f dir, Vector3f vel) { @@ -40,9 +43,10 @@ void Audio::Update3DAudio(Vector3f pos, Vector3f dir, Vector3f vel) { } irrklang::ISound* Audio::Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, bool is_looped = false, float volume = 1) { - sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), is_looped, false, true, is_looped? irrklang::ESM_STREAMING: irrklang::ESM_NO_STREAMING, true); + sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), is_looped, true, true, is_looped? irrklang::ESM_STREAMING: irrklang::ESM_NO_STREAMING, true); sound->setVelocity(irrklang::vec3df(vel.x, vel.y, vel.z)); sound->setVolume(volume); + sound->setIsPaused(false); return sound; } @@ -56,6 +60,17 @@ void Audio::Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& v // m_music = m_engine->play2D(music, false, false, false, irrklang::ESM_STREAMING); //} +void Audio::CleanupSFX() { + while (!m_sfxes.empty()) { + irrklang::ISound* sfx = m_sfxes.back(); + if (sfx->isFinished()) { + sfx->drop(); // drop() fait deja la job du delete sfx. + } + else break; + m_sfxes.pop_back(); + } +} + void Audio::ToggleMusicState(GameState state) { if (m_music_on) { switch (state) { diff --git a/SQCSim2021/audio.h b/SQCSim2021/audio.h index c0497eb..008cd62 100644 --- a/SQCSim2021/audio.h +++ b/SQCSim2021/audio.h @@ -19,6 +19,7 @@ private: float m_mainvolume; float m_sfxvolume; bool m_music_on = true; + std::vector m_sfxes; public: Audio(); @@ -35,9 +36,11 @@ public: //void PlaySong(const char* music); + void CleanupSFX(); + void ToggleMusicState(GameState state); - void playSound(const char* sound); + void playSound(const char* sound, float volume); void SetMusic(bool ison, GameState state); bool GetMusic(); diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 4a68b8a..b87cff3 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -1248,6 +1248,7 @@ void Engine::DisplayGraphicsMenu(float centerX, float centerY) { void Engine::Render(float elapsedTime) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + m_audio.CleanupSFX(); if (m_gamestate == GameState::SPLASH) { if (m_splashTime > 0.0f) { @@ -2206,7 +2207,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomThird = centerY + Height() * 0.198f; if (x > leftButton && x < rightButton && y > bottomFirst && y < topFirst) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { m_gamestate = GameState::PLAY; m_audio.ToggleMusicState(m_gamestate); @@ -2216,7 +2217,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { } } else if (x > leftButton && x < rightButton && y > bottomSecond && y < topSecond) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { m_gamestate = GameState::LOBBY; m_settingUsername = true; @@ -2226,7 +2227,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { } } else if (x > leftButton && x < rightButton && y > bottomThird && y < topThird) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { m_selectedPlayOptions = false; } @@ -2257,19 +2258,19 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomBack = centerY + Height() * 0.2f; if (x > leftAudio && x < rightAudio && y > bottomAudio && y < topAudio) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 0; // Volume } else if (x > leftGraph && x < rightGraph && y > bottomGraph && y < topGraph) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 1; // Graphics } else if (x > leftGameplay && x < rightGameplay && y > bottomGameplay && y < topGameplay) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 2; // Gameplay } else if (x > leftBack && x < rightBack && y > bottomBack && y < topBack) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::MAIN_MENU; } @@ -2290,19 +2291,19 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float normalizedPosition = (x - leftBar) / barLength; if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOptAudioMainBar = true; m_mainvolume = normalizedPosition; m_audio.SetMainVolume(m_mainvolume); } else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarMusique && y < topBarMusique) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOptAudioMusicBar = true; m_musicvolume = normalizedPosition; m_audio.SetMusicVolume(m_musicvolume); } else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarEffets && y < topBarEffets) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOptAudioSfxBar = true; m_sfxvolume = normalizedPosition; m_audio.SetSfxVolume(m_sfxvolume); @@ -2325,19 +2326,19 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomChkUHD = centerY; if (m_selectedOption == 1 && leftChk && x < rightChk && y > bottomChkHD && y < topChkHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_resolution = HD; } else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkFHD && y < topChkFHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_resolution = FHD; } else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkQHD && y < topChkQHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_resolution = QHD; } else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkUHD && y < topChkUHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_resolution = UHD; } @@ -2350,7 +2351,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { // Gameplay if (m_selectedOption == 2 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedGameplaySensitivityBar = true; m_sensitivity = normalizedPosition; m_player.SetSensitivity(m_sensitivity); @@ -2368,12 +2369,12 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomMainMenu = centerY - Height() * 0.4415f; if (x > leftResume && x < rightResume && y > bottomResume && y < topResume) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::PLAY; m_audio.ToggleMusicState(m_gamestate); } else if (x > leftMainMenu && x < rightMainMenu && y > bottomMainMenu && y < topMainMenu) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::MAIN_MENU; m_selectedPlayOptions = false; m_audio.ToggleMusicState(m_gamestate);