timeclock.h

00001 /*--License:
00002         Lilith 3D Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2002-2007
00004         www.grinninglizard.com/lilith
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020         The full text of the license can be found in license.txt
00021 */
00022 #ifndef LILITH_TIMECLOCK_INCLUDED
00023 #define LILITH_TIMECLOCK_INCLUDED
00024 
00025 #include <time.h>
00026 #include "../grinliz/gltypes.h"
00027 #include "../grinliz/glcolor.h"
00028 #include "../grinliz/glvector.h"
00029 #include "worlddefine.h"
00030 
00031 namespace lilith3d
00032 {
00033 
00034 class Texture;
00035 
00040 struct SunMoon
00041 {
00042         enum {
00043                 SUNRISE, DAYTIME, SUNSET, NIGHT
00044         };
00045 
00046         bool            sun;                                    
00047         grinliz::Vector3F       vector;                 
00048         grinliz::Vector4F       vector4;                
00049         grinliz::Color3F ambientColorH;         
00050         grinliz::Color4F ambientColorH4;        
00051         grinliz::Color3F diffuseColorH;         
00052         grinliz::Color4F diffuseColorH4;        
00053         grinliz::Color3F ambientColorL;         
00054         grinliz::Color3F diffuseColorL;         
00055         grinliz::Color4F ambientColorL4;        
00056         grinliz::Color4F diffuseColorL4;        
00057         grinliz::Color3F rawAmbientColor;       
00058         grinliz::Color3F rawDiffuseColor;       
00059         float           skyBlend;                               
00060         int                     primarySky;                             
00061         int                     secondarySky;                   
00062         grinliz::Color3F        skyColor;               
00063         const Texture* primarySkydomeTex;               
00064         const Texture* secondarySkydomeTex;             
00065 };
00066 
00067 
00076 class TimeClock
00077 {
00078   public:
00079         TimeClock( U32 msecPerDay, U32 msecStart );
00080         ~TimeClock()    {}
00081 
00083         // --- Frames --- //
00084 
00086         U32 CurrentFrame()                                              {       return frameCount; }
00087 
00088         // Tell a clock when "now" is. Called only by the lilith engine.
00089         void SetFrameStart();
00090         // Tell the clock that a frame was draw. Again called only by the engine.
00091         void SetFrameDrawn();
00092 
00094         float Fps()     {       return fps; }
00096         float FastFps() { return shortFps; }
00097 
00099         float TotalFPS();
00100 
00102         // --- Speed Based Computation --- //
00103 
00107         float CalcVelocity( float vel ) {       return float( msecToLast ) * vel / 1000.0f; }
00108 
00112         float CalcChange( float value, float velocity, float minVal, float maxVal )     
00113         {       
00114                 GLASSERT( maxVal >= minVal );
00115 
00116                 value = value + CalcVelocity( velocity );
00117                 return grinliz::Clamp( value, minVal, maxVal );
00118         }
00119         
00120 
00122         // --- Time elapsed --- //
00123 
00125         U32     Msec()                                          {       return msec;    }
00126 
00128         float HoursElapsed()                    {       return float( msec + msecStart ) * 24.0f / float( msecPerDay ); }
00129 
00131         void CalcCalendarTime( U32* days, U32* hours, U32* minutes, U32* seconds );
00132 
00133 
00135         // --- The sun and moon -- //
00136 
00138         void ToggleSun()                                {       ++holdSun;
00139                                                                                 if ( holdSun == HOLD_COUNT ) holdSun = 0; 
00140                                                                         }
00141 
00142         const SunMoon& GetSunMoon()     {       return sunMoon; }
00143 
00144   private:
00145         struct SkyState
00146         {
00147                 float   hour;
00148                 int             state;
00149         };
00150 
00151         struct ColorState
00152         {
00153                 float           hour;
00154                 grinliz::Color3F        color;
00155         };
00156 
00157         U32 msecPerDay;
00158         U32 msecStart;
00159 
00160 
00161         // Called once the sun/moon vector is computed.
00162         void FillSunMoonStruct();
00163 
00164         static TimeClock* instance;
00165 
00166         U32 frameCount;
00167         U32 msec;                       // clock ticks
00168 
00169         enum { HOLD_NONE, HOLD_MIDDAY, HOLD_SUNRISE, HOLD_COUNT };
00170         int holdSun;
00171 
00172         SunMoon sunMoon;
00173 
00174         // For long sample fps:
00175         U32 longStart;
00176         U32 fpsFrame;
00177         float fps;
00178 
00179         // For 200ms fps:
00180         U32 shortStart;
00181         U32 shortFpsFrame;
00182         float shortFps;
00183         U32 startupMsec;
00184 
00185         // For short sample
00186         U32 msecToLast;
00187         U32 lastStart;
00188 };
00189 };
00190 
00191 #endif

Generated on Fri Mar 23 19:36:22 2007 for Lilith3D by  doxygen 1.5.1-p1