Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sounds #52

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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