Added Time class

This commit is contained in:
Robert 2021-04-24 00:12:34 +02:00
parent 2c17d5d533
commit 4b1ed7da1e
4 changed files with 110 additions and 0 deletions

View file

@ -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++)
{

View file

@ -3,6 +3,7 @@
#include <graphics/Graphics.hpp>
#include <structures/Mouse.hpp>
#include <structures/Cursor.hpp>
#include <structures/Time.hpp>
namespace sdlu {
// TODO: Eventually we should initialize things once the object gets created

View file

@ -0,0 +1,44 @@
#pragma once
#include <chrono>
#include "Util.hpp"
SDLU_BEGIN
class Time {
public:
Time();
template<typename Rep = Int64, typename Period = std::ratio<1>> Rep AsValue() const;
float AsSeconds() const;
Int32 AsMilliseconds() const;
Int64 AsMicroseconds() const;
template<typename Duration = std::chrono::seconds> 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<Int64, std::micro> microseconds;
};
template<typename Rep, typename Period>
inline Rep Time::AsValue() const
{
return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(microseconds).count();
}
template<typename Duration>
inline Duration Time::AsChrono() const
{
return std::chrono::duration_cast<Duration>(microseconds);
}
SDLU_END

62
src/structures/Time.cpp Normal file
View file

@ -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<float>();
}
Int32 Time::AsMilliseconds() const
{
return AsValue<Int32, std::milli>();
}
Int64 Time::AsMicroseconds() const
{
return AsValue<Int64, std::micro>();
}
std::chrono::seconds Time::AsChronoSeconds() const
{
return AsChrono<std::chrono::seconds>();
}
std::chrono::milliseconds Time::AsChronoMilliseconds() const
{
return AsChrono<std::chrono::milliseconds>();
}
std::chrono::microseconds Time::AsChronoMicroseconds() const
{
return AsChrono<std::chrono::microseconds>();
}
Time Seconds(float seconds)
{
Time newTime;
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<Int64, std::micro>>(std::chrono::duration<float>(seconds));
return newTime;
}
Time Milliseconds(Int32 milliseconds)
{
Time newTime;
newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<Int64, std::micro>>(std::chrono::duration<Int32, std::milli>(milliseconds));
return newTime;
}
Time Microseconds(Int64 microseconds)
{
Time newTime;
newTime.microseconds = std::chrono::duration<Int64, std::micro>(microseconds);
return newTime;
}
SDLU_END