From 4b1ed7da1e954a9c9b73c109224de625423086a1 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Apr 2021 00:12:34 +0200 Subject: [PATCH] Added Time class --- examples/main.cpp | 3 ++ include/SDLU.hpp | 1 + include/structures/Time.hpp | 44 ++++++++++++++++++++++++++ src/structures/Time.cpp | 62 +++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 include/structures/Time.hpp create mode 100644 src/structures/Time.cpp diff --git a/examples/main.cpp b/examples/main.cpp index dd579da..9ff0394 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -9,6 +9,9 @@ int main(int argc, char** argv) { sdlu::Initialize(); + sdlu::Time test = sdlu::Microseconds(420); + std::cout << test.AsSeconds() << std::endl; + Uint32* icon_data = new Uint32[64 * 64]; for (int y = 0; y < 64; y++) { diff --git a/include/SDLU.hpp b/include/SDLU.hpp index bee07e3..e9ef1db 100644 --- a/include/SDLU.hpp +++ b/include/SDLU.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace sdlu { // TODO: Eventually we should initialize things once the object gets created diff --git a/include/structures/Time.hpp b/include/structures/Time.hpp new file mode 100644 index 0000000..e7ea9ef --- /dev/null +++ b/include/structures/Time.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +#include "Util.hpp" + +SDLU_BEGIN + +class Time { +public: + Time(); + + template> Rep AsValue() const; + float AsSeconds() const; + Int32 AsMilliseconds() const; + Int64 AsMicroseconds() const; + + template Duration AsChrono() const; + std::chrono::seconds AsChronoSeconds() const; + std::chrono::milliseconds AsChronoMilliseconds() const; + std::chrono::microseconds AsChronoMicroseconds() const; + + friend Time Seconds(float seconds); + friend Time Milliseconds(Int32 milliseconds); + friend Time Microseconds(Int64 microseconds); + +private: + std::chrono::duration microseconds; +}; + + +template +inline Rep Time::AsValue() const +{ + return std::chrono::duration_cast>(microseconds).count(); +} + +template +inline Duration Time::AsChrono() const +{ + return std::chrono::duration_cast(microseconds); +} + +SDLU_END \ No newline at end of file diff --git a/src/structures/Time.cpp b/src/structures/Time.cpp new file mode 100644 index 0000000..f20d88e --- /dev/null +++ b/src/structures/Time.cpp @@ -0,0 +1,62 @@ +#include "structures/Time.hpp" +#include "..\..\include\structures\Time.hpp" + +SDLU_BEGIN + +Time::Time() : + microseconds(0) +{ +} + +float Time::AsSeconds() const +{ + return AsValue(); +} + +Int32 Time::AsMilliseconds() const +{ + return AsValue(); +} + +Int64 Time::AsMicroseconds() const +{ + return AsValue(); +} + +std::chrono::seconds Time::AsChronoSeconds() const +{ + return AsChrono(); +} + +std::chrono::milliseconds Time::AsChronoMilliseconds() const +{ + return AsChrono(); +} + +std::chrono::microseconds Time::AsChronoMicroseconds() const +{ + return AsChrono(); +} + +Time Seconds(float seconds) +{ + Time newTime; + newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(seconds)); + return newTime; +} + +Time Milliseconds(Int32 milliseconds) +{ + Time newTime; + newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(milliseconds)); + return newTime; +} + +Time Microseconds(Int64 microseconds) +{ + Time newTime; + newTime.microseconds = std::chrono::duration(microseconds); + return newTime; +} + +SDLU_END \ No newline at end of file