-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBGMusic.cpp
117 lines (105 loc) · 2.51 KB
/
BGMusic.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
#include "BGMusic.h"
#include <iostream>
BGMusic::BGMusic() {
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
if (!loadMedia())
{
printf( "Failed to load media!\n");
}
if( Mix_PlayingMusic() == 0 )
{
cout << "HELLO\n";
//Play the music
Mix_PlayMusic( bgmusic, -1 );
}
}
BGMusic::~BGMusic() {
closeMedia();
}
bool BGMusic::loadMedia()
{
//Load music
bgmusic = Mix_LoadMUS( "bgmusic.wav" );
if( bgmusic == NULL )
{
cout << Mix_GetError();
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
start_sound = Mix_LoadWAV( "start_gong.wav" );
if( start_sound == NULL )
{
cout << Mix_GetError();
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
victory_sound = Mix_LoadWAV( "victory_sound.wav" );
if( victory_sound == NULL )
{
cout << Mix_GetError();
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
lose_sound = Mix_LoadWAV( "lose_sound.wav" );
if( lose_sound == NULL )
{
cout << Mix_GetError();
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
return success;
}
void BGMusic::playOrPause()
{
//If the music is paused
if( Mix_PausedMusic() == 1 )
{
//Resume the music
Mix_ResumeMusic();
}
//If the music is playing
else
{
//Pause the music
Mix_PauseMusic();
}
}
void BGMusic::start()
{
Mix_PlayChannel( -1, start_sound, 0 );
}
void BGMusic::win()
{
Mix_PlayChannel( -1, victory_sound, 0 );
}
void BGMusic::lose()
{
Mix_PlayChannel( -1, lose_sound, 0 );
}
void BGMusic::closeMedia()
{
Mix_FreeMusic( bgmusic );
bgmusic = NULL;
Mix_FreeChunk( start_sound );
start_sound = NULL;
Mix_FreeChunk( victory_sound );
victory_sound = NULL;
Mix_FreeChunk( lose_sound );
lose_sound = NULL;
Mix_FreeChunk( aggro_sound );
aggro_sound = NULL;
//Quit SDL subsystems
Mix_Quit();
SDL_Quit();
}