Add audio, bug fix in map reader
parent
6896cf037d
commit
f8060438da
Binary file not shown.
@ -0,0 +1,76 @@
|
|||||||
|
#include "audio.h"
|
||||||
|
|
||||||
|
Audio::Audio(const std::string &background_path, std::array<std::string, N_SOUNDS> &&sounds_paths)
|
||||||
|
{
|
||||||
|
SfMusicPtr music = std::make_unique<sf::Music>();
|
||||||
|
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<SoundEffect>();
|
||||||
|
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<SoundEffect>();
|
||||||
|
|
||||||
|
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<sf::Music>();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
#ifndef AUDIO_H
|
||||||
|
#define AUDIO_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
|
||||||
|
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<sf::Music>;
|
||||||
|
using SoundEffectPtr = std::unique_ptr<SoundEffect>;
|
||||||
|
|
||||||
|
std::array<SoundEffectPtr, N_SOUNDS> array_sounds;
|
||||||
|
SfMusicPtr background_music;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Audio(const std::string &background_file_name, std::array<std::string, N_SOUNDS> &&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<Audio>;
|
||||||
|
|
||||||
|
#endif // AUDIO_H
|
Loading…
Reference in New Issue