Repository navigation
Expand file tree
/
Copy pathSoundManager.cpp
More file actions
executable file
·74 lines (64 loc) · 1.7 KB
/
Copy pathSoundManager.cpp
File metadata and controls
executable file
·74 lines (64 loc) · 1.7 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
#include "SoundManager.h"
#include "window.h"
#include <iostream>
SoundManager::SoundManager()
{
m_music = nullptr;
}
SoundManager::~SoundManager()
{
for (std::map<string,Mix_Music*>::iterator it = m_musics.begin(); it != m_musics.end(); ++it)
{
Mix_FreeMusic(it->second);
}
for (std::map<string,Mix_Chunk*>::iterator it = m_sfx.begin(); it != m_sfx.end(); ++it)
{
Mix_FreeChunk(it->second);
}
Mix_CloseAudio();
}
void SoundManager::init()
{
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
throw InitError();
Mix_HookMusicFinished(SoundManager::musicFinished);
}
void SoundManager::loadMusic(string _path)
{
Mix_Music* music = m_musics[_path];
if (music == nullptr)
m_musics[_path] = Mix_LoadMUS(_path.c_str());
}
void SoundManager::loadSFX(string _path)
{
Mix_Chunk* sfx = m_sfx[_path];
if (sfx == nullptr)
m_sfx[_path] = Mix_LoadWAV(_path.c_str());
}
void SoundManager::playMusic(string _path, float _fade)
{
string path = (_path != "" ? _path : m_nextMusic);
fadeMusic = (_fade != -1 ? _fade/2.0f : fadeMusic);
Mix_Music* music = m_musics[path];
if (Mix_PlayingMusic())
{
if (m_music != music && Mix_FadeOutMusic(fadeMusic))
m_nextMusic = path;
}
else
{
m_music = music;
if (Mix_FadeInMusic(m_music, -1, fadeMusic) == -1)
cerr << "Mix_FadeInMusic: " << Mix_GetError() << endl;
}
}
void SoundManager::musicFinished()
{
SoundManager::Instance()->playMusic();
}
void SoundManager::playSFX(string _path)
{
Mix_Chunk* sfx = m_sfx[_path];
if (Mix_PlayChannel(-1, sfx, 0) == -1)
cerr << "Mix_PlayChannel: " << Mix_GetError() << endl;
}