diff --git a/examples/main.cpp b/examples/main.cpp index 9ff0394..5780733 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -23,7 +23,7 @@ int main(int argc, char** argv) } } - std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); + sdlu::Clock timer; Uint64 diff = 1; MyWindow window(800, 800, "Test"); @@ -65,8 +65,7 @@ int main(int argc, char** argv) window.Display(); - diff = std::chrono::duration_cast - (std::chrono::steady_clock::now() - start).count(); + diff = timer.Restart().AsMicroseconds(); title += (std::to_string(1000000 / diff) + " FPS | Mouse: "); title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Left)) ? "L " : "l "; title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Middle)) ? "M " : "m "; @@ -75,7 +74,6 @@ int main(int argc, char** argv) title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::XButton2)) ? "X2" : "x2"; window.SetTitle(title); - start = std::chrono::steady_clock::now(); } sdlu::Quit(); diff --git a/include/SDLU.hpp b/include/SDLU.hpp index e9ef1db..fc3d98c 100644 --- a/include/SDLU.hpp +++ b/include/SDLU.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include namespace sdlu { // TODO: Eventually we should initialize things once the object gets created diff --git a/include/structures/Clock.hpp b/include/structures/Clock.hpp new file mode 100644 index 0000000..20499f3 --- /dev/null +++ b/include/structures/Clock.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "Util.hpp" +#include "Time.hpp" + +SDLU_BEGIN + +class Clock +{ +public: + Clock(); + + Time GetElapsedTime(); + Time Restart(); + +private: + Time lastTime; +}; + +SDLU_END \ No newline at end of file diff --git a/include/structures/Time.hpp b/include/structures/Time.hpp index aaf5dfa..267e5ed 100644 --- a/include/structures/Time.hpp +++ b/include/structures/Time.hpp @@ -6,6 +6,9 @@ SDLU_BEGIN +typedef Int64 TimeRep; +typedef std::nano TimePeriod; + class Time { public: Time(); @@ -20,8 +23,9 @@ public: std::chrono::milliseconds AsChronoMilliseconds() const; std::chrono::microseconds AsChronoMicroseconds() const; - template friend Time Create(const Rep& duration); - template friend Time Create(const std::chrono::duration& duration); + template> static Time Create(const Rep& duration); + template> static Time Create(const std::chrono::duration& duration); + template> static Time Now(); friend Time Seconds(float seconds); friend Time Milliseconds(Int32 milliseconds); friend Time Microseconds(Int64 microseconds); @@ -58,7 +62,7 @@ public: friend Time& operator%=(Time& left, const Time& right); private: - std::chrono::duration microseconds; + std::chrono::duration microseconds; }; @@ -74,20 +78,26 @@ inline std::chrono::duration Time::AsChrono() const return std::chrono::duration_cast>(microseconds); } -template> -inline Time Create(const Rep& duration) +template +inline Time Time::Create(const Rep& duration) { Time newTime; - newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(duration)); + newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(duration)); return newTime; } -template> -inline Time Create(const std::chrono::duration& duration) +template +inline Time Time::Create(const std::chrono::duration& duration) { Time newTime; - newTime.microseconds = std::chrono::duration_cast>(duration); + newTime.microseconds = std::chrono::duration_cast>(duration); return newTime; } +template +inline Time Time::Now() +{ + return Create(std::chrono::steady_clock::now().time_since_epoch()); +} + SDLU_END \ No newline at end of file diff --git a/src/structures/Clock.cpp b/src/structures/Clock.cpp new file mode 100644 index 0000000..38b63f9 --- /dev/null +++ b/src/structures/Clock.cpp @@ -0,0 +1,21 @@ +#include "structures/Clock.hpp" + +SDLU_BEGIN + +Clock::Clock() +{ +} + +Time Clock::GetElapsedTime() +{ + return (Time::Now() - lastTime); +} + +Time Clock::Restart() +{ + Time elapsedTime = GetElapsedTime(); + lastTime = Time::Now(); + return elapsedTime; +} + +SDLU_END \ No newline at end of file diff --git a/src/structures/Time.cpp b/src/structures/Time.cpp index def608a..017ec67 100644 --- a/src/structures/Time.cpp +++ b/src/structures/Time.cpp @@ -40,17 +40,17 @@ std::chrono::microseconds Time::AsChronoMicroseconds() const Time Seconds(float seconds) { - return Create(seconds); + return Time::Create(seconds); } Time Milliseconds(Int32 milliseconds) { - return Create(milliseconds); + return Time::Create(milliseconds); } Time Microseconds(Int64 microseconds) { - return Create(microseconds); + return Time::Create(microseconds); } bool operator==(const Time& left, const Time& right) @@ -85,12 +85,12 @@ bool operator>=(const Time& left, const Time& right) Time operator-(const Time& right) { - return Create(-right.microseconds); + return Time::Create(-right.microseconds); } Time operator+(const Time& left, const Time& right) { - return Create(left.microseconds + right.microseconds); + return Time::Create(left.microseconds + right.microseconds); } Time& operator+=(Time& left, const Time& right) @@ -101,7 +101,7 @@ Time& operator+=(Time& left, const Time& right) Time operator-(const Time& left, const Time& right) { - return Create(left.microseconds - right.microseconds); + return Time::Create(left.microseconds - right.microseconds); } Time& operator-=(Time& left, const Time& right) @@ -112,12 +112,12 @@ Time& operator-=(Time& left, const Time& right) Time operator*(const Time& left, float right) { - return Create(left.microseconds * right); + return Time::Create(left.microseconds * right); } Time operator*(const Time& left, Int64 right) { - return Create(left.microseconds * right); + return Time::Create(left.microseconds * right); } Time operator*(float left, const Time& right) @@ -144,12 +144,12 @@ Time& operator*=(Time& left, Int64 right) Time operator/(const Time& left, float right) { - return Create(left.microseconds / right); + return Time::Create(left.microseconds / right); } Time operator/(const Time& left, Int64 right) { - return Create(left.microseconds / right); + return Time::Create(left.microseconds / right); } Time& operator/=(Time& left, float right) @@ -171,7 +171,7 @@ float operator/(const Time& left, const Time& right) Time operator%(const Time& left, const Time& right) { - return Create(left.microseconds % right.microseconds); + return Time::Create(left.microseconds % right.microseconds); } Time& operator%=(Time& left, const Time& right)