Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
event_sounds.hpp
Go to the documentation of this file.
1
3#pragma once
4
6#include <aegis2d/audio/export.hpp>
8
9#include <chrono>
10#include <concepts>
11#include <cstddef>
12#include <functional>
13#include <utility>
14#include <vector>
15
16namespace aegis2d::audio {
17
25 std::chrono::milliseconds min_interval{50};
26};
27
51class AEGIS2D_AUDIO_EXPORT EventSounds {
52public:
56 EventSounds(AudioDevice& device, core::EventBus& events) noexcept;
57
58 EventSounds(const EventSounds&) = delete;
64
70 template <typename Event>
71 void play_on(SoundId sound, const SoundOptions& options = {}) {
72 play_on<Event>(sound, [](const Event& /*event*/) { return true; }, options);
73 }
74
82 template <typename Event, typename Filter>
83 requires std::predicate<Filter&, const Event&> && std::copy_constructible<Filter>
84 void play_on(SoundId sound, Filter filter, const SoundOptions& options = {}) {
85 const std::size_t rule = add_rule(sound, options);
86 // Subscribed here, in the caller's code: the bus finds its handlers by `typeid`.
87 subscriptions_.push_back(events_.get().subscribe<Event>(
88 [this, rule, filter = std::move(filter)](const Event& event) mutable {
89 if (filter(event)) {
90 trigger(rule);
91 }
92 }));
93 }
94
96 void clear() noexcept;
97
100 [[nodiscard]] std::size_t rules() const noexcept;
101
102private:
103 struct Rule {
104 SoundId sound = 0;
105 SoundOptions options;
106 bool played = false;
107 std::chrono::steady_clock::time_point last{};
108 };
109
110 std::size_t add_rule(SoundId sound, const SoundOptions& options);
111 void trigger(std::size_t rule);
112
113 std::reference_wrapper<AudioDevice> device_;
114 std::reference_wrapper<core::EventBus> events_;
115 std::vector<Rule> rules_;
116 std::vector<core::SubscriptionId> subscriptions_;
117};
118
119} // namespace aegis2d::audio
Audio output: sounds loaded once, then played on a channel, on top of SDL3.
The default audio output and the sounds it plays.
Definition audio.hpp:54
Plays sounds when events are published on an event bus, so gameplay code never calls audio.
Definition event_sounds.hpp:51
EventSounds & operator=(EventSounds &&)=delete
EventSounds(const EventSounds &)=delete
EventSounds(EventSounds &&)=delete
void play_on(SoundId sound, const SoundOptions &options={})
Plays a sound each time an Event is published.
Definition event_sounds.hpp:71
void clear() noexcept
Removes every rule from the bus.
EventSounds & operator=(const EventSounds &)=delete
EventSounds(AudioDevice &device, core::EventBus &events) noexcept
Creates an EventSounds with no rule.
void play_on(SoundId sound, Filter filter, const SoundOptions &options={})
Plays a sound each time an Event that filter accepts is published.
Definition event_sounds.hpp:84
~EventSounds()
Removes every rule from the bus.
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
Typed publish/subscribe event bus with immediate dispatch.
Sound effects and music on top of SDL3 (client only).
Definition audio.hpp:17
Channel
Group of sounds whose volume is set together.
Definition audio.hpp:28
@ Effects
Short sounds of the game: shots, explosions, menus.
std::size_t SoundId
Identifier of a sound loaded by an AudioDevice, given by AudioDevice::load().
Definition audio.hpp:36
Definition endpoint.hpp:85
How EventSounds plays the sound of an event.
Definition event_sounds.hpp:19
std::chrono::milliseconds min_interval
Shortest time between two starts of the sound by this rule: events published sooner are not heard aga...
Definition event_sounds.hpp:25
Channel channel
Channel of the sound, hence its volume.
Definition event_sounds.hpp:21