1
0
Fork 0

Adding Spectre/Math/Time

This commit is contained in:
Henrik Hautakoski 2020-12-13 17:29:29 +01:00
parent 4d69ff3a18
commit 6de6028ba4
3 changed files with 153 additions and 1 deletions

View file

@ -115,7 +115,8 @@ local math_module = Module("source/Math", {
"Color.cpp",
"Logarithm.cpp",
"Math.cpp",
"Transform.cpp"
"Transform.cpp",
"Time.cpp"
})
local game_module = Module("source", {

View file

@ -0,0 +1,50 @@
#ifndef SPECTRE_MATH_TIME_H
#define SPECTRE_MATH_TIME_H
namespace sp {
class Time {
public :
Time(long value = 0);
double seconds() const;
int milliseconds() const;
long microseconds() const;
// ----------------------------
// Helper construct functions
// ----------------------------
static Time seconds(double value);
static Time milliseconds(int value);
static Time microseconds(long value);
private :
// In microseconds.
long m_value;
};
// ----------------------------
// Compare
// ----------------------------
bool operator ==(const Time& a, const Time& b);
bool operator !=(const Time& a, const Time& b);
bool operator <(const Time& a, const Time& b);
bool operator <=(const Time& a, const Time& b);
bool operator >(const Time& a, const Time& b);
bool operator >=(const Time& a, const Time& b);
// ----------------------------
// Arithmetic
// ----------------------------
Time operator +(const Time& a, const Time& b);
Time& operator +=(Time& a, const Time& b);
Time operator -(const Time& a, const Time& b);
Time& operator -=(Time& a, const Time& b);
}; // namespace sp
#endif /* SPECTRE_MATH_TIME_H */

101
source/Math/Time.cpp Normal file
View file

@ -0,0 +1,101 @@
#include <cmath>
#include <Spectre/Math/Time.h>
namespace sp {
Time::Time(long value) :
m_value(value)
{
}
double Time::seconds() const
{
return ((double) m_value) / 1000000.0f;
}
int Time::milliseconds() const
{
return ((double) m_value) / 1000;
}
long Time::microseconds() const
{
return m_value;
}
// ----------------------------
// Helper construct functions
// ----------------------------
Time Time::seconds(double value)
{
return Time((long) std::round(value * 1000000.0f));
}
Time Time::milliseconds(int value)
{
return Time((long) (value * 1000));
}
Time Time::microseconds(long value)
{
return Time(value);
}
// ----------------------------
// Compare
// ----------------------------
bool operator ==(const Time& a, const Time& b)
{
return a.microseconds() == b.microseconds();
}
bool operator !=(const Time& a, const Time& b)
{
return a.microseconds() != b.microseconds();
}
bool operator <(const Time& a, const Time& b)
{
return a.microseconds() < b.microseconds();
}
bool operator <=(const Time& a, const Time& b)
{
return a.microseconds() <= b.microseconds();
}
bool operator >(const Time& a, const Time& b)
{
return a.microseconds() > b.microseconds();
}
bool operator >=(const Time& a, const Time& b)
{
return a.microseconds() >= b.microseconds();
}
// ----------------------------
// Arithmetic
// ----------------------------
Time operator +(const Time& a, const Time& b)
{
return Time(a.microseconds() + b.microseconds());
}
Time& operator +=(Time& a, const Time& b)
{
return a = a + b;
}
Time operator -(const Time& a, const Time& b)
{
return Time(a.microseconds() - b.microseconds());
}
Time& operator -=(Time& a, const Time& b)
{
return a = a - b;
}
} // namespace sp