-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundManager.cpp
60 lines (51 loc) · 1.31 KB
/
SoundManager.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
//
// SoundManager.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 26/03/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#include "SoundManager.h"
SoundManager* SoundManager::s_pInstance;
SoundManager::SoundManager()
{
Mix_OpenAudio(22050, AUDIO_S16, 2, (4096 / 2));
}
SoundManager::~SoundManager()
{
Mix_CloseAudio();
}
bool SoundManager::load(std::string fileName, std::string const &id, sound_type type)
{
if(type == SOUND_MUSIC)
{
Mix_Music* pMusic = Mix_LoadMUS(fileName.c_str());
if(pMusic == 0)
{
std::cout << "Could not load music: ERROR - " << Mix_GetError() << std::endl;
return false;
}
m_music[id] = pMusic;
return true;
}
else if(type == SOUND_SFX)
{
Mix_Chunk* pChunk = Mix_LoadWAV(fileName.c_str());
if(pChunk == 0)
{
std::cout << "Could not load SFX: ERROR - " << Mix_GetError() << std::endl;
return false;
}
m_sfxs[id] = pChunk;
return true;
}
return false;
}
void SoundManager::playMusic(std::string const &id, int loop)
{
Mix_PlayMusic(m_music[id], loop);
}
void SoundManager::playSound(std::string const &id, int loop)
{
Mix_PlayChannel(-1, m_sfxs[id], loop);
}