Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
CCIGAMES committed Mar 2, 2024
1 parent ad52cbc commit 79afddc
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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 <chrono>


class HighResolutionTimer
{
public:
void reset();
void start();

inline bool isRunning() const { return mRunning; }
double getSecondsSinceStart() const;

protected:
typedef std::chrono::time_point<std::chrono::high_resolution_clock> TimePoint;
typedef std::chrono::duration<double> 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;
};
Original file line number Diff line number Diff line change
@@ -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<uint8> 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());
}
Original file line number Diff line number Diff line change
@@ -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 <rmxbase.h>


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) {}
};

0 comments on commit 79afddc

Please sign in to comment.