Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
audio.cpp
1/*
2 Wizard Engine
3 Copyright (C) 2023-2024 Zana Domán
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
23#define __WIZARD_ENGINE_INTERNAL__
24
27
28int8_t wze::audio::volume() {
29 return (int8_t)Mix_MasterVolume(-1);
30}
31
32void wze::audio::set_volume(int8_t volume) {
33 Mix_MasterVolume(volume);
34}
35
36std::vector<std::shared_ptr<wze::speaker>>& wze::audio::speakers() {
37 return _speakers;
38}
39
40void wze::audio::update() {
41 speakers().erase(
42 std::remove_if(speakers().begin(), speakers().end(),
43 [](std::shared_ptr<speaker> const& speaker) -> bool {
44 return !speaker || !speaker->playing();
45 }),
46 speakers().end());
47
48 std::for_each(speaker::instances().begin(), speaker::instances().end(),
49 [](speaker* instance) -> void {
50 if (instance->auto_panning()) {
51 instance->align_panning();
52 }
53 });
54}
55
56int32_t wze::audio::request_channel() {
57 int32_t channel;
58
59 for (channel = 0;
60 channel != std::numeric_limits<int32_t>::max() - MIX_CHANNELS;
61 ++channel) {
62 if (std::find(_channels.begin(), _channels.end(), channel) ==
63 _channels.end()) {
64 _channels.push_back(channel);
65 if (_maximum_channel < channel) {
66 _maximum_channel = channel;
67 Mix_AllocateChannels(MIX_CHANNELS + _maximum_channel + 1);
68 }
69 return MIX_CHANNELS + channel;
70 }
71 }
72
73 throw exception("Channel allocation failed");
74}
75
76void wze::audio::drop_channel(int32_t channel) {
77 if (!(bool)Mix_UnregisterAllEffects(channel) ||
78 (bool)Mix_HaltChannel(channel)) {
79 throw exception(Mix_GetError());
80 }
81
82 _channels.erase(
83 std::find(_channels.begin(), _channels.end(), channel -= MIX_CHANNELS));
84 if (channel == _maximum_channel) {
85 _maximum_channel =
86 _channels.empty()
87 ? -1
88 : *std::max_element(_channels.begin(), _channels.end());
89 Mix_AllocateChannels(MIX_CHANNELS + _maximum_channel + 1);
90 }
91}
92
93void wze::audio::pause() {
94 Mix_Pause(-1);
95}
96
97void wze::audio::resume() {
98 Mix_Resume(-1);
99}
100
101void wze::audio::stop() {
102 if ((bool)Mix_HaltChannel(-1)) {
103 throw exception(Mix_GetError());
104 }
105}
106
107int32_t wze::audio::_maximum_channel = -1;
108std::vector<int32_t> wze::audio::_channels = {};
109std::vector<std::shared_ptr<wze::speaker>> wze::audio::_speakers = {};
Subsystem to handle global audio.
Generic exception.