Skip to content

Commit

Permalink
Underwater sound filter: simplify code, improve cvar description
Browse files Browse the repository at this point in the history
Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
  • Loading branch information
bones-was-here committed Mar 10, 2024
1 parent 9071f64 commit aa40915
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
3 changes: 1 addition & 2 deletions snd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ static const speakerlayout_t snd_speakerlayouts[] =
}
};

void S_SetUnderwaterIntensity(void);

// =======================================================================
// Internal sound data & structures
Expand Down Expand Up @@ -184,7 +183,7 @@ cvar_t snd_spatialization_prologic_frontangle = {CF_CLIENT | CF_ARCHIVE, "snd_sp
cvar_t snd_spatialization_occlusion = {CF_CLIENT | CF_ARCHIVE, "snd_spatialization_occlusion", "1", "enable occlusion testing on spatialized sounds, which simply quiets sounds that are blocked by the world; 1 enables PVS method, 2 enables LineOfSight method, 3 enables both"};

// Cvars declared in snd_main.h (shared with other snd_*.c files)
cvar_t snd_waterfx = {CF_CLIENT | CF_ARCHIVE, "snd_waterfx", "1", "underwater sound filter"};
cvar_t snd_waterfx = {CF_CLIENT | CF_ARCHIVE, "snd_waterfx", "1", "underwater sound filter strength"};
cvar_t _snd_mixahead = {CF_CLIENT | CF_ARCHIVE, "_snd_mixahead", "0.15", "how much sound to mix ahead of time"};
cvar_t snd_streaming = {CF_CLIENT | CF_ARCHIVE, "snd_streaming", "1", "enables keeping compressed ogg sound files compressed, decompressing them only as needed, otherwise they will be decompressed completely at load (may use a lot of memory); when set to 2, streaming is performed even if this would waste memory"};
cvar_t snd_streaming_length = {CF_CLIENT | CF_ARCHIVE, "snd_streaming_length", "1", "decompress sounds completely if they are less than this play time when snd_streaming is 1"};
Expand Down
20 changes: 6 additions & 14 deletions snd_mix.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,32 +323,24 @@ Muffles the intensity of sounds when the player is underwater

static struct
{
float intensity;
float alpha;
float accum[SND_LISTENERS];
float intensity;
float alpha;
float accum[SND_LISTENERS];
}
underwater = {0.f, 1.f, {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}};

void S_SetUnderwaterIntensity(void)
{
float host_frametime = cl.realframetime;
float target = cl.view_underwater ? 1.f : 0.f;

if (snd_waterfx.value < 0.f)
target *= 0.f;
else if (snd_waterfx.value > 2.f)
target *= 2.f;
else
target *= snd_waterfx.value;
float target = cl.view_underwater ? bound(0.f, snd_waterfx.value, 2.f) : 0.f;

if (underwater.intensity < target)
{
underwater.intensity += host_frametime * 4.f;
underwater.intensity += cl.realframetime * 4.f;
underwater.intensity = min(underwater.intensity, target);
}
else if (underwater.intensity > target)
{
underwater.intensity -= host_frametime * 4.f;
underwater.intensity -= cl.realframetime * 4.f;
underwater.intensity = max(underwater.intensity, target);
}

Expand Down

0 comments on commit aa40915

Please sign in to comment.