Added Clock
This commit is contained in:
parent
cf859223ab
commit
a3726d0887
|
@ -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;
|
Uint64 diff = 1;
|
||||||
|
|
||||||
MyWindow window(800, 800, "Test");
|
MyWindow window(800, 800, "Test");
|
||||||
|
@ -65,8 +65,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
window.Display();
|
window.Display();
|
||||||
|
|
||||||
diff = std::chrono::duration_cast<std::chrono::microseconds>
|
diff = timer.Restart().AsMicroseconds();
|
||||||
(std::chrono::steady_clock::now() - start).count();
|
|
||||||
title += (std::to_string(1000000 / diff) + " FPS | Mouse: ");
|
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::Left)) ? "L " : "l ";
|
||||||
title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Middle)) ? "M " : "m ";
|
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";
|
title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::XButton2)) ? "X2" : "x2";
|
||||||
|
|
||||||
window.SetTitle(title);
|
window.SetTitle(title);
|
||||||
start = std::chrono::steady_clock::now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sdlu::Quit();
|
sdlu::Quit();
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include <graphics/Graphics.hpp>
|
#include <graphics/Graphics.hpp>
|
||||||
#include <structures/Mouse.hpp>
|
#include <structures/Mouse.hpp>
|
||||||
#include <structures/Cursor.hpp>
|
#include <structures/Cursor.hpp>
|
||||||
#include <structures/Time.hpp>
|
#include <structures/Clock.hpp>
|
||||||
|
|
||||||
namespace sdlu {
|
namespace sdlu {
|
||||||
// TODO: Eventually we should initialize things once the object gets created
|
// TODO: Eventually we should initialize things once the object gets created
|
||||||
|
|
20
include/structures/Clock.hpp
Normal file
20
include/structures/Clock.hpp
Normal file
|
@ -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
|
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
SDLU_BEGIN
|
SDLU_BEGIN
|
||||||
|
|
||||||
|
typedef Int64 TimeRep;
|
||||||
|
typedef std::nano TimePeriod;
|
||||||
|
|
||||||
class Time {
|
class Time {
|
||||||
public:
|
public:
|
||||||
Time();
|
Time();
|
||||||
|
@ -20,8 +23,9 @@ public:
|
||||||
std::chrono::milliseconds AsChronoMilliseconds() const;
|
std::chrono::milliseconds AsChronoMilliseconds() const;
|
||||||
std::chrono::microseconds AsChronoMicroseconds() const;
|
std::chrono::microseconds AsChronoMicroseconds() const;
|
||||||
|
|
||||||
template<typename Rep, typename Period> friend Time Create(const Rep& duration);
|
template<typename Rep = Int64, typename Period = std::ratio<1>> static Time Create(const Rep& duration);
|
||||||
template<typename Rep, typename Period> friend Time Create(const std::chrono::duration<Rep, Period>& duration);
|
template<typename Rep = Int64, typename Period = std::ratio<1>> static Time Create(const std::chrono::duration<Rep, Period>& duration);
|
||||||
|
template<typename Rep = Int64, typename Period = std::ratio<1>> static Time Now();
|
||||||
friend Time Seconds(float seconds);
|
friend Time Seconds(float seconds);
|
||||||
friend Time Milliseconds(Int32 milliseconds);
|
friend Time Milliseconds(Int32 milliseconds);
|
||||||
friend Time Microseconds(Int64 microseconds);
|
friend Time Microseconds(Int64 microseconds);
|
||||||
|
@ -58,7 +62,7 @@ public:
|
||||||
friend Time& operator%=(Time& left, const Time& right);
|
friend Time& operator%=(Time& left, const Time& right);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::duration<Int64, std::micro> microseconds;
|
std::chrono::duration<TimeRep, TimePeriod> microseconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,20 +78,26 @@ inline std::chrono::duration<Rep, Period> Time::AsChrono() const
|
||||||
return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(microseconds);
|
return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(microseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Rep = Int64, typename Period = std::ratio<1>>
|
template<typename Rep, typename Period>
|
||||||
inline Time Create(const Rep& duration)
|
inline Time Time::Create(const Rep& duration)
|
||||||
{
|
{
|
||||||
Time newTime;
|
Time newTime;
|
||||||
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<Int64, std::micro>>(std::chrono::duration<Rep, Period>(duration));
|
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<TimeRep, TimePeriod>>(std::chrono::duration<Rep, Period>(duration));
|
||||||
return newTime;
|
return newTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Rep = Int64, typename Period = std::ratio<1>>
|
template<typename Rep, typename Period>
|
||||||
inline Time Create(const std::chrono::duration<Rep, Period>& duration)
|
inline Time Time::Create(const std::chrono::duration<Rep, Period>& duration)
|
||||||
{
|
{
|
||||||
Time newTime;
|
Time newTime;
|
||||||
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<Int64, std::micro>>(duration);
|
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<TimeRep, TimePeriod>>(duration);
|
||||||
return newTime;
|
return newTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Rep, typename Period>
|
||||||
|
inline Time Time::Now()
|
||||||
|
{
|
||||||
|
return Create(std::chrono::steady_clock::now().time_since_epoch());
|
||||||
|
}
|
||||||
|
|
||||||
SDLU_END
|
SDLU_END
|
21
src/structures/Clock.cpp
Normal file
21
src/structures/Clock.cpp
Normal file
|
@ -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
|
|
@ -40,17 +40,17 @@ std::chrono::microseconds Time::AsChronoMicroseconds() const
|
||||||
|
|
||||||
Time Seconds(float seconds)
|
Time Seconds(float seconds)
|
||||||
{
|
{
|
||||||
return Create<float>(seconds);
|
return Time::Create<float>(seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time Milliseconds(Int32 milliseconds)
|
Time Milliseconds(Int32 milliseconds)
|
||||||
{
|
{
|
||||||
return Create<Int32, std::milli>(milliseconds);
|
return Time::Create<Int32, std::milli>(milliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time Microseconds(Int64 microseconds)
|
Time Microseconds(Int64 microseconds)
|
||||||
{
|
{
|
||||||
return Create<Int64, std::micro>(microseconds);
|
return Time::Create<Int64, std::micro>(microseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Time& left, const Time& right)
|
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)
|
Time operator-(const Time& right)
|
||||||
{
|
{
|
||||||
return Create(-right.microseconds);
|
return Time::Create(-right.microseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time 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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
Time operator*(float left, const Time& right)
|
||||||
|
@ -144,12 +144,12 @@ Time& operator*=(Time& left, Int64 right)
|
||||||
|
|
||||||
Time operator/(const Time& left, float 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)
|
Time operator/(const Time& left, Int64 right)
|
||||||
{
|
{
|
||||||
return Create(left.microseconds / right);
|
return Time::Create(left.microseconds / right);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time& operator/=(Time& left, float 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)
|
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)
|
Time& operator%=(Time& left, const Time& right)
|
||||||
|
|
Loading…
Reference in a new issue