00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef TG_TIMER_H
00019 #define TG_TIMER_H
00020
00021 #include "MathUtil.h"
00022
00023 #include <SDL.h>
00024
00025 namespace tagGame
00026 {
00027 class Timer
00028 {
00029 public:
00030
00031 static int const ticksPerSec = 1000;
00032
00034 inline static Timer& theTimer();
00035
00037 inline void start();
00038
00040 inline void stop();
00041
00043 inline bool isStopped() const;
00044
00049 inline int gameTicks() const;
00050
00052 inline Real gameTime() const;
00053
00058 inline static int ticks();
00059
00061 inline static Real time();
00062
00063 protected:
00064 private:
00066 inline Timer();
00067 inline ~Timer();
00068
00070 Timer(Timer const& timer);
00071
00073 Timer& operator=(Timer const& timer);
00074
00075 int startTime;
00076 int baseTime;
00077 bool isRunning;
00078 };
00079
00080 Timer::Timer() :
00081 startTime(0),
00082 baseTime(0),
00083 isRunning(false)
00084 {
00085 }
00086
00087 Timer::~Timer()
00088 {
00089 }
00090
00091 Timer& Timer::theTimer()
00092 {
00093 static Timer theTimer;
00094
00095 return theTimer;
00096 }
00097
00098 void Timer::start()
00099 {
00100 if (!isRunning)
00101 {
00102 startTime = ticks();
00103 isRunning = true;
00104 }
00105 }
00106
00107 void Timer::stop()
00108 {
00109 if (isRunning)
00110 {
00111 baseTime = baseTime + (ticks() - startTime);
00112 isRunning = false;
00113 }
00114 }
00115
00116 bool Timer::isStopped() const
00117 {
00118 return !isRunning;
00119 }
00120
00121 int Timer::gameTicks() const
00122 {
00123 if (isRunning)
00124 {
00125 return baseTime + (ticks() - startTime);
00126 }
00127
00128 return baseTime;
00129 }
00130
00131 Real Timer::gameTime() const
00132 {
00133 return Real(gameTicks())/Real(ticksPerSec);
00134 }
00135
00136
00137
00138
00139 int Timer::ticks()
00140 {
00141 return SDL_GetTicks();
00142 }
00143
00144 Real Timer::time()
00145 {
00146 return Real(ticks())/Real(ticksPerSec);
00147 }
00148 }
00149
00150 #endif