From 42c1c76f438498887b4646a7d42960f255c36c04 Mon Sep 17 00:00:00 2001 From: Sour Date: Sat, 15 Feb 2020 14:27:36 -0500 Subject: [PATCH] Refactoring: Port back some code changes from Mesen-S --- Core/Profiler.cpp | 2 +- Utilities/Timer.cpp | 79 +++++---------------------------------------- Utilities/Timer.h | 22 ++++++------- 3 files changed, 19 insertions(+), 84 deletions(-) diff --git a/Core/Profiler.cpp b/Core/Profiler.cpp index b2961dd6..20f7c686 100644 --- a/Core/Profiler.cpp +++ b/Core/Profiler.cpp @@ -7,7 +7,7 @@ #include "MemoryManager.h" #include "MemoryDumper.h" #include "DebuggerTypes.h" -#include "Cpu.h" +#include "CPU.h" static constexpr int32_t ResetFunctionIndex = -1; diff --git a/Utilities/Timer.cpp b/Utilities/Timer.cpp index 9a155e1a..323aae6e 100644 --- a/Utilities/Timer.cpp +++ b/Utilities/Timer.cpp @@ -1,69 +1,29 @@ #include "stdafx.h" #include "Timer.h" -#ifndef LIBRETRO - #include +#include -#ifdef _WIN32 -#include +using namespace std::chrono; -Timer::Timer() -{ - LARGE_INTEGER li; - if(!QueryPerformanceFrequency(&li)) { - throw; - } - - _frequency = double(li.QuadPart) / 1000.0; - - QueryPerformanceCounter(&li); - _start = li.QuadPart; -} - -void Timer::Reset() -{ - LARGE_INTEGER li; - QueryPerformanceCounter(&li); - _start = li.QuadPart; -} - -double Timer::GetElapsedMS() -{ - LARGE_INTEGER li; - QueryPerformanceCounter(&li); - return double(li.QuadPart - _start) / _frequency; -} - -#else -#include - -Timer::Timer() +Timer::Timer() { Reset(); } void Timer::Reset() { - timespec start; - clock_gettime(CLOCK_MONOTONIC, &start); - - _start = start.tv_sec * 1000000000 + start.tv_nsec; + _start = high_resolution_clock::now(); } double Timer::GetElapsedMS() { - timespec end; - clock_gettime(CLOCK_MONOTONIC, &end); - - uint64_t currentTime = end.tv_sec * 1000000000 + end.tv_nsec; - - return (double)(currentTime - _start) / 1000000.0; + high_resolution_clock::time_point end = high_resolution_clock::now(); + duration span = duration_cast>(end - _start); + return span.count() * 1000.0; } -#endif - -void Timer::WaitUntil(double targetMillisecond) +void Timer::WaitUntil(double targetMillisecond) { if(targetMillisecond > 0) { double elapsedTime = GetElapsedMS(); @@ -72,26 +32,3 @@ void Timer::WaitUntil(double targetMillisecond) } } } - -#else - -//This is not used by Libretro port, remove its dependencies - -Timer::Timer() -{ -} - -void Timer::Reset() -{ -} - -double Timer::GetElapsedMS() -{ - return 0.0; -} - -void Timer::WaitUntil(double targetMillisecond) -{ -} - -#endif \ No newline at end of file diff --git a/Utilities/Timer.h b/Utilities/Timer.h index 5ed9fdda..f2f0f0c2 100644 --- a/Utilities/Timer.h +++ b/Utilities/Timer.h @@ -1,18 +1,16 @@ #pragma once #include "stdafx.h" +#include +using namespace std::chrono; class Timer { - private: -#ifndef LIBRETRO - #ifdef _WIN32 - double _frequency = 0.0; - #endif - uint64_t _start; -#endif - public: - Timer(); - void Reset(); - double GetElapsedMS(); - void WaitUntil(double targetMillisecond); +private: + high_resolution_clock::time_point _start; + +public: + Timer(); + void Reset(); + double GetElapsedMS(); + void WaitUntil(double targetMillisecond); }; \ No newline at end of file