blob: 6071fca7310f2526c603b81248494ef0ca738d04 (
plain) (
blame)
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
118
119
120
|
#include "mixer.h"
#include <iostream>
void Mixer::playSound(std::string_view filename) {
Mix_Chunk* chunk = getChunkByFilename(std::string(filename));
int ret = Mix_PlayChannel(-1, chunk, 0);
if (ret == -1) {
throw mix_error();
}
Mix_Volume(ret, soundVolume_);
}
int Mixer::loopSound(std::string_view filename) {
Mix_Chunk* chunk = getChunkByFilename(std::string(filename));
int ret = Mix_PlayChannel(-1, chunk, -1);
if (ret == -1) {
throw mix_error();
}
Mix_Volume(ret, soundVolume_);
return ret;
}
void Mixer::stopChannel(int channel) {
Mix_HaltChannel(channel);
}
void Mixer::pauseSounds() {
Mix_Pause(-1);
}
void Mixer::unpauseSounds() {
Mix_Resume(-1);
}
void Mixer::setSoundVolume(int vol) {
soundVolume_ = MIX_MAX_VOLUME / 10.0 * vol;
Mix_Volume(-1, soundVolume_);
}
void Mixer::playMusic(std::string_view name, int ms) {
Mix_Music* song = getMusicByName(name);
int ret;
if (ms == 0) {
ret = Mix_PlayMusic(song, -1);
} else {
ret = Mix_FadeInMusic(song, -1, ms);
}
if (ret == -1) {
throw mix_error();
}
playingTrack_ = name;
}
void Mixer::fadeoutMusic(int ms) {
if (Mix_FadeOutMusic(ms) == 0) {
throw mix_error();
}
playingTrack_ = "";
}
void Mixer::muteMusic() {
Mix_VolumeMusic(0);
musicMuted_ = true;
}
void Mixer::unmuteMusic() {
Mix_VolumeMusic(musicVolume_);
musicMuted_ = false;
}
void Mixer::setMusicVolume(int vol) {
musicVolume_ = MIX_MAX_VOLUME / 10.0 * vol;
musicMuted_ = false;
Mix_VolumeMusic(musicVolume_);
}
void Mixer::pauseMusic() {
Mix_PauseMusic();
}
void Mixer::unpauseMusic() {
Mix_ResumeMusic();
}
Mix_Chunk* Mixer::getChunkByFilename(std::string filename) {
if (!sounds_.count(filename)) {
Mix_Chunk* sample = Mix_LoadWAV(filename.c_str());
if (!sample) {
throw mix_error();
}
sounds_[filename] = chunk_ptr(sample);
}
return sounds_[filename].get();
}
Mix_Music* Mixer::getMusicByName(std::string_view name) {
std::string filename = "../res/music/" + std::string(name) + ".wav";
if (!music_.count(filename)) {
Mix_Music* song = Mix_LoadMUS(filename.c_str());
if (!song) {
throw mix_error();
}
music_[filename] = music_ptr(song);
}
return music_[filename].get();
}
|