Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::audio::EventSounds Class Reference

Plays sounds when events are published on an event bus, so gameplay code never calls audio. More...

#include <aegis2d/audio/event_sounds.hpp>

Public Member Functions

 EventSounds (AudioDevice &device, core::EventBus &events) noexcept
 Creates an EventSounds with no rule.
 
 EventSounds (const EventSounds &)=delete
 
EventSounds & operator= (const EventSounds &)=delete
 
 EventSounds (EventSounds &&)=delete
 
EventSounds & operator= (EventSounds &&)=delete
 
 ~EventSounds ()
 Removes every rule from the bus.
 
template<typename Event >
void play_on (SoundId sound, const SoundOptions &options={})
 Plays a sound each time an Event is published.
 
template<typename Event , typename Filter >
requires std::predicate<Filter&, const Event&> && std::copy_constructible<Filter>
void play_on (SoundId sound, Filter filter, const SoundOptions &options={})
 Plays a sound each time an Event that filter accepts is published.
 
void clear () noexcept
 Removes every rule from the bus.
 
std::size_t rules () const noexcept
 Number of rules.
 

Detailed Description

Plays sounds when events are published on an event bus, so gameplay code never calls audio.

Gameplay publishes what happens (shmup::ShotFired, ecs::Explosion, ecs::LifeLost...) on a core::EventBus; the game says once, at startup, which sound each event plays:

audio::EventSounds sounds{audio, events};
sounds.play_on<shmup::ShotFired>(audio.load(core::asset_path("shot.wav")));
sounds.play_on<ecs::Explosion>(audio.load(core::asset_path("explosion.wav")));
sounds.play_on<ecs::LifeLost>(audio.load(core::asset_path("death.wav")));
sounds.play_on<shmup::ShotFired>(enemy_shot, [](const shmup::ShotFired& shot) {
return !shot.player; // only some events of a type
});
The default audio output and the sounds it plays.
Definition audio.hpp:54
SoundId load(const std::filesystem::path &path)
Loads a sound from a WAV file.
Plays sounds when events are published on an event bus, so gameplay code never calls audio.
Definition event_sounds.hpp:51
void play_on(SoundId sound, const SoundOptions &options={})
Plays a sound each time an Event is published.
Definition event_sounds.hpp:71
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.
An entity whose health reached zero blows up: an enemy shot down, or a player that loses a life.
Definition events.hpp:31
A player who lost a life during the tick.
Definition lives.hpp:17
An entity fired: one shot leaves it during the tick.
Definition weapons.hpp:85

Each play_on() adds a rule: the event type, the sound, an optional filter and SoundOptions. Several rules can listen to the same event type. A sound that SDL cannot start is skipped with a warning in the log: a sound never stops the game. The rules are removed from the bus when the EventSounds is destroyed, so it must not outlive the bus or the device; use it from the game thread, like the bus.

Constructor & Destructor Documentation

◆ EventSounds() [1/3]

aegis2d::audio::EventSounds::EventSounds ( AudioDevice &  device,
core::EventBus &  events 
)
noexcept

Creates an EventSounds with no rule.

Parameters
deviceDevice that plays the sounds.
eventsBus the events are published on.

◆ EventSounds() [2/3]

aegis2d::audio::EventSounds::EventSounds ( const EventSounds &  )
delete

◆ EventSounds() [3/3]

aegis2d::audio::EventSounds::EventSounds ( EventSounds &&  )
delete

◆ ~EventSounds()

aegis2d::audio::EventSounds::~EventSounds ( )

Removes every rule from the bus.

Member Function Documentation

◆ clear()

void aegis2d::audio::EventSounds::clear ( )
noexcept

Removes every rule from the bus.

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ play_on() [1/2]

template<typename Event >
void aegis2d::audio::EventSounds::play_on ( SoundId  sound,
const SoundOptions &  options = {} 
)
inline

Plays a sound each time an Event is published.

Template Parameters
EventEvent type, as published on the bus.
Parameters
soundIdentifier returned by AudioDevice::load().
optionsChannel and shortest time between two starts.
Exceptions
std::out_of_rangeif sound was not loaded.

◆ play_on() [2/2]

template<typename Event , typename Filter >
requires std::predicate<Filter&, const Event&> && std::copy_constructible<Filter>
void aegis2d::audio::EventSounds::play_on ( SoundId  sound,
Filter  filter,
const SoundOptions &  options = {} 
)
inline

Plays a sound each time an Event that filter accepts is published.

Template Parameters
EventEvent type, as published on the bus.
Parameters
soundIdentifier returned by AudioDevice::load().
filterCopyable callable taking the event and returning true to play the sound, e.g. only for the shots of enemies.
optionsChannel and shortest time between two starts.
Exceptions
std::out_of_rangeif sound was not loaded.

◆ rules()

std::size_t aegis2d::audio::EventSounds::rules ( ) const
noexcept

Number of rules.

Returns
The play_on() calls since the creation or the last clear().

The documentation for this class was generated from the following file: