diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 6f66f2c4a..53f97f129 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -106,6 +106,7 @@ set(libdevilutionx_SRCS engine/palette.cpp engine/path.cpp engine/random.cpp + engine/sound_position.cpp engine/surface.cpp engine/trn.cpp diff --git a/Source/effects.cpp b/Source/effects.cpp index efcd272e4..8ca2ddfc3 100644 --- a/Source/effects.cpp +++ b/Source/effects.cpp @@ -8,6 +8,7 @@ #include "engine/random.hpp" #include "engine/sound.h" #include "engine/sound_defs.hpp" +#include "engine/sound_position.hpp" #include "init.h" #include "player.h" #include "utils/stdcompat/algorithm.hpp" @@ -1201,25 +1202,6 @@ void stream_stop() } } -bool CalculateSoundPosition(Point soundPosition, int *plVolume, int *plPan) -{ - const auto &playerPosition = MyPlayer->position.tile; - const auto delta = soundPosition - playerPosition; - - int pan = (delta.deltaX - delta.deltaY) * 256; - *plPan = clamp(pan, PAN_MIN, PAN_MAX); - - int volume = playerPosition.ApproxDistance(soundPosition); - volume *= -64; - - if (volume <= ATTENUATION_MIN) - return false; - - *plVolume = volume; - - return true; -} - void PlaySFX(_sfx_id psfx) { psfx = RndSFX(psfx); diff --git a/Source/effects.h b/Source/effects.h index c4ebd6d9e..e0f74f8f2 100644 --- a/Source/effects.h +++ b/Source/effects.h @@ -1177,7 +1177,6 @@ extern _sfx_id sfxdnum; bool effect_is_playing(int nSFX); void stream_stop(); -bool CalculateSoundPosition(Point soundPosition, int *plVolume, int *plPan); void PlaySFX(_sfx_id psfx); void PlaySfxLoc(_sfx_id psfx, Point position, bool randomizeByCategory = true); void sound_stop(); diff --git a/Source/effects_stubs.cpp b/Source/effects_stubs.cpp index 650f90c39..72942135e 100644 --- a/Source/effects_stubs.cpp +++ b/Source/effects_stubs.cpp @@ -12,7 +12,6 @@ _sfx_id sfxdnum; // clang-format off bool effect_is_playing(int nSFX) { return false; } void stream_stop() { } -bool CalculateSoundPosition(Point soundPosition, int *plVolume, int *plPan) { return false; } void PlaySFX(_sfx_id psfx) { switch (psfx) { diff --git a/Source/engine/sound_position.cpp b/Source/engine/sound_position.cpp new file mode 100644 index 000000000..e2ab755ec --- /dev/null +++ b/Source/engine/sound_position.cpp @@ -0,0 +1,27 @@ +#include "engine/sound_position.hpp" + +#include "engine/sound_defs.hpp" +#include "player.h" + +namespace devilution { + +bool CalculateSoundPosition(Point soundPosition, int *plVolume, int *plPan) +{ + const auto &playerPosition = MyPlayer->position.tile; + const auto delta = soundPosition - playerPosition; + + int pan = (delta.deltaX - delta.deltaY) * 256; + *plPan = clamp(pan, PAN_MIN, PAN_MAX); + + int volume = playerPosition.ApproxDistance(soundPosition); + volume *= -64; + + if (volume <= ATTENUATION_MIN) + return false; + + *plVolume = volume; + + return true; +} + +} // namespace devilution diff --git a/Source/engine/sound_position.hpp b/Source/engine/sound_position.hpp new file mode 100644 index 000000000..de062c59b --- /dev/null +++ b/Source/engine/sound_position.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "engine/point.hpp" + +namespace devilution { + +bool CalculateSoundPosition(Point soundPosition, int *plVolume, int *plPan); + +} // namespace devilution diff --git a/Source/monster.cpp b/Source/monster.cpp index 6b0cea171..2224e3e27 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -21,6 +21,7 @@ #include "engine/points_in_rectangle_range.hpp" #include "engine/random.hpp" #include "engine/render/clx_render.hpp" +#include "engine/sound_position.hpp" #include "engine/world_tile.hpp" #include "init.h" #include "levels/crypt.h" diff --git a/test/effects_test.cpp b/test/effects_test.cpp index 36a576e62..73f55c8ab 100644 --- a/test/effects_test.cpp +++ b/test/effects_test.cpp @@ -1,6 +1,7 @@ #include #include "effects.h" +#include "engine/sound_position.hpp" #include "player.h" using namespace devilution;