Browse Source

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.
pull/2235/head
ephphatha 5 years ago committed by Anders Jenbo
parent
commit
15c285c879
  1. 14
      Source/effects.cpp
  2. 3
      Source/effects.h
  3. 10
      test/effects_test.cpp

14
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);

3
Source/effects.h

@ -8,6 +8,7 @@
#include <cstdint>
#include <memory>
#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

10
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);
}

Loading…
Cancel
Save