diff --git a/RhythmGameUtilities/Scripts/Common.cs b/RhythmGameUtilities/Scripts/Common.cs index fdb88cf..6a04ddf 100644 --- a/RhythmGameUtilities/Scripts/Common.cs +++ b/RhythmGameUtilities/Scripts/Common.cs @@ -7,10 +7,10 @@ public static class CommonInternal { [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float Lerp(float a, float b, float t); + public static extern float LerpInternal(float a, float b, float t); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float InverseLerp(float a, float b, float v); + public static extern float InverseLerpInternal(float a, float b, float v); } @@ -25,7 +25,7 @@ public static class Common /// The value used for interpolation. public static float Lerp(float a, float b, float t) { - return CommonInternal.Lerp(a, b, t); + return CommonInternal.LerpInternal(a, b, t); } /// @@ -36,7 +36,7 @@ public static float Lerp(float a, float b, float t) /// The value in the middle. public static float InverseLerp(float a, float b, float v) { - return CommonInternal.InverseLerp(a, b, v); + return CommonInternal.InverseLerpInternal(a, b, v); } } diff --git a/RhythmGameUtilities/Scripts/Utilities.cs b/RhythmGameUtilities/Scripts/Utilities.cs index 940f47c..c596c41 100644 --- a/RhythmGameUtilities/Scripts/Utilities.cs +++ b/RhythmGameUtilities/Scripts/Utilities.cs @@ -8,20 +8,20 @@ internal static class UtilitiesInternal { [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float ConvertTickToPosition(int tick, int resolution); + public static extern float ConvertTickToPositionInternal(int tick, int resolution); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] public static extern int ConvertSecondsToTicksInternal(float seconds, int resolution, Tempo[] tempoChanges, int tempoChangesSize, TimeSignature[] timeSignatures, int timeSignaturesSize); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern bool IsOnTheBeat(int bpm, float currentTime, float delta); + public static extern bool IsOnTheBeatInternal(int bpm, float currentTime, float delta); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern int RoundUpToTheNearestMultiplier(int value, int multiplier); + public static extern int RoundUpToTheNearestMultiplierInternal(int value, int multiplier); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float CalculateAccuracyRatio(int position, int currentPosition, int delta); + public static extern float CalculateAccuracyRatioInternal(int position, int currentPosition, int delta); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr CalculateBeatBarsInternal(Tempo[] tempoChanges, int tempoChangesSize, int resolution, @@ -42,7 +42,7 @@ public static class Utilities /// The resolution of the song. public static float ConvertTickToPosition(int tick, int resolution) { - return UtilitiesInternal.ConvertTickToPosition(tick, resolution); + return UtilitiesInternal.ConvertTickToPositionInternal(tick, resolution); } /// @@ -67,7 +67,7 @@ public static int ConvertSecondsToTicks(float seconds, int resolution, Tempo[] t /// The plus/minus delta to test the current time against. public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f) { - return UtilitiesInternal.IsOnTheBeat(bpm, currentTime, delta); + return UtilitiesInternal.IsOnTheBeatInternal(bpm, currentTime, delta); } /// @@ -77,7 +77,7 @@ public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f) /// The multiplier to round using. public static int RoundUpToTheNearestMultiplier(int value, int multiplier) { - return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier); + return UtilitiesInternal.RoundUpToTheNearestMultiplierInternal(value, multiplier); } public static BeatBar[] CalculateBeatBars(Tempo[] tempoChanges, int resolution = 192, int ts = 4, @@ -140,7 +140,7 @@ public static BeatBar[] CalculateBeatBars(Tempo[] tempoChanges, int resolution = /// The plus/minus delta to test the current position against. public static float CalculateAccuracyRatio(int position, int currentPosition, int delta = 50) { - return UtilitiesInternal.CalculateAccuracyRatio(position, currentPosition, delta); + return UtilitiesInternal.CalculateAccuracyRatioInternal(position, currentPosition, delta); } } diff --git a/UnityPackage/Scripts/Common.cs b/UnityPackage/Scripts/Common.cs index fdb88cf..6a04ddf 100644 --- a/UnityPackage/Scripts/Common.cs +++ b/UnityPackage/Scripts/Common.cs @@ -7,10 +7,10 @@ public static class CommonInternal { [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float Lerp(float a, float b, float t); + public static extern float LerpInternal(float a, float b, float t); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float InverseLerp(float a, float b, float v); + public static extern float InverseLerpInternal(float a, float b, float v); } @@ -25,7 +25,7 @@ public static class Common /// The value used for interpolation. public static float Lerp(float a, float b, float t) { - return CommonInternal.Lerp(a, b, t); + return CommonInternal.LerpInternal(a, b, t); } /// @@ -36,7 +36,7 @@ public static float Lerp(float a, float b, float t) /// The value in the middle. public static float InverseLerp(float a, float b, float v) { - return CommonInternal.InverseLerp(a, b, v); + return CommonInternal.InverseLerpInternal(a, b, v); } } diff --git a/UnityPackage/Scripts/Utilities.cs b/UnityPackage/Scripts/Utilities.cs index 940f47c..c596c41 100644 --- a/UnityPackage/Scripts/Utilities.cs +++ b/UnityPackage/Scripts/Utilities.cs @@ -8,20 +8,20 @@ internal static class UtilitiesInternal { [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float ConvertTickToPosition(int tick, int resolution); + public static extern float ConvertTickToPositionInternal(int tick, int resolution); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] public static extern int ConvertSecondsToTicksInternal(float seconds, int resolution, Tempo[] tempoChanges, int tempoChangesSize, TimeSignature[] timeSignatures, int timeSignaturesSize); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern bool IsOnTheBeat(int bpm, float currentTime, float delta); + public static extern bool IsOnTheBeatInternal(int bpm, float currentTime, float delta); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern int RoundUpToTheNearestMultiplier(int value, int multiplier); + public static extern int RoundUpToTheNearestMultiplierInternal(int value, int multiplier); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] - public static extern float CalculateAccuracyRatio(int position, int currentPosition, int delta); + public static extern float CalculateAccuracyRatioInternal(int position, int currentPosition, int delta); [DllImport("libRhythmGameUtilities", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr CalculateBeatBarsInternal(Tempo[] tempoChanges, int tempoChangesSize, int resolution, @@ -42,7 +42,7 @@ public static class Utilities /// The resolution of the song. public static float ConvertTickToPosition(int tick, int resolution) { - return UtilitiesInternal.ConvertTickToPosition(tick, resolution); + return UtilitiesInternal.ConvertTickToPositionInternal(tick, resolution); } /// @@ -67,7 +67,7 @@ public static int ConvertSecondsToTicks(float seconds, int resolution, Tempo[] t /// The plus/minus delta to test the current time against. public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f) { - return UtilitiesInternal.IsOnTheBeat(bpm, currentTime, delta); + return UtilitiesInternal.IsOnTheBeatInternal(bpm, currentTime, delta); } /// @@ -77,7 +77,7 @@ public static bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f) /// The multiplier to round using. public static int RoundUpToTheNearestMultiplier(int value, int multiplier) { - return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier); + return UtilitiesInternal.RoundUpToTheNearestMultiplierInternal(value, multiplier); } public static BeatBar[] CalculateBeatBars(Tempo[] tempoChanges, int resolution = 192, int ts = 4, @@ -140,7 +140,7 @@ public static BeatBar[] CalculateBeatBars(Tempo[] tempoChanges, int resolution = /// The plus/minus delta to test the current position against. public static float CalculateAccuracyRatio(int position, int currentPosition, int delta = 50) { - return UtilitiesInternal.CalculateAccuracyRatio(position, currentPosition, delta); + return UtilitiesInternal.CalculateAccuracyRatioInternal(position, currentPosition, delta); } } diff --git a/include/RhythmGameUtilities/Common.hpp b/include/RhythmGameUtilities/Common.hpp index 584d7ad..73d5eee 100644 --- a/include/RhythmGameUtilities/Common.hpp +++ b/include/RhythmGameUtilities/Common.hpp @@ -51,7 +51,7 @@ extern "C" * @private */ -std::string Trim(const char *contents) +inline std::string Trim(const char *contents) { return std::regex_replace(contents, std::regex("^\\s+|\\s+$"), ""); } @@ -64,7 +64,8 @@ std::string Trim(const char *contents) * @private */ -std::vector Split(const char *contents, const char delimiter) +inline std::vector Split(const char *contents, + const char delimiter) { auto parts = std::vector(); @@ -80,8 +81,8 @@ std::vector Split(const char *contents, const char delimiter) return parts; } -std::vector FindAllMatches(const char *contents, - std::regex pattern) +inline std::vector FindAllMatches(const char *contents, + std::regex pattern) { auto currentMatch = std::cregex_iterator(contents, contents + strlen(contents), pattern); @@ -101,8 +102,8 @@ std::vector FindAllMatches(const char *contents, return matches; } -std::vector FindMatchGroups(const char *contents, - std::regex pattern) +inline std::vector FindMatchGroups(const char *contents, + std::regex pattern) { auto currentMatch = std::cregex_iterator(contents, contents + strlen(contents), pattern); diff --git a/include/RhythmGameUtilities/CommonInternal.hpp b/include/RhythmGameUtilities/CommonInternal.hpp new file mode 100644 index 0000000..7549a44 --- /dev/null +++ b/include/RhythmGameUtilities/CommonInternal.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "Common.hpp" + +#ifdef _WIN32 +#define PACKAGE_API __declspec(dllexport) +#else +#define PACKAGE_API +#endif + +namespace RhythmGameUtilities +{ + +extern "C" +{ + PACKAGE_API float LerpInternal(float a, float b, float t) + { + return Lerp(a, b, t); + } + + PACKAGE_API float InverseLerpInternal(float a, float b, float v) + { + return InverseLerp(a, b, v); + } +} + +} // namespace RhythmGameUtilities diff --git a/include/RhythmGameUtilities/Enums/Difficulty.h b/include/RhythmGameUtilities/Enums/Difficulty.h index 4a31d34..257b295 100644 --- a/include/RhythmGameUtilities/Enums/Difficulty.h +++ b/include/RhythmGameUtilities/Enums/Difficulty.h @@ -22,7 +22,7 @@ typedef enum Difficulty } DifficultyType; -std::string ToString(Difficulty difficulty) +inline std::string ToString(Difficulty difficulty) { switch (difficulty) { diff --git a/include/RhythmGameUtilities/Enums/NamedSection.h b/include/RhythmGameUtilities/Enums/NamedSection.h index bbdb213..2d0531a 100644 --- a/include/RhythmGameUtilities/Enums/NamedSection.h +++ b/include/RhythmGameUtilities/Enums/NamedSection.h @@ -17,7 +17,7 @@ typedef enum NamedSection Events } NamedSectionType; -std::string ToString(NamedSection namedSection) +inline std::string ToString(NamedSection namedSection) { switch (namedSection) { diff --git a/include/RhythmGameUtilities/Enums/TypeCode.h b/include/RhythmGameUtilities/Enums/TypeCode.h index 0eaa087..304b370 100644 --- a/include/RhythmGameUtilities/Enums/TypeCode.h +++ b/include/RhythmGameUtilities/Enums/TypeCode.h @@ -22,7 +22,7 @@ typedef enum TypeCode } TypeCodeType; -std::string ToString(TypeCode typeCode) +inline std::string ToString(TypeCode typeCode) { switch (typeCode) { diff --git a/include/RhythmGameUtilities/File.hpp b/include/RhythmGameUtilities/File.hpp index c7e1278..5a5f6cc 100644 --- a/include/RhythmGameUtilities/File.hpp +++ b/include/RhythmGameUtilities/File.hpp @@ -16,7 +16,7 @@ namespace RhythmGameUtilities * @public */ -std::vector ReadBytesFromFile(const char *path) +inline std::vector ReadBytesFromFile(const char *path) { std::ifstream file(path, std::ios::binary | std::ios::ate); @@ -43,7 +43,7 @@ std::vector ReadBytesFromFile(const char *path) * @public */ -std::string ReadStringFromFile(const char *path) +inline std::string ReadStringFromFile(const char *path) { std::ifstream file(path); diff --git a/include/RhythmGameUtilities/Parsers.hpp b/include/RhythmGameUtilities/Parsers.hpp index 74d0df9..d9d6d1f 100644 --- a/include/RhythmGameUtilities/Parsers.hpp +++ b/include/RhythmGameUtilities/Parsers.hpp @@ -15,12 +15,6 @@ #include "Common.hpp" -#ifdef _WIN32 -#define PACKAGE_API __declspec(dllexport) -#else -#define PACKAGE_API -#endif - namespace RhythmGameUtilities { @@ -45,8 +39,8 @@ std::regex CHART_SECTION_LINE_PATTERN("([^=]+)\\s*=([^\\r\\n]+)"); std::regex JSON_VALUE_PATTERN("(\"[^\"]+\"|\\S+)"); -std::map>>> +inline std::map>>> ParseSectionsFromChart(const char *contents) { auto matches = FindAllMatches(contents, CHART_SECTION_PATTERN); @@ -93,7 +87,7 @@ ParseSectionsFromChart(const char *contents) return sections; } -std::map ParseMetaDataFromChartSection( +inline std::map ParseMetaDataFromChartSection( std::vector>> section) { auto data = std::map(); @@ -106,7 +100,7 @@ std::map ParseMetaDataFromChartSection( return data; } -std::vector ParseTimeSignatureChangesFromChartSection( +inline std::vector ParseTimeSignatureChangesFromChartSection( std::vector>> section) { auto timeSignaturesChanges = std::vector(); @@ -127,7 +121,7 @@ std::vector ParseTimeSignatureChangesFromChartSection( return timeSignaturesChanges; } -std::vector ParseTempoChangesFromChartSection( +inline std::vector ParseTempoChangesFromChartSection( std::vector>> section) { auto tempoChanges = std::vector(); @@ -146,7 +140,7 @@ std::vector ParseTempoChangesFromChartSection( return tempoChanges; } -std::vector ParseNotesFromChartSection( +inline std::vector ParseNotesFromChartSection( std::vector>> section) { auto notes = std::vector(); @@ -164,7 +158,7 @@ std::vector ParseNotesFromChartSection( return notes; } -std::map ParseLyricsFromChartSection( +inline std::map ParseLyricsFromChartSection( std::vector>> section) { auto lyrics = std::map(); diff --git a/include/RhythmGameUtilities/ParsersInternal.hpp b/include/RhythmGameUtilities/ParsersInternal.hpp index bba57c4..24580ad 100644 --- a/include/RhythmGameUtilities/ParsersInternal.hpp +++ b/include/RhythmGameUtilities/ParsersInternal.hpp @@ -15,9 +15,8 @@ namespace RhythmGameUtilities extern "C" { - PACKAGE_API - ChartSectionInternal *ParseSectionsFromChartInternal(const char *contents, - int *outSize) + PACKAGE_API ChartSectionInternal * + ParseSectionsFromChartInternal(const char *contents, int *outSize) { auto internalSections = ParseSectionsFromChart(contents); diff --git a/include/RhythmGameUtilities/RhythmGameUtilities.cpp b/include/RhythmGameUtilities/RhythmGameUtilities.cpp index ef3c4bd..fab6483 100644 --- a/include/RhythmGameUtilities/RhythmGameUtilities.cpp +++ b/include/RhythmGameUtilities/RhythmGameUtilities.cpp @@ -2,6 +2,7 @@ #include "Audio.hpp" #include "Common.hpp" +#include "CommonInternal.hpp" #include "Parsers.hpp" #include "ParsersInternal.hpp" #include "Utilities.hpp" diff --git a/include/RhythmGameUtilities/Utilities.hpp b/include/RhythmGameUtilities/Utilities.hpp index e8e3274..fa8ecfb 100644 --- a/include/RhythmGameUtilities/Utilities.hpp +++ b/include/RhythmGameUtilities/Utilities.hpp @@ -13,12 +13,6 @@ #include "Common.hpp" -#ifdef _WIN32 -#define PACKAGE_API __declspec(dllexport) -#else -#define PACKAGE_API -#endif - namespace RhythmGameUtilities { @@ -34,9 +28,10 @@ const float SECONDS_PER_MINUTE = 60.0f; * @public */ -int ConvertSecondsToTicks(float seconds, int resolution, - std::vector tempoChanges, - std::vector timeSignatureChanges) +inline int +ConvertSecondsToTicks(float seconds, int resolution, + std::vector tempoChanges, + std::vector timeSignatureChanges) { auto tempoChangesIterator = tempoChanges.begin(); auto timeSignatureIterator = timeSignatureChanges.begin(); @@ -99,7 +94,7 @@ int ConvertSecondsToTicks(float seconds, int resolution, * @public */ -std::vector> +inline std::vector> GenerateAdjacentKeyPairs(std::map keyValuePairs) { auto adjacentKeyPairs = std::vector>(); @@ -122,9 +117,9 @@ GenerateAdjacentKeyPairs(std::map keyValuePairs) return adjacentKeyPairs; } -std::vector CalculateBeatBars(std::vector tempoChanges, - int resolution, int ts, - bool includeHalfNotes) +inline std::vector CalculateBeatBars(std::vector tempoChanges, + int resolution, int ts, + bool includeHalfNotes) { std::vector beatBars; @@ -159,8 +154,8 @@ std::vector CalculateBeatBars(std::vector tempoChanges, return beatBars; } -std::optional FindPositionNearGivenTick(std::vector notes, int tick, - int delta = 50) +inline std::optional FindPositionNearGivenTick(std::vector notes, + int tick, int delta = 50) { auto left = 0; auto right = static_cast(notes.size()) - 1; @@ -188,77 +183,73 @@ std::optional FindPositionNearGivenTick(std::vector notes, int tick, return std::nullopt; } -extern "C" +/** + * Convert a tick to a 2D/3D position. + * + * @param tick The tick. + * @param resolution The resolution of the song. + * @public + */ + +inline float ConvertTickToPosition(int tick, int resolution) { - /** - * Convert a tick to a 2D/3D position. - * - * @param tick The tick. - * @param resolution The resolution of the song. - * @public - */ - - PACKAGE_API float ConvertTickToPosition(int tick, int resolution) - { - return tick / static_cast(resolution); - } + return tick / static_cast(resolution); +} - /** - * Checks to see if the current time of a game or audio file is on the beat. - * - * @param bpm The base BPM for a song. - * @param currentTime A timestamp to compare to the BPM. - * @param delta The plus/minus delta to test the current time against. - * @public - */ - - PACKAGE_API bool IsOnTheBeat(int bpm, float currentTime, - float delta = 0.05f) - { - auto beatInterval = SECONDS_PER_MINUTE / static_cast(bpm); +/** + * Checks to see if the current time of a game or audio file is on the beat. + * + * @param bpm The base BPM for a song. + * @param currentTime A timestamp to compare to the BPM. + * @param delta The plus/minus delta to test the current time against. + * @public + */ - auto beatFraction = currentTime / beatInterval; +inline bool IsOnTheBeat(int bpm, float currentTime, float delta = 0.05f) +{ + auto beatInterval = SECONDS_PER_MINUTE / static_cast(bpm); - auto difference = std::abs(beatFraction - std::round(beatFraction)); + auto beatFraction = currentTime / beatInterval; - auto result = difference < delta; + auto difference = std::abs(beatFraction - std::round(beatFraction)); - return result; - } + auto result = difference < delta; - /** - * Rounds a value up the nearest multiplier. - * - * @param value The value to round. - * @param multiplier The multiplier to round using. - * @public - */ + return result; +} - PACKAGE_API int RoundUpToTheNearestMultiplier(int value, int multiplier) - { - return static_cast( - std::ceil(static_cast(value) / multiplier) * multiplier); - } +/** + * Rounds a value up the nearest multiplier. + * + * @param value The value to round. + * @param multiplier The multiplier to round using. + * @public + */ - /** - * Calculated the accuracy ratio of the current position against a static - * position. - * - * @param position The position to test against. - * @param currentPosition The current position. - * @param delta The plus/minus delta to test the current position against. - * @public - */ - - PACKAGE_API float CalculateAccuracyRatio(int position, int currentPosition, - int delta = 50) - { - auto diff = position - currentPosition; +inline int RoundUpToTheNearestMultiplier(int value, int multiplier) +{ + return static_cast(std::ceil(static_cast(value) / multiplier) * + multiplier); +} - auto ratio = InverseLerp(delta, 0, std::abs(diff)); +/** + * Calculated the accuracy ratio of the current position against a static + * position. + * + * @param position The position to test against. + * @param currentPosition The current position. + * @param delta The plus/minus delta to test the current position against. + * @public + */ - return ratio; - } +inline float CalculateAccuracyRatio(int position, int currentPosition, + int delta = 50) +{ + auto diff = position - currentPosition; + + auto ratio = InverseLerp(delta, 0, std::abs(diff)); + + return ratio; } } // namespace RhythmGameUtilities diff --git a/include/RhythmGameUtilities/UtilitiesInternal.hpp b/include/RhythmGameUtilities/UtilitiesInternal.hpp index e45bba4..85d15d4 100644 --- a/include/RhythmGameUtilities/UtilitiesInternal.hpp +++ b/include/RhythmGameUtilities/UtilitiesInternal.hpp @@ -1,11 +1,5 @@ #pragma once -#include - -#include "Structs/BeatBar.h" -#include "Structs/Tempo.h" -#include "Structs/TimeSignature.h" - #include "Utilities.hpp" #ifdef _WIN32 @@ -71,6 +65,30 @@ extern "C" return beatBars; } + + PACKAGE_API float ConvertTickToPositionInternal(int tick, int resolution) + { + return ConvertTickToPosition(tick, resolution); + } + + PACKAGE_API bool IsOnTheBeatInternal(int bpm, float currentTime, + float delta = 0.05f) + { + return IsOnTheBeat(bpm, currentTime, delta); + } + + PACKAGE_API int RoundUpToTheNearestMultiplierInternal(int value, + int multiplier) + { + return RoundUpToTheNearestMultiplier(value, multiplier); + } + + PACKAGE_API float CalculateAccuracyRatioInternal(int position, + int currentPosition, + int delta = 50) + { + return CalculateAccuracyRatio(position, currentPosition, delta); + } } } // namespace RhythmGameUtilities diff --git a/tests/RhythmGameUtilities/CommonInternal.cpp b/tests/RhythmGameUtilities/CommonInternal.cpp new file mode 100644 index 0000000..ac9ffc1 --- /dev/null +++ b/tests/RhythmGameUtilities/CommonInternal.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "RhythmGameUtilities/CommonInternal.hpp" + +using namespace RhythmGameUtilities; + +void testInverseLerpInternal() +{ + auto value = InverseLerpInternal(0, 10, 5); + + assert(abs(0.5 - value) < 0.01); + + std::cout << "."; +} + +void testLerpInternal() +{ + auto value = LerpInternal(0, 10, 0.5f); + + assert(5 == value); + + std::cout << "."; +} + +int main() +{ + testInverseLerpInternal(); + testLerpInternal(); + + return 0; +} diff --git a/tests/RhythmGameUtilities/UtilitiesInternal.cpp b/tests/RhythmGameUtilities/UtilitiesInternal.cpp index 14bd9d3..35b6a58 100644 --- a/tests/RhythmGameUtilities/UtilitiesInternal.cpp +++ b/tests/RhythmGameUtilities/UtilitiesInternal.cpp @@ -10,6 +10,44 @@ using namespace RhythmGameUtilities; +void testCalculateAccuracyRatioInternal() +{ + const int seconds = 2; + const int resolution = 192; + const int positionDelta = 50; + + std::vector tempoChanges = {{0, 120000}}; + std::vector timeSignatureChanges = {{0, 4}}; + + auto note = new Note{750}; + auto currentPosition = ConvertSecondsToTicksInternal( + seconds, resolution, &tempoChanges[0], tempoChanges.size(), + &timeSignatureChanges[0], timeSignatureChanges.size()); + + auto value = CalculateAccuracyRatioInternal(note->Position, currentPosition, + positionDelta); + + assert(abs(0.64 - value) < 0.01); + + std::cout << "."; +} + +void testCalculateBeatBarsInternal() +{ + std::vector tempoChanges = { + {0, 88000}, {3840, 112000}, {9984, 89600}, {22272, 112000}, + {33792, 111500}, {34560, 112000}, {42240, 111980}}; + + int outSize; + + auto beatBars = CalculateBeatBarsInternal( + &tempoChanges[0], tempoChanges.size(), 192, 4, true, &outSize); + + assert(outSize == 440); + + std::cout << "."; +} + void testConvertSecondsToTicksInternal() { std::vector tempoChanges = { @@ -25,26 +63,48 @@ void testConvertSecondsToTicksInternal() std::cout << "."; } -void testCalculateBeatBarsInternal() +void testConvertTickToPositionInternal() { - std::vector tempoChanges = { - {0, 88000}, {3840, 112000}, {9984, 89600}, {22272, 112000}, - {33792, 111500}, {34560, 112000}, {42240, 111980}}; + const int tick = 1056; + const int resolution = 192; - int *outSize; + auto position = ConvertTickToPositionInternal(tick, resolution); - auto beatBars = CalculateBeatBarsInternal( - &tempoChanges[0], tempoChanges.size(), 192, 4, true, outSize); + assert(abs(5.5 - position) < 0.01); + + std::cout << "."; +} + +void testIsOnTheBeatInternal() +{ + const int bpm = 120; + const float currentTime = 10; + const float delta = 0.05f; + + auto isOnTheBeat = IsOnTheBeatInternal(bpm, currentTime, delta); + + assert(true == isOnTheBeat); + + std::cout << "."; +} + +void testRoundUpToTheNearestMultiplierInternal() +{ + auto value = RoundUpToTheNearestMultiplierInternal(12, 10); - assert(*outSize == 440); + assert(20 == value); std::cout << "."; } int main() { - testConvertSecondsToTicksInternal(); + testCalculateAccuracyRatioInternal(); testCalculateBeatBarsInternal(); + testConvertSecondsToTicksInternal(); + testConvertTickToPositionInternal(); + testIsOnTheBeatInternal(); + testRoundUpToTheNearestMultiplierInternal(); return 0; }