-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundUnit.cpp
More file actions
47 lines (40 loc) · 1.02 KB
/
Copy pathSoundUnit.cpp
File metadata and controls
47 lines (40 loc) · 1.02 KB
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
#include "SoundUnit.hpp"
#include "Logger.hpp"
#include <SDL2/SDL.h> // Required for SDL_InitSubSystem and SDL_INIT_AUDIO
#include <iostream>
TSound::TSound(const std::string& filename) : fChunk(nullptr), fName("") {
Log("loading " + filename);
fChunk = Mix_LoadWAV(filename.c_str());
if (!fChunk) {
Log("Unable to load wav " + filename);
}
}
TSound::~TSound() {
if (fChunk) {
Mix_FreeChunk(fChunk);
fChunk = nullptr;
}
}
void TSound::play() {
if (fChunk) {
Mix_PlayChannel(-1, fChunk, 0);
}
}
bool InitSound() {
Log("Initializing Sound and Mixer");
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
Log("No sdl audio: " + std::string(SDL_GetError()));
return false;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) != 0) {
Log("No sdl mixer: " + std::string(Mix_GetError()));
return false;
}
Mix_Init(MIX_INIT_MOD);
return true;
}
void CloseSound() {
Mix_HaltChannel(-1);
Mix_CloseAudio();
Mix_Quit();
}