From 15c285c8796562897252ee720d27ce779dadc346 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Sun, 6 Jun 2021 23:10:07 +1000 Subject: [PATCH] Refactor calc_snd_position to take a Point instead of x/y params Appears this function is only called internally despite being in the effects header. --- Source/effects.cpp | 14 ++++++-------- Source/effects.h | 3 ++- test/effects_test.cpp | 10 +++++----- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Source/effects.cpp b/Source/effects.cpp index 90ac2a359..b4857f2b4 100644 --- a/Source/effects.cpp +++ b/Source/effects.cpp @@ -1143,19 +1143,17 @@ void FreeMonsterSnd() } } -bool calc_snd_position(int x, int y, int *plVolume, int *plPan) +bool calc_snd_position(Point soundPosition, int *plVolume, int *plPan) { int pan, volume; const auto &playerPosition = plr[myplr].position.tile; + const auto delta = soundPosition - playerPosition; - const int dx = x - playerPosition.x; - const int dy = y - playerPosition.y; - - pan = (dx - dy) * 256; + pan = (delta.x - delta.y) * 256; *plPan = clamp(pan, PAN_MIN, PAN_MAX); - volume = playerPosition.ApproxDistance({ x, y }); + volume = playerPosition.ApproxDistance(soundPosition); volume *= -64; if (volume <= ATTENUATION_MIN) @@ -1183,7 +1181,7 @@ static void PlaySFX_priv(TSFX *pSFX, bool loc, int x, int y) lPan = 0; lVolume = 0; - if (loc && !calc_snd_position(x, y, &lVolume, &lPan)) { + if (loc && !calc_snd_position({ x, y }, &lVolume, &lPan)) { return; } @@ -1218,7 +1216,7 @@ void PlayEffect(int i, int mode) return; } - if (!calc_snd_position(monster[i].position.tile.x, monster[i].position.tile.y, &lVolume, &lPan)) + if (!calc_snd_position(monster[i].position.tile, &lVolume, &lPan)) return; snd_play_snd(snd, lVolume, lPan); diff --git a/Source/effects.h b/Source/effects.h index b1e726611..b6a70b64a 100644 --- a/Source/effects.h +++ b/Source/effects.h @@ -8,6 +8,7 @@ #include #include +#include "engine.h" #include "sound.h" namespace devilution { @@ -1187,7 +1188,7 @@ void ui_sound_init(); void effects_play_sound(const char *snd_file); #ifndef NOSOUND -bool calc_snd_position(int x, int y, int *plVolume, int *plPan); +bool calc_snd_position(Point soundPosition, int *plVolume, int *plPan); int GetSFXLength(int nSFX); #endif diff --git a/test/effects_test.cpp b/test/effects_test.cpp index aaf556c2e..83a8f3033 100644 --- a/test/effects_test.cpp +++ b/test/effects_test.cpp @@ -10,7 +10,7 @@ TEST(Effects, calc_snd_position_center) plr[myplr].position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; - EXPECT_EQ(calc_snd_position(50, 50, &plVolume, &plPan), true); + EXPECT_EQ(calc_snd_position({ 50, 50 }, &plVolume, &plPan), true); EXPECT_EQ(plVolume, 0); EXPECT_EQ(plPan, 0); } @@ -20,7 +20,7 @@ TEST(Effects, calc_snd_position_near) plr[myplr].position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; - EXPECT_EQ(calc_snd_position(55, 50, &plVolume, &plPan), true); + EXPECT_EQ(calc_snd_position({ 55, 50 }, &plVolume, &plPan), true); EXPECT_EQ(plVolume, -320); EXPECT_EQ(plPan, 1280); } @@ -30,7 +30,7 @@ TEST(Effects, calc_snd_position_out_of_range) plr[myplr].position.tile = { 12, 12 }; int plVolume = 1234; int plPan = 0; - EXPECT_EQ(calc_snd_position(112, 112, &plVolume, &plPan), false); + EXPECT_EQ(calc_snd_position({ 112, 112 }, &plVolume, &plPan), false); EXPECT_EQ(plVolume, 1234); EXPECT_EQ(plPan, 0); } @@ -40,7 +40,7 @@ TEST(Effects, calc_snd_position_extreme_right) plr[myplr].position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; - EXPECT_EQ(calc_snd_position(75, 25, &plVolume, &plPan), true); + EXPECT_EQ(calc_snd_position({ 75, 25 }, &plVolume, &plPan), true); EXPECT_EQ(plVolume, -2176); EXPECT_EQ(plPan, 6400); } @@ -50,7 +50,7 @@ TEST(Effects, calc_snd_position_extreme_left) plr[myplr].position.tile = { 50, 50 }; int plVolume = 0; int plPan = 0; - EXPECT_EQ(calc_snd_position(25, 75, &plVolume, &plPan), true); + EXPECT_EQ(calc_snd_position({ 25, 75 }, &plVolume, &plPan), true); EXPECT_EQ(plVolume, -2176); EXPECT_EQ(plPan, -6400); }