Skip to content

Commit

Permalink
Merge pull request #52 from entire-development/sounds
Browse files Browse the repository at this point in the history
Sounds
  • Loading branch information
d0gied authored Mar 20, 2024
2 parents 9e52522 + d190f97 commit 5f1f2eb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
Binary file added res/sounds/chime.wav
Binary file not shown.
Binary file added res/sounds/walking-sound.wav
Binary file not shown.
3 changes: 3 additions & 0 deletions src/gui_controller/game/states/cell_movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "gui_controller/timed_count.h"
#include "gui_controller/utils.h"
#include "static_data/game_config.h"
#include "sound_manager/sound_manager.h"
#include <cmath>
#include <map>
#include <memory>
Expand Down Expand Up @@ -75,6 +76,7 @@ class CellMovement : public GameState {
d->setNextCell(d->getPrevOnPath().lock());
gm->changeState(GUIGameState::kMoveTransition);
}
sound::playSound("walking-sound", 80);
return;
}

Expand All @@ -87,6 +89,7 @@ class CellMovement : public GameState {
} else if (clicked_left && !neighbours[3].expired()) {
r_selected = 3;
} else if (clicked_enter) {
sound::playSound("chime");
d->setNextCell(d->getNextOnPath().lock());
gm->changeState(GUIGameState::kMoveTransition);
}
Expand Down
31 changes: 31 additions & 0 deletions src/sound_manager/sound_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <SFML/Audio.hpp>
#include <string>

namespace sound {
class SoundManager {
public:
static SoundManager &getInstance() {
static SoundManager instance;
return instance;
}
void playSound(const std::string& sound_name, const float volume) {
sound_buffer = std::make_unique<sf::SoundBuffer>();
sound = std::make_unique<sf::Sound>();
if (sound_buffer->loadFromFile(cfg::SOUNDS_PATH + sound_name + ".wav")) {
sound->setBuffer(*sound_buffer);
sound->setVolume(volume);
sound->play();
}
}
private:
SoundManager() = default;

std::unique_ptr<sf::SoundBuffer> sound_buffer;
std::unique_ptr<sf::Sound> sound;
};

static void playSound(const std::string& sound_name, float volume = 100.f) {
sound::SoundManager::getInstance().playSound(sound_name, volume);
}
}
1 change: 1 addition & 0 deletions src/static_data/game_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const unsigned CELL_SIZE = 20;
const std::string WINDOW_NAME = "*EPIC DUNGEONS*";
const std::string FONTS_PATH = "res/fonts/";
const std::string SPRITES_PATH = "res/sprites/";
const std::string SOUNDS_PATH = "res/sounds/";

const keyboard::Key CONTROLS_MOVE_RIGHT = keyboard::Key::KEY_RIGHT;
const keyboard::Key CONTROLS_MOVE_DOWN = keyboard::Key::KEY_DOWN;
Expand Down

0 comments on commit 5f1f2eb

Please sign in to comment.