Adonthell 0.4
gametime.h
Go to the documentation of this file.
00001 /*
00002    $Id: gametime.h,v 1.13 2002/12/04 17:09:48 ksterker Exp $
00003 
00004    Copyright (C) 2001/2002 Kai Sterker <kaisterker@linuxgames.com>
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 /**
00016  * @file gametime.h
00017  *
00018  * @author Kai Sterker
00019  * @brief Declares the gametime class.
00020  */
00021 
00022 #ifndef GAMETIME_H_
00023 #define GAMETIME_H_
00024 
00025 #include "types.h"
00026 
00027 /**
00028  * Length of a %game cycle, in milliseconds. Decrease it to speed up
00029  * the %game, increase it to slow the %game down.  This constant 
00030  * defines how often the state of the game world is updated. 
00031  * A cycle length of 13 means 1000/13 = 76.9 updates per second. 
00032  */ 
00033 #define CYCLE_LENGTH 13
00034 
00035 /**
00036  * Number of maximum displayed frames per second. This value affects
00037  * the renderer only, not the speed of the game itself. Limiting the 
00038  * frame rate prevents Adonthell from using all the CPU of the
00039  * machine it runs on (as long as the machine is fast enough).
00040  */ 
00041 #define FRAME_RATE 50
00042 
00043 /** 
00044  * Defines the maximum number of frames to skip in order to keep the %game's
00045  * speed constant on slow machines. If updating the %game state and
00046  * drawing everything on %screen takes longer than CYCLE_LENGTH, we
00047  * skip frames so that the correct number of updates can be performed,
00048  * thus keeping the speed constant. However, we can't skip too many
00049  * frames, since that would result in jerky animations and eventually
00050  * render the %game unplayable.
00051  */ 
00052 #define FTS_LIMIT 20
00053 
00054 /**
00055  * Tehe %gametime class makes the speed of the %game independent of 
00056  * the machine it runs on. This is achieved by keeping the number of 
00057  * updates to the %game state constant, no matter how fast or slow 
00058  * the machine. This won't work for very slow machines of course, 
00059  * but Adonthell will still be playable on a 100 Ghz CPU.
00060  */
00061 class gametime
00062 {
00063 public:
00064     /**
00065      * Initialize the gametime class.
00066      *
00067      * @param rt_minutes Defines how many real life minutes make one
00068      *      gametime day. 
00069      */
00070     static void init (u_int16 rt_minutes);
00071 
00072     /**
00073      * Return the in-game time that passed since the last call to
00074      * this method. 
00075      *
00076      * @return %gametime in minutes.
00077      */
00078     static double minute ()
00079     {
00080         return Minute;
00081     }
00082 
00083     /**
00084      *
00085      */
00086     static void start_action ()
00087     {
00088         fts = 0;
00089         running = true; 
00090     }
00091     
00092     /**
00093      *
00094      */
00095     static void stop_action () 
00096     {
00097         running = false; 
00098     }
00099 
00100     /**
00101      * @name Methods to sync the %game speed to the machine it runs on
00102      */
00103     //@{
00104     /** 
00105      * Returns the number of updates to perform before drawing
00106      * the next frame. That is, it returns the number of frames to skip.
00107      * If the box Adonthell runs on is sufficiently fast, this number
00108      * will be 0. On slower machines, it can be as high as FTS_LIMIT.   
00109      * 
00110      * @return number of updates to perform before drawing a frame.
00111      * @see FTS_LIMIT
00112      */ 
00113     static u_int8 frames_to_skip ()
00114     {
00115         return fts;
00116     }
00117 
00118     /**
00119       * Call this after each run of the main loop to sync the %game's
00120       * speed to the machine it is running on. On faster machines it
00121       * delays the execution and for slower boxes it calculates the
00122       * number of frames to skip. If the engine should do 50 frames per
00123       * second, for example, but the main loop takes 26ms to perform,
00124       * every second frame will be skipped to keep the %game' speed
00125       * constant.
00126       * It also updates the internal clock.
00127       * 
00128       * @see frames_to_skip ()
00129       */
00130     static void update (); 
00131     //@}
00132     
00133 private:
00134 #ifndef SWIG
00135     // One minute of gametime in game cycles
00136     static double Minute;
00137 
00138     static bool running; 
00139     
00140     // Frames to skip.
00141     static u_int8 fts; 
00142     
00143     // Timers used to calculate the delay between 2 update() calls.
00144     static u_int32 timer1, timer2;
00145 #endif // SWIG
00146 };
00147 
00148 #endif // GAMETIME_H_