From e14b2f145ee43a652d71f53e3f645c58bb7b8e6f Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 29 Jan 2011 19:47:44 +0100 Subject: Added Clock and Timer. --- time.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ time.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 time.cpp create mode 100644 time.h diff --git a/time.cpp b/time.cpp new file mode 100644 index 0000000..fb2137d --- /dev/null +++ b/time.cpp @@ -0,0 +1,41 @@ +#include "time.h" + +Clock::Clock() { + reset(); + running = false; +} + +void Clock::reset() { + base = 0; + clock.Reset(); +} + +void Clock::start() { + running = true; + clock.Reset(); +} + +void Clock::stop() { + running = false; + base += clock.GetElapsedTime(); +} + +float Clock::elapsed() const { + if(running) { + return base + clock.GetElapsedTime(); + } else { + return base; + } +} + +Timer::Timer(const Clock& clock_) : clock(clock_) { + reset(); +} + +void Timer::reset() { + starttime = clock.elapsed(); +} + +float Timer::elapsed() const { + return clock.elapsed() - starttime; +} diff --git a/time.h b/time.h new file mode 100644 index 0000000..e580f6c --- /dev/null +++ b/time.h @@ -0,0 +1,34 @@ +#ifndef TIME_H +#define TIME_H + +#include + +class Clock { + private: + sf::Clock clock; + bool running; + float base; + + public: + Clock(); + + void reset(); + void start(); + void stop(); + + float elapsed() const; +}; + +class Timer { + private: + const Clock& clock; + float starttime; + + public: + Timer(const Clock& clock_); + + void reset(); + float elapsed() const; +}; + +#endif -- cgit v1.2.3