correction sfx

This commit is contained in:
MarcEricMartel 2023-12-14 14:02:29 -05:00
parent 383a19cbd2
commit 1af88ba3e3
3 changed files with 40 additions and 21 deletions

View File

@ -29,8 +29,11 @@ Audio::~Audio() {
if (m_engine) m_engine->drop(); if (m_engine) m_engine->drop();
} }
void Audio::playSound(const char* sound) { void Audio::playSound(const char* name, float volume = 1.) {
m_engine->play2D(sound); 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) { 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) { 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->setVelocity(irrklang::vec3df(vel.x, vel.y, vel.z));
sound->setVolume(volume); sound->setVolume(volume);
sound->setIsPaused(false);
return sound; 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); // 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) { void Audio::ToggleMusicState(GameState state) {
if (m_music_on) { if (m_music_on) {
switch (state) { switch (state) {

View File

@ -19,6 +19,7 @@ private:
float m_mainvolume; float m_mainvolume;
float m_sfxvolume; float m_sfxvolume;
bool m_music_on = true; bool m_music_on = true;
std::vector<irrklang::ISound*> m_sfxes;
public: public:
Audio(); Audio();
@ -35,9 +36,11 @@ public:
//void PlaySong(const char* music); //void PlaySong(const char* music);
void CleanupSFX();
void ToggleMusicState(GameState state); void ToggleMusicState(GameState state);
void playSound(const char* sound); void playSound(const char* sound, float volume);
void SetMusic(bool ison, GameState state); void SetMusic(bool ison, GameState state);
bool GetMusic(); bool GetMusic();

View File

@ -1248,6 +1248,7 @@ void Engine::DisplayGraphicsMenu(float centerX, float centerY) {
void Engine::Render(float elapsedTime) { void Engine::Render(float elapsedTime) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_audio.CleanupSFX();
if (m_gamestate == GameState::SPLASH) { if (m_gamestate == GameState::SPLASH) {
if (m_splashTime > 0.0f) { 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; float bottomThird = centerY + Height() * 0.198f;
if (x > leftButton && x < rightButton && y > bottomFirst && y < topFirst) { 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) { if (m_selectedPlayOptions) {
m_gamestate = GameState::PLAY; m_gamestate = GameState::PLAY;
m_audio.ToggleMusicState(m_gamestate); 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) { 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) { if (m_selectedPlayOptions) {
m_gamestate = GameState::LOBBY; m_gamestate = GameState::LOBBY;
m_settingUsername = true; 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) { 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) { if (m_selectedPlayOptions) {
m_selectedPlayOptions = false; m_selectedPlayOptions = false;
} }
@ -2257,19 +2258,19 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) {
float bottomBack = centerY + Height() * 0.2f; float bottomBack = centerY + Height() * 0.2f;
if (x > leftAudio && x < rightAudio && y > bottomAudio && y < topAudio) { 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 m_selectedOption = 0; // Volume
} }
else if (x > leftGraph && x < rightGraph && y > bottomGraph && y < topGraph) { 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 m_selectedOption = 1; // Graphics
} }
else if (x > leftGameplay && x < rightGameplay && y > bottomGameplay && y < topGameplay) { 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 m_selectedOption = 2; // Gameplay
} }
else if (x > leftBack && x < rightBack && y > bottomBack && y < topBack) { 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; 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; float normalizedPosition = (x - leftBar) / barLength;
if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { 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_selectedOptAudioMainBar = true;
m_mainvolume = normalizedPosition; m_mainvolume = normalizedPosition;
m_audio.SetMainVolume(m_mainvolume); m_audio.SetMainVolume(m_mainvolume);
} }
else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarMusique && y < topBarMusique) { 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_selectedOptAudioMusicBar = true;
m_musicvolume = normalizedPosition; m_musicvolume = normalizedPosition;
m_audio.SetMusicVolume(m_musicvolume); m_audio.SetMusicVolume(m_musicvolume);
} }
else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarEffets && y < topBarEffets) { 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_selectedOptAudioSfxBar = true;
m_sfxvolume = normalizedPosition; m_sfxvolume = normalizedPosition;
m_audio.SetSfxVolume(m_sfxvolume); m_audio.SetSfxVolume(m_sfxvolume);
@ -2325,19 +2326,19 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) {
float bottomChkUHD = centerY; float bottomChkUHD = centerY;
if (m_selectedOption == 1 && leftChk && x < rightChk && y > bottomChkHD && y < topChkHD) { 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; m_resolution = HD;
} }
else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkFHD && y < topChkFHD) { 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; m_resolution = FHD;
} }
else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkQHD && y < topChkQHD) { 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; m_resolution = QHD;
} }
else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkUHD && y < topChkUHD) { 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; m_resolution = UHD;
} }
@ -2350,7 +2351,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) {
// Gameplay // Gameplay
if (m_selectedOption == 2 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { 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_selectedGameplaySensitivityBar = true;
m_sensitivity = normalizedPosition; m_sensitivity = normalizedPosition;
m_player.SetSensitivity(m_sensitivity); 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; float bottomMainMenu = centerY - Height() * 0.4415f;
if (x > leftResume && x < rightResume && y > bottomResume && y < topResume) { 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_gamestate = GameState::PLAY;
m_audio.ToggleMusicState(m_gamestate); m_audio.ToggleMusicState(m_gamestate);
} }
else if (x > leftMainMenu && x < rightMainMenu && y > bottomMainMenu && y < topMainMenu) { 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_gamestate = GameState::MAIN_MENU;
m_selectedPlayOptions = false; m_selectedPlayOptions = false;
m_audio.ToggleMusicState(m_gamestate); m_audio.ToggleMusicState(m_gamestate);