diff --git a/engine.build.lua b/engine.build.lua index 95f5af9..a7920b3 100644 --- a/engine.build.lua +++ b/engine.build.lua @@ -34,7 +34,8 @@ local system_module = Module("source/System", { "MessageQueue.cpp", "Event.cpp", "EventListener.cpp", - "Log.cpp" + "Log.cpp", + "Stopwatch.cpp" }) local platform_common_module = Module("source/Platform", { diff --git a/include/Spectre/System/Stopwatch.h b/include/Spectre/System/Stopwatch.h new file mode 100644 index 0000000..2a5cc4e --- /dev/null +++ b/include/Spectre/System/Stopwatch.h @@ -0,0 +1,27 @@ + +#ifndef SPECTRE_SYSTEM_STOPWATCH_H +#define SPECTRE_SYSTEM_STOPWATCH_H + +#include + +namespace sp { + +class Stopwatch { +public : + Stopwatch(); + + // Restart the watch. also returns the elapsed time before + // the watch was restarted. + Time restart(); + + // Get the elapsed time since the watch was restarted. + Time elapsed() const; + +private : + + Time m_start; +}; + +} // namespace sp + +#endif /* SPECTRE_SYSTEM_STOPWATCH_H */ diff --git a/source/System/Stopwatch.cpp b/source/System/Stopwatch.cpp new file mode 100644 index 0000000..cee21e2 --- /dev/null +++ b/source/System/Stopwatch.cpp @@ -0,0 +1,25 @@ + +#include +#include + +namespace sp { + +Stopwatch::Stopwatch() +{ + m_start = Time::milliseconds(system::getMilliseconds()); +} + +Time Stopwatch::restart() +{ + Time now = Time::milliseconds(system::getMilliseconds()); + Time elapsed = now - m_start; + m_start = now; + return elapsed; +} + +Time Stopwatch::elapsed() const +{ + return Time::milliseconds(system::getMilliseconds()) - m_start; +} + +} // namespace sp