-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSound.cpp
112 lines (95 loc) · 2.2 KB
/
Sound.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
#include "Sound.h"
#include <SDL.h>
#include <SDL_mixer.h>
#include <iostream>
Sound::Sound()
{
SDL_Init(SDL_INIT_AUDIO);
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16SYS;
int audio_channels = 2;
int audio_buffers = 4096;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Step one: Couldn't init audio: %s", Mix_GetError());
exit(-1);
}
}
Sound::~Sound()
{
for (Mix_Chunk* p : mSFXBank)
Mix_FreeChunk(p);
for (Mix_Music* p : mMusicBank)
Mix_FreeMusic(p);
mSFXBank.clear();
mMusicBank.clear();
SDL_Quit();
}
void Sound::loadSfx(std::vector<std::string> sfxPaths)
{
for (std::string n : sfxPaths) {
Mix_Chunk* sfxMix = Mix_LoadWAV(n.c_str());
if (sfxMix != nullptr)
mSFXBank.push_back(sfxMix);
else
std::cerr << "Failed to load " << n << std::endl;
}
}
void Sound::loadMusic(std::vector<std::string> musicPaths)
{
for (std::string n : musicPaths) {
Mix_Music* musicChunk = Mix_LoadMUS(n.c_str());
if (musicChunk != nullptr)
mMusicBank.push_back(musicChunk);
else
std::cerr << "Failed to load " << n << std::endl;
}
}
void Sound::playSound(int index)
{
if (mSFXBank.size() == 0)
return;
if (mSFXBank[index] != nullptr)
{
Mix_PlayChannel(-1, mSFXBank[index], 0);
}
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Step two: Couldn't init audio: %s", Mix_GetError());
}
}
void Sound::playMusic(int index)
{
if (mMusicBank.size() == 0)
return;
if (mMusicBank[index] != nullptr)
{
Mix_PlayMusic(mMusicBank[index], -1);
}
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Step two: Couldn't init audio: %s", Mix_GetError());
}
}
void Sound::Play_Pause()
{
if (!mPaused)
{
Mix_PauseMusic();
mPaused = true;
}
else if (mPaused)
{
Mix_ResumeMusic();
mPaused = false;
}
else
{
return;
}
}
Sound& Sound::getInstance()
{
static Sound instance;
return instance;
}