diff --git a/CMakeLists.txt b/CMakeLists.txt index d9d41d9..cb1d94c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp ) # Lier SDL2 et SDL2_image -target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_ttf -lSDL2_net) +target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_ttf -lSDL2_net -lSDL2_mixer) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") diff --git a/main.cpp b/main.cpp index 2c9011e..8d3f6a3 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -150,6 +151,10 @@ bool initSDL() { std::cerr << "SDLNet could not initialize! SDLNet_Error: " << SDLNet_GetError() << std::endl; return false; } + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { + std::cerr << "SDL_mixer could not initialize! SDL_mixer Error: " << Mix_GetError() << std::endl; + return false; + } window = SDL_CreateWindow("BloubBloub les poissons", SDL_WINDOWPOS_CENTERED, @@ -765,12 +770,6 @@ void cleanup() { std::cerr << "Exception caught for DestroyWindow: " << e.what() << std::endl; } - try { - SDL_Quit(); - } catch (const std::exception& e) { - std::cerr << "Exception caught for SDL_Quit: " << e.what() << std::endl; - } - try { IMG_Quit(); } catch (const std::exception& e) { @@ -782,5 +781,17 @@ void cleanup() { } catch (const std::exception& e) { std::cerr << "Exception caught for SDLNet_Quit: " << e.what() << std::endl; } + + try { + Mix_CloseAudio(); + } catch (const std::exception& e) { + std::cerr << "Exception caught for Mix_CloseAudio: " << e.what() << std::endl; + } + + try { + SDL_Quit(); + } catch (const std::exception& e) { + std::cerr << "Exception caught for SDL_Quit: " << e.what() << std::endl; + } } diff --git a/shark.cpp b/shark.cpp index 769d90d..c5635cf 100644 --- a/shark.cpp +++ b/shark.cpp @@ -17,6 +17,12 @@ Shark::Shark(const int x, const int y, const float vx, const float vy, const int SDL_FreeSurface(sharkSurface); } + sharkSound = Mix_LoadWAV("../sounds/shark.wav"); + if (sharkSound == nullptr) { + std::cerr << "Erreur de chargement du son du requin: " << Mix_GetError() << std::endl; + } + + lastSoundTime = std::chrono::steady_clock::now(); } void Shark::draw(SDL_Renderer *renderer) { @@ -79,7 +85,27 @@ void Shark::cycle() { x += vx; y += vy; + auto now = std::chrono::steady_clock::now(); + if (std::chrono::duration_cast(now - lastSoundTime).count() > 15) { + if (sharkSound != nullptr) { + Mix_PlayChannel(-1, sharkSound, 0); + } + lastSoundTime = now; + } + if (isPlayingOnline && isHost) { sendSharkPosition(client, id, x, y); } +} + +Shark::~Shark() { + if (texture) { + SDL_DestroyTexture(texture); + texture = nullptr; + } + + if (sharkSound) { + Mix_FreeChunk(sharkSound); + sharkSound = nullptr; + } } \ No newline at end of file diff --git a/shark.h b/shark.h index 7f37afc..48604cc 100644 --- a/shark.h +++ b/shark.h @@ -8,6 +8,8 @@ #include #include "player.h" #include +#include +#include class Shark { private: @@ -27,10 +29,13 @@ private: int width, height; std::vector &players_list; + Mix_Chunk* sharkSound; + std::chrono::steady_clock::time_point lastSoundTime; + public: Shark(int x, int y, float vx, float vy, int id, int width, int height, SDL_Renderer *renderer,std::vector &players_list); - ~Shark() = default; + ~Shark(); float getX() const { return x; }; float getY() const { return y; }; diff --git a/sounds/shark.wav b/sounds/shark.wav new file mode 100644 index 0000000..b45f207 Binary files /dev/null and b/sounds/shark.wav differ