diff --git a/build-debug/sfml-test b/build-debug/sfml-test index b203a06..64af38c 100755 Binary files a/build-debug/sfml-test and b/build-debug/sfml-test differ diff --git a/build-debug/test_map b/build-debug/test_map index c4c0b65..2e2f488 100644 --- a/build-debug/test_map +++ b/build-debug/test_map @@ -2,9 +2,13 @@ size 7 7 map 2 2 2 2 2 2 2 -2 0 0 0 0 0 2 +2 0 0 0 0 6 2 2 0 0 1 0 0 2 2 0 0 1 1 0 2 2 1 1 2 2 2 2 -2 0 0 0 0 0 2 +2 0 0 0 0 5 2 2 2 2 2 2 2 2 +teleport +5 5 1 1 +trigger +1 5 4 5 0 diff --git a/src/audio.cpp b/src/audio.cpp new file mode 100644 index 0000000..50f1db1 --- /dev/null +++ b/src/audio.cpp @@ -0,0 +1,76 @@ +#include "audio.h" + +Audio::Audio(const std::string &background_path, std::array &&sounds_paths) +{ + SfMusicPtr music = std::make_unique(); + if (music->openFromFile(background_path)) + music->setLoop(true); + else + music = nullptr; + + background_music = std::move(music); + + SoundEffectPtr effect; + for (int i = 0; i < N_SOUNDS; ++i) + { + effect = std::make_unique(); + if (effect->buffer.loadFromFile(sounds_paths[i])) + { + effect->sound.setBuffer(effect->buffer); + array_sounds[i] = std::move(effect); + } + else + { + array_sounds[i] = nullptr; + } + } +} + +bool Audio::setSound(const SOUND_TYPE &type, const std::string &sound_file_path) +{ + SoundEffectPtr effect = std::make_unique(); + + if (!effect->buffer.loadFromFile(sound_file_path)) + return false; + + effect->sound.setBuffer(effect->buffer); + + array_sounds[type] = std::move(effect); + return true; +} + +void Audio::playSound(const SOUND_TYPE &type) +{ + array_sounds[type]->sound.play(); +} + +bool Audio::setBackground(const std::string &music_file_path) +{ + SfMusicPtr music = std::make_unique(); + + if (!music->openFromFile(music_file_path)) + return false; + + background_music = std::move(music); + return true; +} + +void Audio::playBackground() +{ + background_music->play(); +} + +void Audio::stopBackground() +{ + background_music->stop(); +} + +void Audio::pauseBackground() +{ + background_music->pause(); +} + +void Audio::setBackgroundVolume(const float &volume) +{ + background_music->setVolume(volume); +} diff --git a/src/audio.h b/src/audio.h new file mode 100644 index 0000000..eee26c2 --- /dev/null +++ b/src/audio.h @@ -0,0 +1,47 @@ +#ifndef AUDIO_H +#define AUDIO_H + +#include +#include +#include + +#include + +enum SOUND_TYPE { + FOOTSTEP_SOUND = 0, + + N_SOUNDS +}; + +class Audio +{ +private: + // Struct for small sounds, like shots, foot steps, etc. + // As we always need to store SoundBuffer in the same scope as Sound, it's better to make struct. + struct SoundEffect { + sf::SoundBuffer buffer; + sf::Sound sound; + }; + + using SfMusicPtr = std::unique_ptr; + using SoundEffectPtr = std::unique_ptr; + + std::array array_sounds; + SfMusicPtr background_music; + +public: + Audio(const std::string &background_file_name, std::array &&sounds_paths); + + bool setSound(const SOUND_TYPE &type, const std::string &sound_file_path); + void playSound(const SOUND_TYPE &type); + + bool setBackground(const std::string &music_file_path); + void playBackground(); + void stopBackground(); + void pauseBackground(); + void setBackgroundVolume(const float &volume); +}; + +using AudioPtr = std::unique_ptr