mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-03-18 21:50:32 +01:00
feat: integrate MusicManager for improved audio handling and remove legacy music code
This commit is contained in:
@@ -41,6 +41,7 @@ add_executable(bloubloulespoissons main.cpp
|
|||||||
Utility/utility.h
|
Utility/utility.h
|
||||||
Utility/close.cpp
|
Utility/close.cpp
|
||||||
Utility/close.h
|
Utility/close.h
|
||||||
|
Utility/music.h
|
||||||
Game/LaunchGameSolo.cpp
|
Game/LaunchGameSolo.cpp
|
||||||
Game/LaunchGameSolo.h
|
Game/LaunchGameSolo.h
|
||||||
Game/LaunchGameMulti.cpp
|
Game/LaunchGameMulti.cpp
|
||||||
|
|||||||
@@ -15,15 +15,15 @@ Shark::Shark(const int x, const int y, const float vx, const float vy, const int
|
|||||||
SDL_FreeSurface(sharkSurface);
|
SDL_FreeSurface(sharkSurface);
|
||||||
}
|
}
|
||||||
|
|
||||||
sharkSound = Mix_LoadWAV("../sounds/shark.wav");
|
// sharkSound = Mix_LoadWAV("../sounds/shark.wav");
|
||||||
if (sharkSound == nullptr) {
|
// if (sharkSound == nullptr) {
|
||||||
std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl;
|
// std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
approachingSound = Mix_LoadWAV("../sounds/Shark-approching.wav");
|
// approachingSound = Mix_LoadWAV("../sounds/Shark-approching.wav");
|
||||||
if (approachingSound == nullptr) {
|
// if (approachingSound == nullptr) {
|
||||||
std::cerr << "Erreur de chargement du son d'approche 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();
|
lastSoundTime = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
@@ -72,11 +72,15 @@ void Shark::cycle() {
|
|||||||
if (isInView(player)) {
|
if (isInView(player)) {
|
||||||
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
|
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
|
||||||
checkCollision(player);
|
checkCollision(player);
|
||||||
Mix_PlayChannel(SOUND_CHANNEL, approachingSound, 0);
|
//Mix_PlayChannel(SOUND_CHANNEL, approachingSound, 0);
|
||||||
|
musicManager.playMusic("Shark-approaching");
|
||||||
|
musicManager.pauseMusic("Playing");
|
||||||
}
|
}
|
||||||
if (!isInView(player)) {
|
if (!isInView(player)) {
|
||||||
// Cut the approach sound
|
// Cut the approach sound
|
||||||
Mix_HaltChannel(SOUND_CHANNEL);
|
musicManager.stopMusic("Shark-approaching");
|
||||||
|
musicManager.resumeMusic("Playing");
|
||||||
|
// Mix_HaltChannel(SOUND_CHANNEL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (neighboring_player > 0) {
|
if (neighboring_player > 0) {
|
||||||
@@ -101,9 +105,10 @@ void Shark::cycle() {
|
|||||||
|
|
||||||
auto now = std::chrono::steady_clock::now();
|
auto now = std::chrono::steady_clock::now();
|
||||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSoundTime).count() > 15) {
|
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSoundTime).count() > 15) {
|
||||||
if (sharkSound != nullptr) {
|
// if (sharkSound != nullptr) {
|
||||||
Mix_PlayChannel(SOUND_CHANNEL, sharkSound, 0);
|
// Mix_PlayChannel(SOUND_CHANNEL, sharkSound, 0);
|
||||||
}
|
// }
|
||||||
|
musicManager.playMusic("Shark");
|
||||||
lastSoundTime = now;
|
lastSoundTime = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,15 +126,15 @@ Shark::~Shark() {
|
|||||||
texture = nullptr;
|
texture = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sharkSound) {
|
// if (sharkSound) {
|
||||||
Mix_FreeChunk(sharkSound);
|
// Mix_FreeChunk(sharkSound);
|
||||||
sharkSound = nullptr;
|
// sharkSound = nullptr;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (approachingSound) {
|
// if (approachingSound) {
|
||||||
Mix_FreeChunk(approachingSound);
|
// Mix_FreeChunk(approachingSound);
|
||||||
approachingSound = nullptr;
|
// approachingSound = nullptr;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shark::updatePosition(int newX, int newY) {
|
void Shark::updatePosition(int newX, int newY) {
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ private:
|
|||||||
int width, height;
|
int width, height;
|
||||||
std::vector<Player> &players_list;
|
std::vector<Player> &players_list;
|
||||||
|
|
||||||
Mix_Chunk* sharkSound;
|
// Mix_Chunk* sharkSound;
|
||||||
Mix_Chunk* approachingSound;
|
// Mix_Chunk* approachingSound;
|
||||||
std::chrono::steady_clock::time_point lastSoundTime;
|
std::chrono::steady_clock::time_point lastSoundTime;
|
||||||
std::chrono::steady_clock::time_point lastSendTime;
|
std::chrono::steady_clock::time_point lastSendTime;
|
||||||
|
|
||||||
|
|||||||
@@ -11,19 +11,21 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
|||||||
// }
|
// }
|
||||||
game_running = true;
|
game_running = true;
|
||||||
|
|
||||||
Mix_HaltMusic();
|
musicManager.playMusic("Playing");
|
||||||
|
|
||||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
// Mix_HaltMusic();
|
||||||
if (backgroundMusic == nullptr) {
|
|
||||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
// backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||||
return false;
|
// if (backgroundMusic == nullptr) {
|
||||||
}
|
// std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
// return false;
|
||||||
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
// }
|
||||||
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
// Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||||
return false;
|
// if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
||||||
}
|
// std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
||||||
Mix_PlayMusic(backgroundMusic, -1);
|
// return false;
|
||||||
|
// }
|
||||||
|
// Mix_PlayMusic(backgroundMusic, -1);
|
||||||
|
|
||||||
std::vector<Kelp> kelps;
|
std::vector<Kelp> kelps;
|
||||||
std::vector<Rock> rocks;
|
std::vector<Rock> rocks;
|
||||||
|
|||||||
@@ -12,19 +12,22 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
|
|||||||
|
|
||||||
game_running = true;
|
game_running = true;
|
||||||
|
|
||||||
Mix_HaltMusic();
|
|
||||||
|
musicManager.playMusic("Playing");
|
||||||
|
|
||||||
backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
// Mix_HaltMusic();
|
||||||
if (backgroundMusic == nullptr) {
|
|
||||||
std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
// backgroundMusic = Mix_LoadMUS("../sounds/Playing.wav");
|
||||||
return false;
|
// if (backgroundMusic == nullptr) {
|
||||||
}
|
// std::cerr << "Erreur de chargement de la musique: " << Mix_GetError() << std::endl;
|
||||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
// return false;
|
||||||
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
// }
|
||||||
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
// Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||||
return false;
|
// if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
|
||||||
}
|
// std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
|
||||||
Mix_PlayMusic(backgroundMusic, -1);
|
// return false;
|
||||||
|
// }
|
||||||
|
// Mix_PlayMusic(backgroundMusic, -1);
|
||||||
|
|
||||||
std::vector<Kelp> kelps;
|
std::vector<Kelp> kelps;
|
||||||
std::vector<Rock> rocks;
|
std::vector<Rock> rocks;
|
||||||
|
|||||||
@@ -78,6 +78,13 @@ void drawRoundedRect(SDL_Renderer* renderer, SDL_Rect rect, int radius, SDL_Colo
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string Menu::getCurrentPageName() {
|
||||||
|
if (currentPage == -1) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return pages[currentPage].title;
|
||||||
|
}
|
||||||
|
|
||||||
void Menu::draw(SDL_Renderer* renderer) {
|
void Menu::draw(SDL_Renderer* renderer) {
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|||||||
@@ -85,6 +85,8 @@ class Menu {
|
|||||||
|
|
||||||
void changePage(std::string title);
|
void changePage(std::string title);
|
||||||
|
|
||||||
|
std::string getCurrentPageName();
|
||||||
|
|
||||||
void addButton(std::string page, int x, int y, int w, int h, std::string text, int size, std::function<void()> callback, bool isTextInput = false);
|
void addButton(std::string page, int x, int y, int w, int h, std::string text, int size, std::function<void()> callback, bool isTextInput = false);
|
||||||
|
|
||||||
void addText(std::string page, int x, int y, int w, int h, std::string text, int size);
|
void addText(std::string page, int x, int y, int w, int h, std::string text, int size);
|
||||||
|
|||||||
@@ -103,23 +103,23 @@ void cleanup() {
|
|||||||
std::cerr << "Exception caught for DestroyWindow: " << e.what() << std::endl;
|
std::cerr << "Exception caught for DestroyWindow: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// try {
|
||||||
if (backgroundMusic != nullptr) {
|
// if (backgroundMusic != nullptr) {
|
||||||
Mix_FreeMusic(backgroundMusic);
|
// Mix_FreeMusic(backgroundMusic);
|
||||||
backgroundMusic = nullptr;
|
// backgroundMusic = nullptr;
|
||||||
}
|
// }
|
||||||
} catch (const std::exception& e) {
|
// } catch (const std::exception& e) {
|
||||||
std::cerr << "Exception caught for FreeMusic: " << e.what() << std::endl;
|
// std::cerr << "Exception caught for FreeMusic: " << e.what() << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
try {
|
// try {
|
||||||
if (menuMusic != nullptr) {
|
// if (menuMusic != nullptr) {
|
||||||
Mix_FreeMusic(menuMusic);
|
// Mix_FreeMusic(menuMusic);
|
||||||
menuMusic = nullptr;
|
// menuMusic = nullptr;
|
||||||
}
|
// }
|
||||||
} catch (const std::exception& e) {
|
// } catch (const std::exception& e) {
|
||||||
std::cerr << "Exception caught for FreeMusic (menuMusic): " << e.what() << std::endl;
|
// std::cerr << "Exception caught for FreeMusic (menuMusic): " << e.what() << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
IMG_Quit();
|
IMG_Quit();
|
||||||
|
|||||||
@@ -24,13 +24,10 @@ std::atomic<bool> messageThreadRunning(false);
|
|||||||
std::atomic<bool> isHost(false);
|
std::atomic<bool> isHost(false);
|
||||||
EventHandler eventHandler;
|
EventHandler eventHandler;
|
||||||
std::atomic<bool> soundMuted(false);
|
std::atomic<bool> soundMuted(false);
|
||||||
Mix_Music* backgroundMusic = nullptr;
|
|
||||||
Mix_Music* menuMusic = nullptr;
|
|
||||||
int MUSIC_CHANNEL = 0;
|
|
||||||
int SOUND_CHANNEL = 1;
|
|
||||||
SDL_Texture* playerTexture = nullptr;
|
SDL_Texture* playerTexture = nullptr;
|
||||||
SDL_Texture* fishTextures[100];
|
SDL_Texture* fishTextures[100];
|
||||||
std::vector<Fish> school;
|
std::vector<Fish> school;
|
||||||
|
MusicManager musicManager;
|
||||||
std::atomic<bool> displayFPSFlag(true);
|
std::atomic<bool> displayFPSFlag(true);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "music.h"
|
||||||
#include "event.h"
|
#include "event.h"
|
||||||
|
|
||||||
class Player;
|
class Player;
|
||||||
@@ -42,13 +43,10 @@ extern std::vector<Player> players_server;
|
|||||||
extern std::atomic<bool> isHost;
|
extern std::atomic<bool> isHost;
|
||||||
extern EventHandler eventHandler;
|
extern EventHandler eventHandler;
|
||||||
extern std::atomic<bool> soundMuted;
|
extern std::atomic<bool> soundMuted;
|
||||||
extern Mix_Music* backgroundMusic;
|
|
||||||
extern Mix_Music* menuMusic;
|
|
||||||
extern int MUSIC_CHANNEL;
|
|
||||||
extern int SOUND_CHANNEL;
|
|
||||||
extern SDL_Texture* fishTextures[100];
|
extern SDL_Texture* fishTextures[100];
|
||||||
extern SDL_Texture* playerTexture;
|
extern SDL_Texture* playerTexture;
|
||||||
extern std::vector<Fish> school;
|
extern std::vector<Fish> school;
|
||||||
|
extern MusicManager musicManager;
|
||||||
extern std::atomic<bool> displayFPSFlag;
|
extern std::atomic<bool> displayFPSFlag;
|
||||||
|
|
||||||
bool initEnvironment(SDL_Renderer* renderer);
|
bool initEnvironment(SDL_Renderer* renderer);
|
||||||
|
|||||||
@@ -92,11 +92,11 @@ bool initSDL() {
|
|||||||
Mix_AllocateChannels(16);
|
Mix_AllocateChannels(16);
|
||||||
|
|
||||||
// Charger la musique du menu
|
// Charger la musique du menu
|
||||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
// menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||||
if (menuMusic == nullptr) {
|
// if (menuMusic == nullptr) {
|
||||||
std::cerr << "Erreur de chargement de la musique du menu: " << Mix_GetError() << std::endl;
|
// std::cerr << "Erreur de chargement de la musique du menu: " << Mix_GetError() << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
window = SDL_CreateWindow("BloubBloub les poissons",
|
window = SDL_CreateWindow("BloubBloub les poissons",
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
|||||||
27
main.cpp
27
main.cpp
@@ -58,12 +58,11 @@ void onPlayerLost(Menu &menu){
|
|||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
menu.drawLost(renderer);
|
menu.drawLost(renderer);
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
Mix_Chunk* deathSound = Mix_LoadWAV("../sounds/death.wav");
|
musicManager.playMusic("death");
|
||||||
Mix_PlayChannel(-1, deathSound, 0);
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(8));
|
std::this_thread::sleep_for(std::chrono::seconds(8));
|
||||||
Mix_FreeChunk(deathSound);
|
|
||||||
menuRunning = true;
|
menuRunning = true;
|
||||||
menu.changePage("Main");
|
menu.changePage("Main");
|
||||||
|
musicManager.playMusic("Menu");
|
||||||
menu.show();
|
menu.show();
|
||||||
resetAll();
|
resetAll();
|
||||||
}
|
}
|
||||||
@@ -167,13 +166,21 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
//menu.addButton((windowWidth/2) - 100, (windowHeight/2 + 25) + 50, 200, 50, "Multi", 1024);
|
//menu.addButton((windowWidth/2) - 100, (windowHeight/2 + 25) + 50, 200, 50, "Multi", 1024);
|
||||||
|
|
||||||
menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
// menuMusic = Mix_LoadMUS("../sounds/Menu.wav");
|
||||||
if (menuMusic != nullptr) {
|
// if (menuMusic != nullptr) {
|
||||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
// Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
|
||||||
if (Mix_PlayMusic(menuMusic, -1) == -1) {
|
// if (Mix_PlayMusic(menuMusic, -1) == -1) {
|
||||||
std::cerr << "Erreur de lecture de la musique du menu: " << Mix_GetError() << std::endl;
|
// std::cerr << "Erreur de lecture de la musique du menu: " << Mix_GetError() << std::endl;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
musicManager.addMusic("../sounds/Menu.wav", AudioType::MUSIC);
|
||||||
|
musicManager.addMusic("../sounds/Playing.wav", AudioType::MUSIC);
|
||||||
|
musicManager.addMusic("../sounds/death.wav", AudioType::SOUND_EFFECT);
|
||||||
|
musicManager.addMusic("../sounds/Shark-approching.wav", AudioType::SOUND_EFFECT);
|
||||||
|
musicManager.addMusic("../sounds/shark.wav", AudioType::SOUND_EFFECT);
|
||||||
|
|
||||||
|
musicManager.playMusic("Menu");
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user