-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundEffects.cpp
126 lines (98 loc) · 2.17 KB
/
SoundEffects.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "SoundEffects.h"
SoundEffects::SoundEffects(){}
bool SoundEffects::init() {
//Some code borrowed from lazyfoo.net SDL tutorials
//Initialize SDL subsystems (only audio subsystem necessary?)
//if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
if ( SDL_Init ( SDL_INIT_AUDIO ) == -1)
{
return false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//If everything initialized fine
return true;
}
bool SoundEffects::load_files() {
enable_Sound = true;
//Load the music
gMusic = Mix_LoadMUS( "music.mp3" );
//If there was a problem loading the music
if( gMusic == NULL )
{
return false;
}
//Load the sound effects
//Fillers
gSwing = Mix_LoadWAV( "swing.wav" );
/*
gHigh = Mix_LoadWAV( "high.wav" );
gMed = Mix_LoadWAV( "medium.wav" );
gLow = Mix_LoadWAV( "low.wav" );
//If there was a problem loading the sound effects
if( ( gScratch == NULL ) || ( gHigh == NULL ) || ( gMed == NULL ) || ( gLow == NULL ) )
{
return false;
}
*/
//If everything loaded fine
return true;
}
void SoundEffects::clean_up() {
//Free the sound effects
Mix_FreeChunk( gSwing );
/*
Mix_FreeChunk( gHigh );
Mix_FreeChunk( gMed );
Mix_FreeChunk( gLow );
*/
//Free the music
Mix_FreeMusic( gMusic );
//Quit SDL_mixer
Mix_CloseAudio();
//Quit SDL
SDL_Quit();
}
void SoundEffects::playMusic(){
Mix_PlayMusic(gMusic, -1);
}
void SoundEffects::playEffect(int s){
if(enable_Sound)
{
switch(s) {
case 1:
Mix_PlayChannel( -1, gSwing, 0 );
}
}
}
void SoundEffects::pauseMusic(){
//If the music is paused
if( Mix_PausedMusic() == 1 )
{
//Resume the music
if(enable_Sound)
{
Mix_ResumeMusic();
}
}
else
{
//Pause the music
Mix_PauseMusic();
}
}
void SoundEffects::enableSound()
{
if(enable_Sound)
{
enable_Sound = false;
Mix_PauseMusic();
}
else
{
enable_Sound = true;
}
}