From 9260f05025174d8e17edf0de300aa8260e7b1324 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Mon, 22 Feb 2021 21:52:11 +0100 Subject: [PATCH] Implement TickCount --- CMakeLists.txt | 1 - src/Pomme.cpp | 2 -- src/PommeTime.h | 6 ------ src/Text/TextUtilities.cpp | 1 - src/Time/TimeManager.cpp | 39 +++++++++++++++++++------------------- 5 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 src/PommeTime.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2db9926..bb9e241 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,6 @@ set(POMME_SOURCES ${POMME_SRCDIR}/PommeInit.h ${POMME_SRCDIR}/PommeInput.h ${POMME_SRCDIR}/PommeSound.h - ${POMME_SRCDIR}/PommeTime.h ${POMME_SRCDIR}/PommeTypes.h ${POMME_SRCDIR}/PommeVideo.h ${POMME_SRCDIR}/Files/Files.cpp diff --git a/src/Pomme.cpp b/src/Pomme.cpp index 7168bf4..1e5b29e 100644 --- a/src/Pomme.cpp +++ b/src/Pomme.cpp @@ -3,7 +3,6 @@ #include "Pomme.h" #include "PommeInit.h" -#include "PommeTime.h" #include "PommeFiles.h" #include "PommeGraphics.h" #include "PommeSound.h" @@ -68,7 +67,6 @@ void ShowCursor() void Pomme::Init() { - Pomme::Time::Init(); Pomme::Files::Init(); Pomme::Graphics::Init(); Pomme::Sound::Init(); diff --git a/src/PommeTime.h b/src/PommeTime.h deleted file mode 100644 index 4ccd08e..0000000 --- a/src/PommeTime.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -namespace Pomme::Time -{ - void Init(); -} diff --git a/src/Text/TextUtilities.cpp b/src/Text/TextUtilities.cpp index 298705f..61a1db2 100644 --- a/src/Text/TextUtilities.cpp +++ b/src/Text/TextUtilities.cpp @@ -1,5 +1,4 @@ #include "Pomme.h" -#include "PommeTime.h" #include "PommeTypes.h" #include "PommeDebug.h" diff --git a/src/Time/TimeManager.cpp b/src/Time/TimeManager.cpp index 68be19e..8b078e2 100644 --- a/src/Time/TimeManager.cpp +++ b/src/Time/TimeManager.cpp @@ -1,42 +1,41 @@ #include "Pomme.h" -#include "PommeTime.h" #include "PommeTypes.h" -#include "PommeDebug.h" #include -#include #include -#include -std::chrono::time_point bootTP; +using namespace std::chrono; -// timestamp (from unix epoch) of the mac epoch, Jan 1, 1904, 00:00:00 -constexpr int JANUARY_1_1904 = -2'082'844'800; +// System time point on application start +static const time_point gBootTimePoint = high_resolution_clock::now(); + +// Timestamp of the Mac epoch (Jan 1, 1904, 00:00:00), relative to UNIX epoch (Jan 1, 1970, 00:00:00) +static constexpr int JANUARY_1_1904 = -2'082'844'800; + +static int64_t GetMicrosecondsSinceBoot() +{ + auto now = high_resolution_clock::now(); + microseconds usecs = duration_cast(now - gBootTimePoint); + return usecs.count(); +} //----------------------------------------------------------------------------- // Time Manager -void Pomme::Time::Init() -{ - bootTP = std::chrono::high_resolution_clock::now(); -} - void GetDateTime(unsigned long* secs) { *secs = (unsigned long) (std::time(nullptr) + JANUARY_1_1904); } -void Microseconds(UnsignedWide* usecs) +void Microseconds(UnsignedWide* usecsOut) { - auto now = std::chrono::high_resolution_clock::now(); - auto usecs1 = std::chrono::duration_cast(now - bootTP); - auto usecs2 = usecs1.count(); - usecs->lo = usecs2 & 0xFFFFFFFFL; - usecs->hi = (usecs2 >> 32) & 0xFFFFFFFFL; + auto usecs = GetMicrosecondsSinceBoot(); + usecsOut->lo = (usecs ) & 0xFFFFFFFFL; + usecsOut->hi = (usecs >> 32) & 0xFFFFFFFFL; } UInt32 TickCount() { - TODO(); - return 0; + // A tick is approximately 1/60 of a second + return (UInt32) (60L * GetMicrosecondsSinceBoot() / 1'000'000L); }