group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
SfxSystem.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "SfxTypes.hpp"
14
15#include <SDL3/SDL.h>
16
17#include <array>
18#include <cstdint>
19
28{
29public:
32 bool init();
33
37 void update(float dt, const Registry& registry);
38
40 void quit();
41
45 void play(SfxId id, float gain = 1.0f);
46
48 void stop(SfxId id);
49
50 // --- Volume control ---
51 void setMasterVolume(float v) { masterVolume_ = v; }
52 void setCategoryVolume(SfxCategory cat, float v);
53 float masterVolume() const { return masterVolume_; }
54 float categoryVolume(SfxCategory cat) const;
55
56 // --- entt::dispatcher event handlers ---
57 void onWeaponFired(const WeaponFiredEvent& e);
58 void onExplosion(const ExplosionEvent& e);
59
61 bool isInitialized() const { return device_ != 0; }
62
63private:
64 SDL_AudioDeviceID device_ = 0;
65
66 std::array<SoundClip, static_cast<size_t>(SfxId::_Count)> clips_;
67
69 struct Voice
70 {
71 SDL_AudioStream* stream = nullptr;
72 bool active = false;
73 SfxId playingId = SfxId::_Count;
74 float duration = 0.0f;
75 float elapsed = 0.0f;
76 };
77 static constexpr int kMaxVoices = 32;
78 std::array<Voice, kMaxVoices> voices_{};
79
80 float masterVolume_ = 0.8f;
81 std::array<float, static_cast<size_t>(SfxCategory::_Count)> categoryVolumes_{};
82
84 std::array<float, static_cast<size_t>(SfxId::_Count)> cooldowns_{};
85
86 // --- Client-side state tracking for event detection ---
87 float prevHealth_ = 100.0f;
88 float prevArmor_ = 100.0f;
89 int prevDeaths_ = 0;
90 int prevKills_ = 0;
91 float healingSoundCooldown_ = 0.0f;
92 bool stateInitialized_ = false;
93
95 bool loadClip(SfxId id, const char* filename, SfxCategory cat, float gain, float cooldownSecs);
96
98 Voice* acquireVoice();
99
101 float effectiveGain(SfxId id, float extraGain) const;
102};
Event structs dispatched via entt::dispatcher for particle spawning.
Shared ECS registry type alias for the game engine.
entt::registry Registry
Shared ECS registry type alias.
Definition Registry.hpp:11
Sound effect identifiers, categories, and the SoundClip data type.
SfxId
Identifies a loaded sound clip.
Definition SfxTypes.hpp:13
SfxCategory
Sound category for per-category volume control.
Definition SfxTypes.hpp:48
Client-side sound effects system.
Definition SfxSystem.hpp:28
void setMasterVolume(float v)
Definition SfxSystem.hpp:51
bool init()
Initialise SDL audio subsystem, open default playback device, load all clips.
Definition SfxSystem.cpp:96
void quit()
Destroy all active streams, close the audio device, quit audio subsystem.
Definition SfxSystem.cpp:166
void setCategoryVolume(SfxCategory cat, float v)
Definition SfxSystem.cpp:253
float categoryVolume(SfxCategory cat) const
Definition SfxSystem.cpp:258
void play(SfxId id, float gain=1.0f)
Play a sound immediately.
Definition SfxSystem.cpp:188
void onExplosion(const ExplosionEvent &e)
Definition SfxSystem.cpp:286
void update(float dt, const Registry &registry)
Per-frame update: decrement cooldowns, retire finished voices, detect state changes.
Definition SfxSystem.cpp:295
void onWeaponFired(const WeaponFiredEvent &e)
Definition SfxSystem.cpp:267
float masterVolume() const
Definition SfxSystem.hpp:53
void stop(SfxId id)
Stop all active voices playing the given sound.
Definition SfxSystem.cpp:239
bool isInitialized() const
True after a successful init().
Definition SfxSystem.hpp:61
Emitted when a rocket/grenade explodes.
Definition ParticleEvents.hpp:33
Emitted when a weapon fires (both hitscan and projectile).
Definition ParticleEvents.hpp:13