-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSFMLSoundProvider.cpp
More file actions
111 lines (103 loc) · 2.81 KB
/
SFMLSoundProvider.cpp
File metadata and controls
111 lines (103 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stdafx.h"
#include "SFMLSoundProvider.h"
// for when I decide to implement program options #include "Game.h"
SFMLSoundProvider::SFMLSoundProvider() :
mCurrentSongName("")
{}
void SFMLSoundProvider::PlaySound(std::string filename)
{
//Find first available channel where a sound is not playing
int availChannel = -1;
for (int i = 0; i < MAX_SOUND_CHANNELS; ++i)
{
if (mCurrentSounds[i].GetStatus() != sf::Sound::Playing)
{
availChannel = i;
break;
}
}
//If all sound channels are in use, do nothing for now
if (availChannel != -1)
{
//Load sound from cache and play it
try
{
mCurrentSounds[availChannel] = mSoundFileCache.GetSound(filename);
mCurrentSounds[availChannel].Play();
}
catch(SoundNotFoundExeception&)
{
//ERROR, file wasn't found, should handle error here
//Currently, this will simply mean nothing happens if an error occurs
}
}
}
void SFMLSoundProvider::PlaySong(std::string filename, bool looping)
{
//Load Song from Cache
sf::Music * currentSong;
try
{
currentSong = mSoundFileCache.GetSong(filename);
}
catch (SoundNotFoundExeception&)
{
//This one is dire, means we couldn't find or load the selected song
//So, let's exit
return;
}
//See if prior song is playing still, if so, stop it
if (mCurrentSongName != "")
{
try
{
sf::Music* priorSong = mSoundFileCache.GetSong(mCurrentSongName);
if (priorSong->GetStatus() != sf::Sound::Stopped)
{
priorSong->Stop();
}
}
catch (SoundNotFoundExeception&)
{
//Do nothing, this exception isn't dire. It simply means the previous sound we were
//trying to stop wasn't located
}
}
//Set and Play Song.
mCurrentSongName = filename;
currentSong->SetLoop(looping);
currentSong->Play();
}
void SFMLSoundProvider::StopAllSounds()
{
for (int i = 0; i < MAX_SOUND_CHANNELS; ++i)
{
mCurrentSounds[i].Stop();
}
if (mCurrentSongName != "")
{
sf::Music* currentSong = mSoundFileCache.GetSong(mCurrentSongName);
if (currentSong->GetStatus() == sf::Sound::Playing)
{
currentSong->Stop();
}
}
}
//Checks if any sound is playing
bool SFMLSoundProvider::IsSoundPlaying()
{
for (int i = 0; i < MAX_SOUND_CHANNELS; ++i)
{
if (mCurrentSounds[i].GetStatus() == sf::Sound::Playing)
return true;
}
return false;
}
bool SFMLSoundProvider::IsSongPlaying()
{
if (mCurrentSongName != "")
{
return mSoundFileCache.GetSong(mCurrentSongName)->GetStatus() == sf::Music::Playing;
}
return false;
}