Timer.h

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 #ifndef _NR_C_TIMER_H_
00015 #define _NR_C_TIMER_H_
00016 
00017 
00018 //----------------------------------------------------------------------------------
00019 // Includes
00020 //----------------------------------------------------------------------------------
00021 #include "Prerequisities.h"
00022 #include "Clock.h"
00023 #include "ITimeObserver.h"
00024 #include "Log.h"
00025 
00026 namespace nrEngine{
00027 
00028         //! Timers are used to retrieve time. You can hav emore than one timer in your app
00029         /**
00030         * Timer is an standard timer shipped with the engine. You can
00031         * create your own timers because they are specialized for your application.
00032         * This timer can only ticks and provide user with information he/she need.
00033         * Also this timer supports scaling and pausing, so you are able to use
00034         * at lesser speed or hold on parts of your game using this timer.
00035         *
00036         * @note Because each timer is created from the clock, so the timer lifespan
00037         * is the same as from the clock. So if clock is destroyed, timer will also
00038         * be killed. You also can not destroy the timer object, only clock can do this.
00039         *
00040         * \ingroup time
00041         **/
00042         class _NRExport Timer: public ITimeObserver {
00043         public:
00044         
00045                 /**
00046                 * Define default values and set clock for using with this timer.
00047                 * This timer will automaticly be added to the clock as observer.
00048                 **/
00049                 Timer(Clock& clock);    
00050                 
00051                 /**
00052                 * Release used memory and remove this timer from observer list of
00053                 * the clock.
00054                 **/
00055                 virtual ~Timer();
00056                 
00057                 /**
00058                 * Returns current time since this timer is started
00059                 **/
00060                 float64 getTime() const { return _currentTime; }
00061                 
00062                 /**
00063                 * Returns the time interval between two frames
00064                 * Frame interval depends on the scale value you set for
00065                 * this timer. So it can be different for different timers
00066                 **/
00067                 float32 getFrameInterval() const { return static_cast<float32>(_frameTime); }
00068         
00069                 /**
00070                 * Returns real frame interval based on Clock::getRealFrameInterval();
00071                 **/
00072                 float32 getRealFrameInterval() const;
00073                 
00074                 /**
00075                 * Returns true if this timer is currently sleeping/paused
00076                 **/
00077                 bool isPaused () const{ return _bPaused; }
00078                         
00079                 /**
00080                 * Returns the time scale value of this timer. Each timer can have each own
00081                 * opinion how much time is passed. This is done by setting the scale value
00082                 * of the timer. This scale shows how much faster/slower this timer runs
00083                 **/
00084                 float32 getScale () const{ return _fScale; }
00085         
00086                 /**
00087                 * This will hold on this timer so time stops
00088                 * @param bOn if true timer stops, otherwise it runs
00089                 **/
00090                 void setPause (bool bOn){ _bPaused = bOn; }
00091                 
00092                 /**
00093                 * Set time scale value for this timer. With the help of this value you have
00094                 * the possibility to run your local time at different speed
00095                 **/
00096                 void    setScale (float32 fScale){ _fScale = fScale; }
00097                 
00098                 /**
00099                 * This function will be always called by clock. Here the main update of the timer
00100                 * is done. 
00101                 **/
00102                 virtual void notifyTimeObserver();
00103 
00104                 /**
00105                 * If you set this to true so fix frame rate will be used. This helps us
00106                 * to run our application on console or do internal calculations based
00107                 * on fix frame rate like Doom3 does.
00108                 * @param setFixRate if true fix frame rate will be used.
00109                 * @param fixFrameRate frame rate to be used
00110                 **/
00111                 void    setFixFrameRate(bool setFixRate, float32 fixFrameRate = 60.0f);
00112 
00113                 /**
00114                 * Reset the timer, so it start count the time from the value you define
00115                 **/
00116                 NR_FORCEINLINE void resetObserver(float64 resetToTime = 0.0f);
00117                 
00118         private:
00119                 
00120                 // clock to be used with this timer     
00121                 Clock& _clock;
00122                 
00123                 // current time and interval
00124                 float64 _currentTime;
00125                 float64 _frameTime;
00126                 float64 _fixFrameTime;
00127                 
00128                 // pausing and scaling
00129                 bool            _bPaused;
00130                 float32         _fScale;
00131                 bool            _bFixFrameRate;
00132                 
00133         };
00134 
00135         
00136 
00137         //----------------------------------------------------------------------------------
00138         NR_FORCEINLINE void Timer::resetObserver(float64 resetToTime)
00139         {
00140                 _currentTime = resetToTime;
00141                 NR_Log(Log::LOG_ENGINE, "Timer: Reset timer %d to %f", resetToTime, getObserverID());
00142         }
00143 
00144         
00145 }; // end namespace
00146 
00147 
00148 #endif

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1