Skip to content

Commit

Permalink
Introduce SoundVisualization struct to resolve code duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Nov 29, 2023
1 parent a348a90 commit 34b27ba
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 58 deletions.
15 changes: 1 addition & 14 deletions Source/FeatureHelpers/Sound/BombBeepSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@
struct BombBeepSound {
static constexpr auto kFadeAwayStart = 0.0f;
static constexpr auto kFadeAwayDuration = 0.3f;

[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept
{
return (std::max)(1.0f - clipSpaceZ / 1000.0f, 0.4f);
}

[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept
{
if (timeAlive >= kFadeAwayStart) {
return 1.0f - (std::min)((timeAlive - kFadeAwayStart) / kFadeAwayDuration, 1.0f);
} else {
return 1.0f;
}
}
static constexpr auto kMinScale = 0.4f;

[[nodiscard]] static constexpr bool isSound(std::string_view soundName) noexcept
{
Expand Down
15 changes: 1 addition & 14 deletions Source/FeatureHelpers/Sound/BombDefuseSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@
struct BombDefuseSound {
static constexpr auto kFadeAwayStart = 2.0f;
static constexpr auto kFadeAwayDuration = 1.0f;

[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept
{
return (std::max)(1.0f - clipSpaceZ / 1000.0f, 0.4f);
}

[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept
{
if (timeAlive >= kFadeAwayStart) {
return 1.0f - (std::min)((timeAlive - kFadeAwayStart) / kFadeAwayDuration, 1.0f);
} else {
return 1.0f;
}
}
static constexpr auto kMinScale = 0.4f;

[[nodiscard]] static constexpr bool isSound(std::string_view soundName) noexcept
{
Expand Down
15 changes: 1 addition & 14 deletions Source/FeatureHelpers/Sound/BombPlantSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@
struct BombPlantSound {
static constexpr auto kFadeAwayStart = 2.1f;
static constexpr auto kFadeAwayDuration = 0.4f;

[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept
{
return (std::max)(1.0f - clipSpaceZ / 1000.0f, 0.4f);
}

[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept
{
if (timeAlive >= kFadeAwayStart) {
return 1.0f - (std::min)((timeAlive - kFadeAwayStart) / kFadeAwayDuration, 1.0f);
} else {
return 1.0f;
}
}
static constexpr auto kMinScale = 0.4f;

[[nodiscard]] static constexpr bool isSound(std::string_view soundName) noexcept
{
Expand Down
15 changes: 1 addition & 14 deletions Source/FeatureHelpers/Sound/FootstepSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@
struct FootstepSound {
static constexpr auto kFadeAwayStart = 1.6f;
static constexpr auto kFadeAwayDuration = 0.4f;

[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept
{
return (std::max)(1.0f - clipSpaceZ / 1000.0f, 0.3f);
}

[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept
{
if (timeAlive >= kFadeAwayStart) {
return 1.0f - (std::min)((timeAlive - kFadeAwayStart) / kFadeAwayDuration, 1.0f);
} else {
return 1.0f;
}
}
static constexpr auto kMinScale = 0.3f;

[[nodiscard]] static constexpr bool isSound(std::string_view soundName) noexcept
{
Expand Down
20 changes: 20 additions & 0 deletions Source/FeatureHelpers/Sound/SoundVisualization.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <algorithm>

template <typename SoundType>
struct SoundVisualization {
[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept
{
return (std::max)(1.0f - clipSpaceZ / 1000.0f, SoundType::kMinScale);
}

[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept
{
if (timeAlive >= SoundType::kFadeAwayStart) {
return 1.0f - (std::min)((timeAlive - SoundType::kFadeAwayStart) / SoundType::kFadeAwayDuration, 1.0f);
} else {
return 1.0f;
}
}
};
5 changes: 3 additions & 2 deletions Source/FeatureHelpers/Sound/SoundVisualizationFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <FeatureHelpers/HudInWorldPanels.h>
#include <FeatureHelpers/TogglableFeature.h>
#include <Hooks/ViewRenderHook.h>
#include "SoundVisualization.h"
#include "SoundVisualizationHelpers.h"
#include "SoundWatcher.h"

Expand Down Expand Up @@ -34,7 +35,7 @@ class SoundVisualizationFeature : public TogglableFeature<SoundVisualizationFeat
if (!soundInClipSpace.onScreen())
return;

const auto opacity = SoundType::getOpacity(sound.getTimeAlive(params.globalVarsProvider.getGlobalVars()->curtime));
const auto opacity = SoundVisualization<SoundType>::getOpacity(sound.getTimeAlive(params.globalVarsProvider.getGlobalVars()->curtime));
if (opacity <= 0.0f)
return;

Expand All @@ -52,7 +53,7 @@ class SoundVisualizationFeature : public TogglableFeature<SoundVisualizationFeat
const auto deviceCoordinates = soundInClipSpace.toNormalizedDeviceCoordinates();

PanoramaTransformations{
params.transformFactory.scale(SoundType::getScale(soundInClipSpace.z)),
params.transformFactory.scale(SoundVisualization<SoundType>::getScale(soundInClipSpace.z)),
params.transformFactory.translate(deviceCoordinates.getX(), deviceCoordinates.getY())
}.applyTo(style);

Expand Down
1 change: 1 addition & 0 deletions Source/Osiris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ClInclude Include="FeatureHelpers\Sound\FootstepSound.h" />
<ClInclude Include="FeatureHelpers\Sound\PlayedSound.h" />
<ClInclude Include="FeatureHelpers\Sound\SoundExpiryChecker.h" />
<ClInclude Include="FeatureHelpers\Sound\SoundVisualization.h" />
<ClInclude Include="FeatureHelpers\Sound\SoundVisualizationFeature.h" />
<ClInclude Include="FeatureHelpers\Sound\SoundVisualizationHelpers.h" />
<ClInclude Include="FeatureHelpers\Sound\SoundWatcher.h" />
Expand Down
3 changes: 3 additions & 0 deletions Source/Osiris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@
<ClInclude Include="FeatureHelpers\Sound\SoundVisualizationHelpers.h">
<Filter>FeatureHelpers\Sound</Filter>
</ClInclude>
<ClInclude Include="FeatureHelpers\Sound\SoundVisualization.h">
<Filter>FeatureHelpers\Sound</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="UI\Panorama\CreateGUI.js">
Expand Down

0 comments on commit 34b27ba

Please sign in to comment.