-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSound.cpp
80 lines (74 loc) · 1.87 KB
/
BSound.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
#include "BSound.h"
BSound::~BSound()
{
//Free the music
Mix_FreeMusic(gMusic);
gMusic = NULL;
Mix_FreeChunk(gMedium);
gMedium = NULL;
Mix_FreeChunk(gLow);
gLow = NULL;
}
bool BSound::loadMusicMedia()
{
//Loading success flag
bool success = true;
//Load music
gMusic = Mix_LoadMUS("Breakout_Media/BackgroundMusic.wav");
if (gMusic == NULL)
{
std::cout << "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError();
success = false;
}
gMedium = Mix_LoadWAV("Breakout_Media/medium.wav");
if (gMedium == NULL)
{
std::cout << "Failed to load medium sound effect! SDL_mixer Error: %s\n", Mix_GetError();
success = false;
}
gLow = Mix_LoadWAV("Breakout_Media/low.wav");
if (gLow == NULL)
{
std::cout << "Failed to load low sound effect! SDL_mixer Error: %s\n", Mix_GetError();
success = false;
}
return success;
}
void BSound::handleMusicEvent(SDL_Event* e)
{
//Handle key press
if (e->type == SDL_KEYDOWN)
{
switch (e->key.keysym.sym)
{
case SDLK_9:
//If there is no music playing
if (Mix_PlayingMusic() == 0)
{
//Play the music
Mix_PlayMusic(gMusic, -1);
}
//If music is being played
else
{
//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();
}
}
break;
case SDLK_0:
//Stop the music
Mix_HaltMusic();
break;
}
}
}