-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cpp
72 lines (61 loc) · 1.33 KB
/
Sound.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
#include "Sound.h"
Sound::Sound(const std::string & filename) {
_chunk = Mix_LoadWAV(filename.c_str());
if (!_chunk)
_isLoaded = false;
else
_isLoaded = true;
_refs = 1;
_curchannel = -1;
_isPaused = false;
_filename = filename;
}
Sound::Sound(const Sound & sound) {
_refs = sound._refs;
_curchannel = -1;
_filename = sound._filename;
_isPaused = false;
_chunk = Mix_LoadWAV(sound._filename.c_str());
if (!_chunk)
_isLoaded = false;
else
_isLoaded = true;
}
Sound::~Sound() {
if (_chunk) {
try {
if (_curchannel != -1 && Mix_Playing(_curchannel)) Stop();
Mix_FreeChunk(_chunk);
_chunk = NULL;
} catch (int a) {
if (_chunk)
Mix_FreeChunk(_chunk);
}
}
}
bool Sound::Play(bool loop) {
try {
if (_isLoaded) {
if (loop)
_curchannel = Mix_PlayChannel(-1, _chunk, -1);
else
_curchannel = Mix_PlayChannel(-1, _chunk, 0);
}
} catch (int a) {
return false;
}
return false;
}
void Sound::Stop() {
if (_curchannel > -1) {
Mix_HaltChannel(_curchannel);
_curchannel = -1;
}
}
void Sound::Pause() {
_isPaused = !_isPaused;
if (_isPaused) {
Mix_Pause(_curchannel);
} else
Mix_Resume(_curchannel);
}