From 12c853bc3121a0f8ffc911fe4317434dea6a2b92 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 17 Aug 2021 22:11:16 +0200 Subject: [PATCH] Moved the remaining math functions to a math utility namespace. --- es-app/src/VolumeControl.cpp | 2 +- es-app/src/animations/MoveCameraAnimation.h | 2 +- es-core/CMakeLists.txt | 8 +- es-core/src/HelpStyle.h | 2 +- es-core/src/ThemeData.h | 2 +- es-core/src/components/TextListComponent.h | 9 +-- es-core/src/math/Misc.cpp | 82 --------------------- es-core/src/math/Misc.h | 45 ----------- es-core/src/renderers/Renderer.h | 2 +- es-core/src/renderers/Shader_GL21.h | 2 +- es-core/src/resources/TextureResource.h | 2 +- es-core/src/utils/MathUtil.cpp | 81 ++++++++++++++++++++ es-core/src/utils/MathUtil.h | 40 ++++++++++ 13 files changed, 134 insertions(+), 145 deletions(-) delete mode 100644 es-core/src/math/Misc.cpp delete mode 100644 es-core/src/math/Misc.h create mode 100644 es-core/src/utils/MathUtil.cpp create mode 100644 es-core/src/utils/MathUtil.h diff --git a/es-app/src/VolumeControl.cpp b/es-app/src/VolumeControl.cpp index 0b24ff75d..18fd01b0d 100644 --- a/es-app/src/VolumeControl.cpp +++ b/es-app/src/VolumeControl.cpp @@ -9,7 +9,7 @@ #include "VolumeControl.h" #include "Log.h" -#include "math/Misc.h" +#include "utils/MathUtil.h" #if defined(_WIN64) #include diff --git a/es-app/src/animations/MoveCameraAnimation.h b/es-app/src/animations/MoveCameraAnimation.h index bfaef9155..9111a51b1 100644 --- a/es-app/src/animations/MoveCameraAnimation.h +++ b/es-app/src/animations/MoveCameraAnimation.h @@ -10,7 +10,7 @@ #define ES_APP_ANIMATIONS_MOVE_CAMERA_ANIMATION_H #include "animations/Animation.h" -#include "math/Misc.h" +#include "utils/MathUtil.h" class MoveCameraAnimation : public Animation { diff --git a/es-core/CMakeLists.txt b/es-core/CMakeLists.txt index 2cd16b11f..5be39b28d 100644 --- a/es-core/CMakeLists.txt +++ b/es-core/CMakeLists.txt @@ -57,9 +57,6 @@ set(CORE_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiMsgBox.h ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.h - # Math - ${CMAKE_CURRENT_SOURCE_DIR}/src/math/Misc.h - # Renderers ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.h ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Shader_GL21.h @@ -74,6 +71,7 @@ set(CORE_HEADERS # Utils ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.h ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/FileSystemUtil.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/MathUtil.h ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/StringUtil.h ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/TimeUtil.h ) @@ -129,9 +127,6 @@ set(CORE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiMsgBox.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.cpp - # Math - ${CMAKE_CURRENT_SOURCE_DIR}/src/math/Misc.cpp - # Renderer ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer_GL21.cpp @@ -148,6 +143,7 @@ set(CORE_SOURCES # Utils ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/FileSystemUtil.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/MathUtil.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/StringUtil.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/TimeUtil.cpp ) diff --git a/es-core/src/HelpStyle.h b/es-core/src/HelpStyle.h index 4edc0e1dd..b6eb62e68 100644 --- a/es-core/src/HelpStyle.h +++ b/es-core/src/HelpStyle.h @@ -10,7 +10,7 @@ #ifndef ES_CORE_HELP_STYLE_H #define ES_CORE_HELP_STYLE_H -#include "math/Misc.h" +#include "utils/MathUtil.h" #include #include diff --git a/es-core/src/ThemeData.h b/es-core/src/ThemeData.h index 70f3193a1..181c215a2 100644 --- a/es-core/src/ThemeData.h +++ b/es-core/src/ThemeData.h @@ -11,8 +11,8 @@ #ifndef ES_CORE_THEME_DATA_H #define ES_CORE_THEME_DATA_H -#include "math/Misc.h" #include "utils/FileSystemUtil.h" +#include "utils/MathUtil.h" #include #include diff --git a/es-core/src/components/TextListComponent.h b/es-core/src/components/TextListComponent.h index 3f99b0143..4a513be7c 100644 --- a/es-core/src/components/TextListComponent.h +++ b/es-core/src/components/TextListComponent.h @@ -352,8 +352,7 @@ template void TextListComponent::update(int deltaTime) const float limit = mSize.x - mHorizontalMargin * 2.0f; if (textLength > limit) { - // Loop. - // Pixels per second (based on nes-mini font at 1920x1080 to produce a speed of 200). + // Loop the text. const float speed = mFont->sizeText("ABCDEFGHIJKLMNOPQRSTUVWXYZ").x * 0.247f; const float delay = 3000.0f; const float scrollLength = textLength; @@ -366,9 +365,9 @@ template void TextListComponent::update(int deltaTime) while (mMarqueeTime > maxTime) mMarqueeTime -= maxTime; - mMarqueeOffset = static_cast(Math::Scroll::loop(delay, scrollTime + returnTime, - static_cast(mMarqueeTime), - scrollLength + returnLength)); + mMarqueeOffset = static_cast(Utils::Math::Scroll::loop( + delay, scrollTime + returnTime, static_cast(mMarqueeTime), + scrollLength + returnLength)); if (mMarqueeOffset > (scrollLength - (limit - returnLength))) mMarqueeOffset2 = static_cast(mMarqueeOffset - (scrollLength + returnLength)); diff --git a/es-core/src/math/Misc.cpp b/es-core/src/math/Misc.cpp deleted file mode 100644 index 6e14e0e10..000000000 --- a/es-core/src/math/Misc.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// SPDX-License-Identifier: MIT -// -// EmulationStation Desktop Edition -// Misc.cpp -// -// Miscellaneous math functions. -// - -#include "math/Misc.h" - -namespace Math -{ - float lerp(const float _start, const float _end, const float _fraction) - { - return (_start + ((_end - _start) * clamp(_fraction, 0.0f, 1.0f))); - } - - float smoothStep(const float _left, const float _right, const float _x) - { - const float x = clamp((_x - _left) / (_right - _left), 0.0f, 1.0f); - return x * x * (3 - (2 * x)); - } - - float smootherStep(const float _left, const float _right, const float _x) - { - const float x = clamp((_x - _left) / (_right - _left), 0.0f, 1.0f); - return x * x * x * (x * ((x * 6) - 15) + 10); - } - - namespace Scroll - { - float bounce(const float _delayTime, - const float _scrollTime, - const float _currentTime, - const float _scrollLength) - { - if (_currentTime < _delayTime) { - // Wait. - return 0; - } - else if (_currentTime < (_delayTime + _scrollTime)) { - // Lerp from 0 to scrollLength. - const float fraction = (_currentTime - _delayTime) / _scrollTime; - return lerp(0.0f, _scrollLength, smootherStep(0, 1, fraction)); - } - else if (_currentTime < (_delayTime + _scrollTime + _delayTime)) { - // Wait some more. - return _scrollLength; - } - else if (_currentTime < (_delayTime + _scrollTime + _delayTime + _scrollTime)) { - // Lerp back from scrollLength to 0. - const float fraction = - (_currentTime - _delayTime - _scrollTime - _delayTime) / _scrollTime; - return lerp(_scrollLength, 0.0f, smootherStep(0, 1, fraction)); - } - // And back to waiting. - return 0; - } - - float loop(const float _delayTime, - const float _scrollTime, - const float _currentTime, - const float _scrollLength) - { - if (_currentTime < _delayTime) { - // Wait. - return 0; - } - else if (_currentTime < (_delayTime + _scrollTime)) { - // Lerp from 0 to scrollLength. - const float fraction = (_currentTime - _delayTime) / _scrollTime; - return lerp(0.0f, _scrollLength, fraction); - } - - // And back to waiting. - return 0; - - } // Math::Scroll::loop - - } // namespace Scroll - -} // namespace Math diff --git a/es-core/src/math/Misc.h b/es-core/src/math/Misc.h deleted file mode 100644 index a9aedc398..000000000 --- a/es-core/src/math/Misc.h +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-License-Identifier: MIT -// -// EmulationStation Desktop Edition -// Misc.h -// -// Miscellaneous math functions. -// - -#ifndef ES_CORE_MATH_MISC_H -#define ES_CORE_MATH_MISC_H - -#include -#include -#include -#include - -#include - -namespace Math -{ - // When moving to the C++20 standard these functions are no longer required. - template T const& clamp(const T& _num, const T& _min, const T& _max) - { - return std::max(std::min(_num, _max), _min); - } - float lerp(const float _start, const float _end, const float _fraction); - - float smoothStep(const float _left, const float _right, const float _x); - float smootherStep(const float _left, const float _right, const float _x); - - namespace Scroll - { - float bounce(const float _delayTime, - const float _scrollTime, - const float _currentTime, - const float _scrollLength); - float loop(const float _delayTime, - const float _scrollTime, - const float _currentTime, - const float _scrollLength); - } // namespace Scroll - -} // namespace Math - -#endif // ES_CORE_MATH_MISC_H diff --git a/es-core/src/renderers/Renderer.h b/es-core/src/renderers/Renderer.h index ec6aac4b4..f02531a35 100644 --- a/es-core/src/renderers/Renderer.h +++ b/es-core/src/renderers/Renderer.h @@ -11,7 +11,7 @@ #include "Log.h" #include "Shader_GL21.h" -#include "math/Misc.h" +#include "utils/MathUtil.h" #include #include diff --git a/es-core/src/renderers/Shader_GL21.h b/es-core/src/renderers/Shader_GL21.h index d7080ed7c..764a28af6 100644 --- a/es-core/src/renderers/Shader_GL21.h +++ b/es-core/src/renderers/Shader_GL21.h @@ -11,7 +11,7 @@ #define GL_GLEXT_PROTOTYPES -#include "math/Misc.h" +#include "utils/MathUtil.h" #if defined(_WIN64) #include diff --git a/es-core/src/resources/TextureResource.h b/es-core/src/resources/TextureResource.h index 12319d81d..f3f5ae34b 100644 --- a/es-core/src/resources/TextureResource.h +++ b/es-core/src/resources/TextureResource.h @@ -9,9 +9,9 @@ #ifndef ES_CORE_RESOURCES_TEXTURE_RESOURCE_H #define ES_CORE_RESOURCES_TEXTURE_RESOURCE_H -#include "math/Misc.h" #include "resources/ResourceManager.h" #include "resources/TextureDataManager.h" +#include "utils/MathUtil.h" #include #include diff --git a/es-core/src/utils/MathUtil.cpp b/es-core/src/utils/MathUtil.cpp new file mode 100644 index 000000000..b11b7eb62 --- /dev/null +++ b/es-core/src/utils/MathUtil.cpp @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: MIT +// +// EmulationStation Desktop Edition +// MathUtil.cpp +// +// Math utility functions. +// The GLM library headers are also included from here. +// + +#include "utils/MathUtil.h" + +namespace Utils +{ + namespace Math + { + float smoothStep(const float left, const float right, const float value) + { + const float x{glm::clamp((value - left) / (right - left), 0.0f, 1.0f)}; + return x * x * (3.0f - (2.0f * x)); + } + + float smootherStep(const float left, const float right, const float value) + { + const float x{glm::clamp((value - left) / (right - left), 0.0f, 1.0f)}; + return x * x * x * (x * ((x * 6.0f) - 15.0f) + 10.0f); + } + + namespace Scroll + { + float bounce(const float delayTime, + const float scrollTime, + const float currentTime, + const float scrollLength) + { + if (currentTime < delayTime) { + // Wait. + return 0.0f; + } + else if (currentTime < (delayTime + scrollTime)) { + // Interpolate from 0 to scrollLength. + const float fraction{(currentTime - delayTime) / scrollTime}; + return glm::mix(0.0f, scrollLength, smootherStep(0.0f, 1.0f, fraction)); + } + else if (currentTime < (delayTime + scrollTime + delayTime)) { + // Wait some more. + return scrollLength; + } + else if (currentTime < (delayTime + scrollTime + delayTime + scrollTime)) { + // Interpolate back from scrollLength to 0. + const float fraction{(currentTime - delayTime - scrollTime - delayTime) / + scrollTime}; + return glm::mix(scrollLength, 0.0f, smootherStep(0.0f, 1.0f, fraction)); + } + // And back to waiting. + return 0.0f; + } + + float loop(const float delayTime, + const float scrollTime, + const float currentTime, + const float scrollLength) + { + if (currentTime < delayTime) { + // Wait. + return 0.0f; + } + else if (currentTime < (delayTime + scrollTime)) { + // Interpolate from 0 to scrollLength. + const float fraction{(currentTime - delayTime) / scrollTime}; + return glm::mix(0.0f, scrollLength, fraction); + } + + // And back to waiting. + return 0.0f; + } + + } // namespace Scroll + + } // namespace Math + +} // namespace Utils diff --git a/es-core/src/utils/MathUtil.h b/es-core/src/utils/MathUtil.h new file mode 100644 index 000000000..5954e6b41 --- /dev/null +++ b/es-core/src/utils/MathUtil.h @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT +// +// EmulationStation Desktop Edition +// MathUtil.h +// +// Math utility functions. +// The GLM library headers are also included from here. +// + +#ifndef ES_CORE_UTILS_MATH_UTIL_H +#define ES_CORE_UTILS_MATH_UTIL_H + +#include +#include +#include + +namespace Utils +{ + namespace Math + { + float smoothStep(const float left, const float right, const float value); + float smootherStep(const float left, const float right, const float value); + + namespace Scroll + { + float bounce(const float delayTime, + const float scrollTime, + const float currentTime, + const float scrollLength); + float loop(const float delayTime, + const float scrollTime, + const float currentTime, + const float scrollLength); + } // namespace Scroll + + } // namespace Math + +} // namespace Utils + +#endif // ES_CORE_UTILS_MATH_UTIL_H