-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoundManager.cpp
More file actions
88 lines (77 loc) · 2.03 KB
/
SoundManager.cpp
File metadata and controls
88 lines (77 loc) · 2.03 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
#include "SoundManager.h"
#include "Exceptions.h"
namespace typing
{
void Sound::Play(int loops)
{
m_channel = Mix_PlayChannel(-1, m_chunk, loops);
}
void Sound::FadeIn(int loops, int ms)
{
m_channel = Mix_FadeInChannel(-1, m_chunk, loops, ms);
}
void Sound::FadeOut(int ms)
{
if (m_channel != -1)
{
(void)Mix_FadeOutChannel(m_channel, ms);
}
}
void Sound::Stop()
{
if (m_channel != -1)
{
Mix_HaltChannel(m_channel);
}
}
std::auto_ptr<SoundManager> SoundManager::m_singleton(new SoundManager);
SoundManager& SoundManager::GetSoundManager()
{
return *(m_singleton.get());
}
Sound SoundManager::Add(const std::string& soundName)
{
if(m_soundMap.find(soundName) == m_soundMap.end())
{
Mix_Chunk * sound = Mix_LoadWAV(soundName.c_str());
if (!sound)
{
throw FileNotFoundException(soundName);
}
else
{
m_soundMap[soundName] = sound;
}
return Sound(sound);
} else {
return Get(soundName);
}
}
Mix_Chunk * SoundManager::GetChunk(const std::string& soundName) const
{
SoundMap::const_iterator iter = m_soundMap.find(soundName);
if (iter == m_soundMap.end())
{
throw MediaNotLoadedException(soundName);
}
else
{
return iter->second;
}
}
Sound SoundManager::Get(const std::string& soundName) const
{
return Sound(GetChunk(soundName));
}
// SoundManager::Play can be used for 'fire and forget' sound playing.
// The underlying Sound is not returned so there is no further control of the sound
// after it has been started
void SoundManager::Play(const std::string& soundName) const
{
Get(soundName).Play(0);
}
void SoundManager::StopAll() const
{
Mix_HaltChannel(-1);
}
}