VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::AudioSystem Class Reference

System responsible for managing audio playback, mixing, and spatialization. More...

#include <AudioSystem.hpp>

Classes

struct  StandaloneAudio

Public Member Functions

 AudioSystem (vex::Registry &reg)
 Constructor for AudioSystem.
void Init (vex::VirtualFileSystem *vfs)
 Initializes the audio subsystem and binding to the VFS.
void Update (vex::Entity cameraEntity)
 Main update loop for the audio system.
void Shutdown ()
 Shuts down the audio system.
void PlaySound2D (const std::string &filePath, float volume=1.0f, float pitch=1.0f, bool loop=false)
 Quickly plays a 2D sound without requiring an entity or AudioSourceComponent.

Private Member Functions

void OnAudioComponentDestroyed (vex::Registry &registry, vex::Entity entity)
 Callback triggered when an AudioSourceComponent is destroyed.

Private Attributes

SDL_AudioDeviceID deviceID
VirtualFileSystemvfs = nullptr
vex::Registryregistry
std::unordered_map< std::string, std::unique_ptr< AudioClip > > clipCache
 Cache of loaded audio clips to prevent reloading the same file multiple times. Key is the file path.
std::unordered_map< vex::Entity, SDL_AudioStream * > streamMap
 Map linking entities to their active SDL audio streams.
std::vector< StandaloneAudiostandaloneStreams

Detailed Description

System responsible for managing audio playback, mixing, and spatialization.

Handles the lifecycle of SDL audio streams, loads audio clips via the VirtualFileSystem, and updates audio properties (volume, pitch, 3D position) every frame.

Definition at line 27 of file AudioSystem.hpp.

Constructor & Destructor Documentation

◆ AudioSystem()

vex::AudioSystem::AudioSystem ( vex::Registry & reg)

Constructor for AudioSystem.

Parameters
vex::Registry&reg - Reference to the main EnTT registry used for component lookups.

Definition at line 55 of file AudioSystem.cpp.

Member Function Documentation

◆ Init()

void vex::AudioSystem::Init ( vex::VirtualFileSystem * vfs)

Initializes the audio subsystem and binding to the VFS.

Sets up the SDL audio subsystem, opens the default playback device, and connects the component destruction signal to handle automatic stream cleanup.

Parameters
vex::VirtualFileSystem*vfs - Pointer to the initialized VirtualFileSystem for loading audio assets.

Definition at line 58 of file AudioSystem.cpp.

◆ OnAudioComponentDestroyed()

void vex::AudioSystem::OnAudioComponentDestroyed ( vex::Registry & registry,
vex::Entity entity )
private

Callback triggered when an AudioSourceComponent is destroyed.

Ensures the associated SDL audio stream is destroyed to prevent memory leaks or playing "ghost" audio.

Parameters
vex::Registry&registry - The registry where the destruction occurred.
vex::Entityentity - The entity that was destroyed or had the component removed.

Definition at line 198 of file AudioSystem.cpp.

◆ PlaySound2D()

void vex::AudioSystem::PlaySound2D ( const std::string & filePath,
float volume = 1.0f,
float pitch = 1.0f,
bool loop = false )

Quickly plays a 2D sound without requiring an entity or AudioSourceComponent.

Creates Standalone stream for the sound, plays it, and automatically cleans up when playback completes.

Parameters
conststd::string& filePath - Path to the audio file to play.
floatvolume - Volume level (default 1.0f). Range: 0.0 - 1.0+
floatpitch - Pitch/speed multiplier (default 1.0f). Range: > 0.0
boolloop - Whether the sound should loop (default false).

Definition at line 206 of file AudioSystem.cpp.

◆ Shutdown()

void vex::AudioSystem::Shutdown ( )

Shuts down the audio system.

Destroys all active audio streams, clears the clip cache, and closes the SDL audio device.

Definition at line 184 of file AudioSystem.cpp.

◆ Update()

void vex::AudioSystem::Update ( vex::Entity cameraEntity)

Main update loop for the audio system.

Iterates over all entities with AudioSourceComponents. It handles:

  • Auto-loading audio clips if the path is set but the clip is null.
  • Processing state changes (Play/Stop/Pause).
  • Refilling audio buffers for looping sounds.
  • Calculating 3D spatial audio volume based on distance to the camera/listener.
    Parameters
    vex::EntitycameraEntity - The entity representing the listener (usually the active camera) for 3D calculations.

Definition at line 72 of file AudioSystem.cpp.

Member Data Documentation

◆ clipCache

std::unordered_map<std::string, std::unique_ptr<AudioClip> > vex::AudioSystem::clipCache
private

Cache of loaded audio clips to prevent reloading the same file multiple times. Key is the file path.

Definition at line 76 of file AudioSystem.hpp.

◆ deviceID

SDL_AudioDeviceID vex::AudioSystem::deviceID
private

Definition at line 71 of file AudioSystem.hpp.

◆ registry

vex::Registry& vex::AudioSystem::registry
private

Definition at line 73 of file AudioSystem.hpp.

◆ standaloneStreams

std::vector<StandaloneAudio> vex::AudioSystem::standaloneStreams
private

Definition at line 94 of file AudioSystem.hpp.

◆ streamMap

std::unordered_map<vex::Entity, SDL_AudioStream*> vex::AudioSystem::streamMap
private

Map linking entities to their active SDL audio streams.

Definition at line 79 of file AudioSystem.hpp.

◆ vfs

VirtualFileSystem* vex::AudioSystem::vfs = nullptr
private

Definition at line 72 of file AudioSystem.hpp.


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