Moved the remaining math functions to a math utility namespace.

This commit is contained in:
Leon Styhre 2021-08-17 22:11:16 +02:00
parent 74e21e8c03
commit 12c853bc31
13 changed files with 134 additions and 145 deletions

View file

@ -9,7 +9,7 @@
#include "VolumeControl.h" #include "VolumeControl.h"
#include "Log.h" #include "Log.h"
#include "math/Misc.h" #include "utils/MathUtil.h"
#if defined(_WIN64) #if defined(_WIN64)
#include <cmath> #include <cmath>

View file

@ -10,7 +10,7 @@
#define ES_APP_ANIMATIONS_MOVE_CAMERA_ANIMATION_H #define ES_APP_ANIMATIONS_MOVE_CAMERA_ANIMATION_H
#include "animations/Animation.h" #include "animations/Animation.h"
#include "math/Misc.h" #include "utils/MathUtil.h"
class MoveCameraAnimation : public Animation class MoveCameraAnimation : public Animation
{ {

View file

@ -57,9 +57,6 @@ set(CORE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiMsgBox.h ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiMsgBox.h
${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.h ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.h
# Math
${CMAKE_CURRENT_SOURCE_DIR}/src/math/Misc.h
# Renderers # Renderers
${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.h ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.h
${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Shader_GL21.h ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Shader_GL21.h
@ -74,6 +71,7 @@ set(CORE_HEADERS
# Utils # Utils
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.h ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.h
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/FileSystemUtil.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/StringUtil.h
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/TimeUtil.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/GuiMsgBox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/guis/GuiTextEditPopup.cpp
# Math
${CMAKE_CURRENT_SOURCE_DIR}/src/math/Misc.cpp
# Renderer # Renderer
${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer_GL21.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/renderers/Renderer_GL21.cpp
@ -148,6 +143,7 @@ set(CORE_SOURCES
# Utils # Utils
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/CImgUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/FileSystemUtil.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/StringUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/TimeUtil.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/TimeUtil.cpp
) )

View file

@ -10,7 +10,7 @@
#ifndef ES_CORE_HELP_STYLE_H #ifndef ES_CORE_HELP_STYLE_H
#define ES_CORE_HELP_STYLE_H #define ES_CORE_HELP_STYLE_H
#include "math/Misc.h" #include "utils/MathUtil.h"
#include <memory> #include <memory>
#include <string> #include <string>

View file

@ -11,8 +11,8 @@
#ifndef ES_CORE_THEME_DATA_H #ifndef ES_CORE_THEME_DATA_H
#define ES_CORE_THEME_DATA_H #define ES_CORE_THEME_DATA_H
#include "math/Misc.h"
#include "utils/FileSystemUtil.h" #include "utils/FileSystemUtil.h"
#include "utils/MathUtil.h"
#include <deque> #include <deque>
#include <map> #include <map>

View file

@ -352,8 +352,7 @@ template <typename T> void TextListComponent<T>::update(int deltaTime)
const float limit = mSize.x - mHorizontalMargin * 2.0f; const float limit = mSize.x - mHorizontalMargin * 2.0f;
if (textLength > limit) { if (textLength > limit) {
// Loop. // Loop the text.
// Pixels per second (based on nes-mini font at 1920x1080 to produce a speed of 200).
const float speed = mFont->sizeText("ABCDEFGHIJKLMNOPQRSTUVWXYZ").x * 0.247f; const float speed = mFont->sizeText("ABCDEFGHIJKLMNOPQRSTUVWXYZ").x * 0.247f;
const float delay = 3000.0f; const float delay = 3000.0f;
const float scrollLength = textLength; const float scrollLength = textLength;
@ -366,8 +365,8 @@ template <typename T> void TextListComponent<T>::update(int deltaTime)
while (mMarqueeTime > maxTime) while (mMarqueeTime > maxTime)
mMarqueeTime -= maxTime; mMarqueeTime -= maxTime;
mMarqueeOffset = static_cast<int>(Math::Scroll::loop(delay, scrollTime + returnTime, mMarqueeOffset = static_cast<int>(Utils::Math::Scroll::loop(
static_cast<float>(mMarqueeTime), delay, scrollTime + returnTime, static_cast<float>(mMarqueeTime),
scrollLength + returnLength)); scrollLength + returnLength));
if (mMarqueeOffset > (scrollLength - (limit - returnLength))) if (mMarqueeOffset > (scrollLength - (limit - returnLength)))

View file

@ -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

View file

@ -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 <algorithm>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/trigonometric.hpp>
#include <glm/gtc/round.hpp>
namespace Math
{
// When moving to the C++20 standard these functions are no longer required.
template <typename T> 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

View file

@ -11,7 +11,7 @@
#include "Log.h" #include "Log.h"
#include "Shader_GL21.h" #include "Shader_GL21.h"
#include "math/Misc.h" #include "utils/MathUtil.h"
#include <string> #include <string>
#include <vector> #include <vector>

View file

@ -11,7 +11,7 @@
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include "math/Misc.h" #include "utils/MathUtil.h"
#if defined(_WIN64) #if defined(_WIN64)
#include <GL/glew.h> #include <GL/glew.h>

View file

@ -9,9 +9,9 @@
#ifndef ES_CORE_RESOURCES_TEXTURE_RESOURCE_H #ifndef ES_CORE_RESOURCES_TEXTURE_RESOURCE_H
#define ES_CORE_RESOURCES_TEXTURE_RESOURCE_H #define ES_CORE_RESOURCES_TEXTURE_RESOURCE_H
#include "math/Misc.h"
#include "resources/ResourceManager.h" #include "resources/ResourceManager.h"
#include "resources/TextureDataManager.h" #include "resources/TextureDataManager.h"
#include "utils/MathUtil.h"
#include <cmath> #include <cmath>
#include <set> #include <set>

View file

@ -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

View file

@ -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 <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/trigonometric.hpp>
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