Merge pull request #33 from BreizhHardware/new_menu

New menu
This commit is contained in:
Félix MARQUET
2024-12-16 15:03:38 +01:00
committed by GitHub
8 changed files with 103 additions and 1 deletions

View File

@@ -23,6 +23,11 @@ std::atomic<bool> isPlayingOnline(false);
std::atomic<bool> messageThreadRunning(false);
std::atomic<bool> isHost(false);
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(){

6
env.h
View File

@@ -5,6 +5,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <iostream>
#include <vector>
#include <dirent.h>
@@ -38,6 +39,11 @@ extern std::vector<Player> players;
extern std::vector<Player> players_server;
extern std::atomic<bool> isHost;
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);

View File

@@ -187,6 +187,15 @@ 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;
return false;
}
window = SDL_CreateWindow("BloubBloub les poissons",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
@@ -423,6 +432,14 @@ 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) {
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) {
handleQuit();
@@ -472,6 +489,20 @@ 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;
return false;
}
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
return false;
}
Mix_PlayMusic(backgroundMusic, -1);
std::vector<Kelp> kelps;
std::vector<Rock> rocks;
std::vector<Coral> corals;
@@ -554,6 +585,21 @@ int pas_la_fontion_main_enfin_ce_nest_pas_la_fontion_principale_du_programme_mai
// return -1;
// }
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;
return false;
}
Mix_VolumeMusic(MIX_MAX_VOLUME / 4);
if (Mix_PlayMusic(backgroundMusic, -1) == -1) {
std::cerr << "Erreur de lecture de la musique: " << Mix_GetError() << std::endl;
return false;
}
Mix_PlayMusic(backgroundMusic, -1);
std::vector<Kelp> kelps;
std::vector<Rock> rocks;
std::vector<Coral> corals;
@@ -741,6 +787,17 @@ void handleQuit() {
}
game_running = false;
}
if (keystate[SDL_SCANCODE_M]) {
soundMuted = !soundMuted;
if (soundMuted) {
Mix_Volume(-1, 0);
Mix_VolumeMusic(0);
} else {
Mix_Volume(-1, MIX_MAX_VOLUME);
Mix_VolumeMusic(MIX_MAX_VOLUME);
}
}
}
void renderScene(std::vector<Player>& players, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks, const std::vector<Coral>& corals,Shark& shark) {
@@ -845,6 +902,24 @@ void cleanup() {
std::cerr << "Exception caught for DestroyWindow: " << e.what() << std::endl;
}
try {
if (backgroundMusic != nullptr) {
Mix_FreeMusic(backgroundMusic);
backgroundMusic = nullptr;
}
} catch (const std::exception& e) {
std::cerr << "Exception caught for FreeMusic: " << e.what() << std::endl;
}
try {
if (menuMusic != nullptr) {
Mix_FreeMusic(menuMusic);
menuMusic = nullptr;
}
} catch (const std::exception& e) {
std::cerr << "Exception caught for FreeMusic (menuMusic): " << e.what() << std::endl;
}
try {
IMG_Quit();
} catch (const std::exception& e) {

View File

@@ -22,6 +22,11 @@ Shark::Shark(const int x, const int y, const float vx, const float vy, const int
std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl;
}
approachingSound = Mix_LoadWAV("../sounds/Shark-approching.wav");
if (approachingSound == nullptr) {
std::cerr << "Erreur de chargement du son d'approche du requin: " << Mix_GetError() << std::endl;
}
lastSoundTime = std::chrono::steady_clock::now();
}
@@ -69,6 +74,11 @@ void Shark::cycle() {
if (isInView(player)) {
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
checkCollision(player);
Mix_PlayChannel(SOUND_CHANNEL, approachingSound, 0);
}
if (!isInView(player)) {
// Cut the approach sound
Mix_HaltChannel(SOUND_CHANNEL);
}
}
if (neighboring_player > 0) {
@@ -94,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;
}
@@ -117,6 +127,11 @@ Shark::~Shark() {
Mix_FreeChunk(sharkSound);
sharkSound = nullptr;
}
if (approachingSound) {
Mix_FreeChunk(approachingSound);
approachingSound = nullptr;
}
}
void Shark::updatePosition(int newX, int newY) {

View File

@@ -31,6 +31,7 @@ private:
std::vector<Player> &players_list;
Mix_Chunk* sharkSound;
Mix_Chunk* approachingSound;
std::chrono::steady_clock::time_point lastSoundTime;
std::chrono::steady_clock::time_point lastSendTime;

BIN
sounds/Menu.wav Normal file

Binary file not shown.

BIN
sounds/Playing.wav Normal file

Binary file not shown.

BIN
sounds/Shark-approching.wav Normal file

Binary file not shown.