From bce16ed519add0d5398d504d2554395c43c74571 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 10 Aug 2012 20:13:48 +0200 Subject: [PATCH] added timer facility --- src/app/Application.cpp | 5 ++ src/app/Application.h | 3 ++ src/app/State.h | 10 ++++ src/app/Timer.h | 100 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/app/Timer.h diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 29eaee7..fc1d380 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -120,8 +120,12 @@ void Application::Run() { void Application::Loop() { Uint32 now(SDL_GetTicks()); Uint32 deltaT(now - last); + GlobalTimers().Update(deltaT); if (deltaT > 34) deltaT = 34; + if (CurrentState()) { + CurrentState()->GraphicsTimers().Update(deltaT); + } HandleEvents(); if (!StateChangePending()) { UpdateWorld(deltaT); @@ -161,6 +165,7 @@ void Application::HandleEvents() { void Application::UpdateWorld(Uint32 deltaT) { if (!CurrentState()) return; for (Uint32 i(0); i < deltaT; ++i) { + CurrentState()->PhysicsTimers().Update(0.001f); CurrentState()->UpdateWorld(0.001f); } } diff --git a/src/app/Application.h b/src/app/Application.h index 5e9e888..cf1261b 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -9,6 +9,7 @@ #define APP_APPLICATION_H_ #include "Input.h" +#include "Timer.h" #include "../sdl/InitScreen.h" #include @@ -40,6 +41,7 @@ public: void Quit(); Input &Buttons() { return input; } const Input &Buttons() const { return input; } + Timers &GlobalTimers() { return globalTimers; } private: struct StateCommand { @@ -67,6 +69,7 @@ private: sdl::InitScreen *screen; std::stack states; std::queue stateChanges; + Timers globalTimers; Input input; Uint32 last; diff --git a/src/app/State.h b/src/app/State.h index 678f2b9..b4c26d0 100644 --- a/src/app/State.h +++ b/src/app/State.h @@ -8,6 +8,8 @@ #ifndef APP_APPLICATIONSTATE_H_ #define APP_APPLICATIONSTATE_H_ +#include "Timer.h" + #include namespace app { @@ -40,6 +42,14 @@ public: virtual void UpdateWorld(float deltaT) = 0; virtual void Render(SDL_Surface *) = 0; +public: + Timers &GraphicsTimers() { return graphicsTimers; } + Timers &PhysicsTimers() { return physicsTimers; } + +private: + Timers graphicsTimers; + Timers physicsTimers; + }; } diff --git a/src/app/Timer.h b/src/app/Timer.h new file mode 100644 index 0000000..465ed64 --- /dev/null +++ b/src/app/Timer.h @@ -0,0 +1,100 @@ +/* + * Timer.h + * + * Created on: Aug 10, 2012 + * Author: holy + */ + +#ifndef APP_TIMER_H_ +#define APP_TIMER_H_ + +#include +#include + +namespace app { + +template +struct TimerData { + + TimerData() : time(0), target(0), refCount(0) { } + TimerData(Time target) : time(0), target(target), refCount(0) { } + + Time time; + Time target; + int refCount; + +}; + + +template +class Timer { + +public: + Timer() : data(0) { } + ~Timer() { if (data) --data->refCount; } + Timer(TimerData