-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundEffect.cpp
77 lines (69 loc) · 1.59 KB
/
SoundEffect.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
#include "SoundEffect.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <string>
#include <iostream>
#include <memory>
SoundEffect::SoundEffect()
{
// initialize variables
mMusic = NULL;
}
SoundEffect ::SoundEffect(std ::string filename)
{
// initialize variables
mMusic = NULL;
loadMusic(filename);
}
SoundEffect::~SoundEffect()
{
// Deallocate
free();
}
// SoundEffect::SoundEffect(const std::string &path, int volume)
// : chunk(Mix_LoadWAV(path.c_str()), Mix_FreeChunk) {
// if (!chunk.get()) {
// // LOG("Couldn't load audio sample: ", path);
// }
// Mix_VolumeChunk(chunk.get(), volume);
// }
bool SoundEffect::loadMusic(std ::string filename)
{
// get rid of pre-existing music
free();
// std::cout << "loading music...\n" ;
mMusic = Mix_LoadWAV(filename.c_str());
if (mMusic == NULL)
{
printf("Failed to load music!\n");
std::cout << Mix_GetError() << "\n";
return false;
}
// std::cout << "loaded music...\n" ;
return true;
}
void SoundEffect::free()
{
// Free music if it exists
if (mMusic != NULL)
{
// Free the sound effects
Mix_FreeChunk(mMusic);
mMusic = NULL;
}
}
void SoundEffect::play()
{
int channel = Mix_PlayChannel(-1, mMusic, 0);
// std::cout << ( mMusic == NULL ) << "\n" ;
if (channel == -1)
{
printf("Mix_PlayChannel: %s\n", Mix_GetError());
}
else
{
// std::cout << "playing on channel " << channel << "\n";
}
// SDL_Delay(1000) ;
// Mix_PlayChannel(-1, chunk.get(), 0);
}