-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioManager.cpp
75 lines (54 loc) · 2.14 KB
/
AudioManager.cpp
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
//--------------------------------------------------------------//
// AudioManager.cpp //
// Singleton //
// Plays WAV files //
// Can play one BGM at a time, and many sound effects at once //
// //
// By: Ather Omar //
//--------------------------------------------------------------//
#include "AudioManager.h"
//----------------------------------------------------------
// QuickSDL
//----------------------------------------------------------
namespace QuickSDL {
//Initializing to NULL
AudioManager* AudioManager::sInstance = NULL;
AudioManager* AudioManager::Instance() {
//Create a new instance if no instance was created before
if(sInstance == NULL)
sInstance = new AudioManager();
return sInstance;
}
void AudioManager::Release() {
delete sInstance;
sInstance = NULL;
}
AudioManager::AudioManager() {
mAssetMgr = AssetManager::Instance();
//Initialize the SDL_mixer using 44.1 khz and 2 channels (change if needed) and handle initialization errors
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0) {
printf("Mixer Initialization Error: %s\n", Mix_GetError());
}
}
AudioManager::~AudioManager() {
mAssetMgr = NULL;
//Close the SDL_mixer
Mix_Quit();
}
void AudioManager::PlayMusic(std::string filename, int loops) {
//Play the Mix_Music* loaded in the AssetManager so that the file is not reloaded every time
Mix_PlayMusic(mAssetMgr->GetMusic(filename), loops);
}
void AudioManager::PauseMusic() {
if(Mix_PlayingMusic() != 0)
Mix_PauseMusic();
}
void AudioManager::ResumeMusic() {
if(Mix_PausedMusic() != 0)
Mix_ResumeMusic();
}
void AudioManager::PlaySFX(std::string filename, int loops, int channel) {
//Play the Mix_Chunk* loaded in the AssetManager so that the file is not reloaded every time
Mix_PlayChannel(channel, mAssetMgr->GetSFX(filename), loops);
}
}