diff --git a/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.cpp b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.cpp new file mode 100644 index 00000000..1e44f932 --- /dev/null +++ b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.cpp @@ -0,0 +1,81 @@ +/* +* Part of the Oxygen Engine / Sonic 3 A.I.R. software distribution. +* Copyright (C) 2017-2024 by Eukaryot +* +* Published under the GNU GPLv3 open source software license, see license.txt +* or https://www.gnu.org/licenses/gpl-3.0.en.html +*/ + +#include "oxygen/pch.h" +#include "oxygen/helper/HighResolutionTimer.h" + + +void HighResolutionTimer::reset() +{ + mRunning = false; +} + +void HighResolutionTimer::start() +{ + mStart = std::chrono::high_resolution_clock::now(); + mRunning = true; +} + +double HighResolutionTimer::getSecondsSinceStart() const +{ + if (mRunning) + { + const Duration duration = (std::chrono::high_resolution_clock::now() - mStart); + return duration.count(); + } + return 0.0; +} + + +void AccumulativeTimer::resetTiming() +{ + reset(); + mAccumulatedTime = Duration::zero(); +} + +void AccumulativeTimer::resumeTiming() +{ + if (!mRunning) + { + start(); + } +} + +void AccumulativeTimer::pauseTiming() +{ + if (mRunning) + { + mAccumulatedTime += (std::chrono::high_resolution_clock::now() - mStart); + mRunning = false; + } +} + +double AccumulativeTimer::getAccumulatedSeconds() const +{ + Duration totalDuration = mAccumulatedTime; + if (mRunning) + { + totalDuration += (std::chrono::high_resolution_clock::now() - mStart); + } + return totalDuration.count(); +} + +double AccumulativeTimer::getAccumulatedSecondsAndRestart() +{ + const TimePoint now = std::chrono::high_resolution_clock::now(); + Duration totalDuration = mAccumulatedTime; + if (mRunning) + { + totalDuration += (now - mStart); + } + + mStart = now; + mAccumulatedTime = Duration::zero(); + mRunning = true; + return totalDuration.count(); +} diff --git a/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.h b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.h new file mode 100644 index 00000000..983b87d1 --- /dev/null +++ b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/HighResolutionTimer.h @@ -0,0 +1,44 @@ +/* +* Part of the Oxygen Engine / Sonic 3 A.I.R. software distribution. +* Copyright (C) 2017-2024 by Eukaryot +* +* Published under the GNU GPLv3 open source software license, see license.txt +* or https://www.gnu.org/licenses/gpl-3.0.en.html +*/ + +#pragma once + +#include + + +class HighResolutionTimer +{ +public: + void reset(); + void start(); + + inline bool isRunning() const { return mRunning; } + double getSecondsSinceStart() const; + +protected: + typedef std::chrono::time_point TimePoint; + typedef std::chrono::duration Duration; + + TimePoint mStart; + bool mRunning = false; +}; + + +class AccumulativeTimer : protected HighResolutionTimer +{ +public: + void resetTiming(); + void resumeTiming(); + void pauseTiming(); + + double getAccumulatedSeconds() const; + double getAccumulatedSecondsAndRestart(); + +private: + Duration mAccumulatedTime; +}; diff --git a/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.cpp b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.cpp new file mode 100644 index 00000000..7acdf896 --- /dev/null +++ b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.cpp @@ -0,0 +1,35 @@ +/* +* Part of the Oxygen Engine / Sonic 3 A.I.R. software distribution. +* Copyright (C) 2017-2024 by Eukaryot +* +* Published under the GNU GPLv3 open source software license, see license.txt +* or https://www.gnu.org/licenses/gpl-3.0.en.html +*/ + +#include "oxygen/pch.h" +#include "oxygen/helper/JsonHelper.h" + + +Json::Value JsonHelper::loadFile(const std::wstring& filename) +{ + std::vector content; + if (FTX::FileSystem->readFile(filename, content)) + { + if (!content.empty()) // Silently ignore empty JSON files + { + std::string errors; + Json::Value result = loadFromMemory(content, &errors); + if (errors.empty()) + return result; + + RMX_ERROR("Error parsing JSON file '" << *WString(filename).toString() << "':\n" << errors, ); + } + } + return Json::Value(); +} + +bool JsonHelper::saveFile(const std::wstring& filename, const Json::Value& value) +{ + const String output(value.toStyledString()); + return FTX::FileSystem->saveFile(filename, *output, output.length()); +} diff --git a/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.h b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.h new file mode 100644 index 00000000..c80d724f --- /dev/null +++ b/sonic3air-main/Oxygen/oxygenengine/source/oxygen/helper/JsonHelper.h @@ -0,0 +1,22 @@ +/* +* Part of the Oxygen Engine / Sonic 3 A.I.R. software distribution. +* Copyright (C) 2017-2024 by Eukaryot +* +* Published under the GNU GPLv3 open source software license, see license.txt +* or https://www.gnu.org/licenses/gpl-3.0.en.html +*/ + +#pragma once + +#include + + +class JsonHelper : public rmx::JsonHelper +{ +public: + static Json::Value loadFile(const std::wstring& filename); + static bool saveFile(const std::wstring& filename, const Json::Value& value); + +public: + inline JsonHelper(const Json::Value& json) : rmx::JsonHelper(json) {} +};