mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-18 16:47:31 +01:00
Fix sound not working properly
This commit is contained in:
2
env.cpp
2
env.cpp
@@ -26,6 +26,8 @@ EventHandler eventHandler;
|
||||
std::atomic<bool> soundMuted(false);
|
||||
Mix_Music* backgroundMusic = nullptr;
|
||||
Mix_Music* menuMusic = nullptr;
|
||||
int MUSIC_CHANNEL = 0;
|
||||
int SOUND_CHANNEL = 1;
|
||||
|
||||
|
||||
void resetAll(){
|
||||
|
||||
2
env.h
2
env.h
@@ -42,6 +42,8 @@ extern EventHandler eventHandler;
|
||||
extern std::atomic<bool> soundMuted;
|
||||
extern Mix_Music* backgroundMusic;
|
||||
extern Mix_Music* menuMusic;
|
||||
extern int MUSIC_CHANNEL;
|
||||
extern int SOUND_CHANNEL;
|
||||
|
||||
bool initEnvironment(SDL_Renderer* renderer);
|
||||
std::vector<SDL_Texture*> initTexture(SDL_Renderer* renderer);
|
||||
|
||||
25
main.cpp
25
main.cpp
@@ -187,6 +187,9 @@ bool initSDL() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Mix_AllocateChannels(16);
|
||||
|
||||
// Charger la musique du menu
|
||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||
if (menuMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
@@ -430,14 +433,11 @@ int main(int argc, char* args[]) {
|
||||
//menu.addButton((windowWidth/2) - 100, (windowHeight/2 + 25) + 50, 200, 50, "Multi", 1024);
|
||||
|
||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||
if (menuMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||
if (Mix_PlayMusic(menuMusic, -1) == -1) {
|
||||
std::cerr << "Erreur de lecture de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
if (menuMusic != nullptr) {
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||
if (Mix_PlayMusic(menuMusic, -1) == -1) {
|
||||
std::cerr << "Erreur de lecture de la musique du menu: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
while (running) {
|
||||
@@ -448,11 +448,6 @@ int main(int argc, char* args[]) {
|
||||
menu.draw(renderer);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (Mix_PlayingMusic()) {
|
||||
Mix_HaltMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Check Threads 1" << std::endl;
|
||||
@@ -494,6 +489,8 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
||||
|
||||
game_running = true;
|
||||
|
||||
Mix_HaltMusic();
|
||||
|
||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||
if (backgroundMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||
@@ -589,6 +586,8 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
||||
// }
|
||||
game_running = true;
|
||||
|
||||
Mix_HaltMusic();
|
||||
|
||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||
if (backgroundMusic == nullptr) {
|
||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||
|
||||
10
shark.cpp
10
shark.cpp
@@ -24,7 +24,7 @@ Shark::Shark(const int x, const int y, const float vx, const float vy, const int
|
||||
|
||||
approachingSound = Mix_LoadWAV("../sounds/Shark-approching.wav");
|
||||
if (approachingSound == nullptr) {
|
||||
std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl;
|
||||
std::cerr << "Erreur de chargement du son d'approche du requin: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
|
||||
lastSoundTime = std::chrono::steady_clock::now();
|
||||
@@ -74,7 +74,11 @@ void Shark::cycle() {
|
||||
if (isInView(player)) {
|
||||
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
|
||||
checkCollision(player);
|
||||
Mix_PlayChannel(-1, approachingSound, 0);
|
||||
Mix_PlayChannel(SOUND_CHANNEL, approachingSound, 0);
|
||||
}
|
||||
if (!isInView(player)) {
|
||||
// Cut the approach sound
|
||||
Mix_HaltChannel(SOUND_CHANNEL);
|
||||
}
|
||||
}
|
||||
if (neighboring_player > 0) {
|
||||
@@ -100,7 +104,7 @@ void Shark::cycle() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSoundTime).count() > 15) {
|
||||
if (sharkSound != nullptr) {
|
||||
Mix_PlayChannel(-1, sharkSound, 0);
|
||||
Mix_PlayChannel(SOUND_CHANNEL, sharkSound, 0);
|
||||
}
|
||||
lastSoundTime = now;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user