From 767ef4417539f581950b80aae3928597d6050fdd Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Sun, 9 Nov 2025 00:56:26 -0500 Subject: [PATCH] add sounds --- CMakeLists.txt | 2 +- src/Game.cpp | 29 +++++++++++++++++++++++++++++ src/ResourceManager.cpp | 8 ++++++++ src/ResourceManager.h | 9 +++++++-- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f61f194..a36d56a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,4 +22,4 @@ FetchContent_MakeAvailable(SFML json) add_executable(main src/main.cpp src/Entity.cpp src/Game.cpp src/MazeMap.cpp src/ResourceManager.cpp) target_compile_features(main PRIVATE cxx_std_17) -target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json) +target_link_libraries(main PRIVATE SFML::Graphics SFML::Audio nlohmann_json::nlohmann_json) diff --git a/src/Game.cpp b/src/Game.cpp index 0bcd737..2adb7b0 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "Game.h" #include "MazeMap.h" @@ -19,6 +20,31 @@ void Game::run() { // Pellet data: 0=none, 1=pellet, 2=power resources.loadMap("pelletMap", "assets/game/pellets.txt"); + resources.loadSound("credit", "assets/sounds/credit.wav"); + resources.loadSound("death_0", "assets/sounds/death_0.wav"); + resources.loadSound("death_1", "assets/sounds/death_1.wav"); + resources.loadSound("eat_dot_0", "assets/sounds/eat_dot_0.wav"); + resources.loadSound("eat_dot_1", "assets/sounds/eat_dot_1.wav"); + resources.loadSound("eat_fruit", "assets/sounds/eat_fruit.wav"); + resources.loadSound("eat_ghost", "assets/sounds/eat_ghost.wav"); + resources.loadSound("extend", "assets/sounds/extend.wav"); + resources.loadSound("eyes", "assets/sounds/eyes.wav"); + resources.loadSound("eyes_firstloop", "assets/sounds/eyes_firstloop.wav"); + resources.loadSound("fright", "assets/sounds/fright.wav"); + resources.loadSound("fright_firstloop", "assets/sounds/fright_firstloop.wav"); + resources.loadSound("intermission.wav", "assets/sounds/intermission.wav"); + resources.loadSound("siren0", "assets/sounds/siren0.wav"); + resources.loadSound("siren0_firstloop", "assets/sounds/siren0_firstloop.wav"); + resources.loadSound("siren1", "assets/sounds/siren1.wav"); + resources.loadSound("siren1_firstloop", "assets/sounds/siren1_firstloop.wav"); + resources.loadSound("siren2", "assets/sounds/siren2.wav"); + resources.loadSound("siren2_firstloop", "assets/sounds/siren2_firstloop.wav"); + resources.loadSound("siren3", "assets/sounds/siren3.wav"); + resources.loadSound("siren3_firstloop", "assets/sounds/siren3_firstloop.wav"); + resources.loadSound("siren4", "assets/sounds/siren4.wav"); + resources.loadSound("siren4_firstloop", "assets/sounds/siren4_firstloop.wav"); + resources.loadSound("start", "assets/sounds/start.wav"); + // Texture layout: [Pellet Maze Image] + gap (16px) + [Base Maze Image] // Original: 224px (28*8) + 4px gap at 8px tiles // Scaled 4x: 896px (28*32) + 16px gap at 32px tiles @@ -71,6 +97,9 @@ void Game::run() { auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName); window.setFramerateLimit(maxFPS); + sf::Sound sound(*resources.getSound("start")); + sound.play(); + while (window.isOpen()) { while (const std::optional event = window.pollEvent()) diff --git a/src/ResourceManager.cpp b/src/ResourceManager.cpp index 3c9d0ca..fe0df51 100644 --- a/src/ResourceManager.cpp +++ b/src/ResourceManager.cpp @@ -1,5 +1,6 @@ #include "ResourceManager.h" #include "MazeMap.h" +#include #include #include #include @@ -44,6 +45,13 @@ bool ResourceManager::loadMap(const std::string& mapName, const std::filesystem: return !mapData.empty(); }; +bool ResourceManager::loadSound(const std::string& soundName, const std::filesystem::path& soundPath) { + sounds[soundName] = sf::SoundBuffer(soundPath); + + return true; +}; + + void ResourceManager::resizeTexture(const std::string& textureName, unsigned int newTextureTileSize) { auto it = textures.find(textureName); if (it == textures.end()) return; diff --git a/src/ResourceManager.h b/src/ResourceManager.h index 360f8c5..d1d1b91 100644 --- a/src/ResourceManager.h +++ b/src/ResourceManager.h @@ -1,6 +1,7 @@ #ifndef RESOURCEMANAGER_H #define RESOURCEMANAGER_H +#include #include #include #include @@ -22,7 +23,7 @@ public: bool loadMap(const std::string& mapName, const std::filesystem::path& mapPath); - //bool loadSound(const std::filesystem::path& soundPath); + bool loadSound(const std::string& soundName, const std::filesystem::path& soundPath); sf::Texture& getTexture(const std::string& textureName) { return *textures[textureName]; @@ -38,6 +39,10 @@ public: return pelletMap; } + sf::SoundBuffer* getSound(const std::string& soundName) { + return &sounds[soundName]; + }; + //load gameplay info private: @@ -49,7 +54,7 @@ private: std::map fonts; //sounds - //std::map sounds; + std::map sounds; //map std::vector mazeMap;