source: nrEngine/include/Timer.h @ 23

Revision 23, 4.6 KB checked in by art, 13 years ago (diff)
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13
14#ifndef _NR_C_TIMER_H_
15#define _NR_C_TIMER_H_
16
17
18//----------------------------------------------------------------------------------
19// Includes
20//----------------------------------------------------------------------------------
21#include "Prerequisities.h"
22#include "Clock.h"
23#include "ITimeObserver.h"
24#include "Log.h"
25
26namespace nrEngine{
27
28        //! Timers are used to retrieve time. You can hav emore than one timer in your app
29        /**
30        * Timer is an standard timer shipped with the engine. You can
31        * create your own timers because they are specialized for your application.
32        * This timer can only ticks and provide user with information he/she need.
33        * Also this timer supports scaling and pausing, so you are able to use
34        * at lesser speed or hold on parts of your game using this timer.
35        *
36        * @note Because each timer is created from the clock, so the timer lifespan
37        * is the same as from the clock. So if clock is destroyed, timer will also
38        * be killed. You also can not destroy the timer object, only clock can do this.
39        *
40        * \ingroup time
41        **/
42        class _NRExport Timer: public ITimeObserver {
43        public:
44       
45                /**
46                * Define default values and set clock for using with this timer.
47                * This timer will automaticly be added to the clock as observer.
48                **/
49                Timer(Clock& clock);   
50               
51                /**
52                * Release used memory and remove this timer from observer list of
53                * the clock.
54                **/
55                virtual ~Timer();
56               
57                /**
58                * Returns current time since this timer is started
59                **/
60                float64 getTime() const { return _currentTime; }
61               
62                /**
63                * Returns the time interval between two frames
64                * Frame interval depends on the scale value you set for
65                * this timer. So it can be different for different timers
66                **/
67                float32 getFrameInterval() const { return static_cast<float32>(_frameTime); }
68       
69                /**
70                * Returns true if this timer is currently sleeping/paused
71                **/
72                bool isPaused () const{ return _bPaused; }
73                       
74                /**
75                * Returns the time scale value of this timer. Each timer can have each own
76                * opinion how much time is passed. This is done by setting the scale value
77                * of the timer. This scale shows how much faster/slower this timer runs
78                **/
79                float32 getScale () const{ return _fScale; }
80       
81                /**
82                * This will hold on this timer so time stops
83                * @param bOn if true timer stops, otherwise it runs
84                **/
85                void setPause (bool bOn){ _bPaused = bOn; }
86               
87                /**
88                * Set time scale value for this timer. With the help of this value you have
89                * the possibility to run your local time at different speed
90                **/
91                void    setScale (float32 fScale){ _fScale = fScale; }
92               
93                /**
94                * This function will be always called by clock. Here the main update of the timer
95                * is done.
96                **/
97                virtual void notifyTimeObserver();
98
99                /**
100                * If you set this to true so fix frame rate will be used. This helps us
101                * to run our application on console or do internal calculations based
102                * on fix frame rate like Doom3 does.
103                * @param setFixRate if true fix frame rate will be used.
104                * @param fixFrameRate frame rate to be used
105                **/
106                void    setFixFrameRate(bool setFixRate, float32 fixFrameRate = 60.0f);
107
108                /**
109                * Reset the timer, so it start count the time from the value you define
110                **/
111                NR_FORCEINLINE void resetObserver(float64 resetToTime = 0.0f);
112               
113        private:
114               
115                // clock to be used with this timer     
116                Clock& _clock;
117               
118                // current time and interval
119                float64 _currentTime;
120                float64 _frameTime;
121                float64 _fixFrameTime;
122               
123                // pausing and scaling
124                bool            _bPaused;
125                float32         _fScale;
126                bool            _bFixFrameRate;
127               
128        };
129
130       
131
132        //----------------------------------------------------------------------------------
133        NR_FORCEINLINE void Timer::resetObserver(float64 resetToTime)
134        {
135                _currentTime = resetToTime;
136                NR_Log(Log::LOG_ENGINE, "Timer: Reset timer %d to %f", resetToTime, getObserverID());
137        }
138
139       
140}; // end namespace
141
142
143#endif
Note: See TracBrowser for help on using the repository browser.