Adonthell  0.4
gametime.h
Go to the documentation of this file.
1 /*
2  $Id: gametime.h,v 1.13 2002/12/04 17:09:48 ksterker Exp $
3 
4  Copyright (C) 2001/2002 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file gametime.h
17  *
18  * @author Kai Sterker
19  * @brief Declares the gametime class.
20  */
21 
22 #ifndef GAMETIME_H_
23 #define GAMETIME_H_
24 
25 #include "types.h"
26 
27 /**
28  * Length of a %game cycle, in milliseconds. Decrease it to speed up
29  * the %game, increase it to slow the %game down. This constant
30  * defines how often the state of the game world is updated.
31  * A cycle length of 13 means 1000/13 = 76.9 updates per second.
32  */
33 #define CYCLE_LENGTH 13
34 
35 /**
36  * Number of maximum displayed frames per second. This value affects
37  * the renderer only, not the speed of the game itself. Limiting the
38  * frame rate prevents Adonthell from using all the CPU of the
39  * machine it runs on (as long as the machine is fast enough).
40  */
41 #define FRAME_RATE 50
42 
43 /**
44  * Defines the maximum number of frames to skip in order to keep the %game's
45  * speed constant on slow machines. If updating the %game state and
46  * drawing everything on %screen takes longer than CYCLE_LENGTH, we
47  * skip frames so that the correct number of updates can be performed,
48  * thus keeping the speed constant. However, we can't skip too many
49  * frames, since that would result in jerky animations and eventually
50  * render the %game unplayable.
51  */
52 #define FTS_LIMIT 20
53 
54 /**
55  * Tehe %gametime class makes the speed of the %game independent of
56  * the machine it runs on. This is achieved by keeping the number of
57  * updates to the %game state constant, no matter how fast or slow
58  * the machine. This won't work for very slow machines of course,
59  * but Adonthell will still be playable on a 100 Ghz CPU.
60  */
61 class gametime
62 {
63 public:
64  /**
65  * Initialize the gametime class.
66  *
67  * @param rt_minutes Defines how many real life minutes make one
68  * gametime day.
69  */
70  static void init (u_int16 rt_minutes);
71 
72  /**
73  * Return the in-game time that passed since the last call to
74  * this method.
75  *
76  * @return %gametime in minutes.
77  */
78  static double minute ()
79  {
80  return Minute;
81  }
82 
83  /**
84  *
85  */
86  static void start_action ()
87  {
88  fts = 0;
89  running = true;
90  }
91 
92  /**
93  *
94  */
95  static void stop_action ()
96  {
97  running = false;
98  }
99 
100  /**
101  * @name Methods to sync the %game speed to the machine it runs on
102  */
103  //@{
104  /**
105  * Returns the number of updates to perform before drawing
106  * the next frame. That is, it returns the number of frames to skip.
107  * If the box Adonthell runs on is sufficiently fast, this number
108  * will be 0. On slower machines, it can be as high as FTS_LIMIT.
109  *
110  * @return number of updates to perform before drawing a frame.
111  * @see FTS_LIMIT
112  */
114  {
115  return fts;
116  }
117 
118  /**
119  * Call this after each run of the main loop to sync the %game's
120  * speed to the machine it is running on. On faster machines it
121  * delays the execution and for slower boxes it calculates the
122  * number of frames to skip. If the engine should do 50 frames per
123  * second, for example, but the main loop takes 26ms to perform,
124  * every second frame will be skipped to keep the %game' speed
125  * constant.
126  * It also updates the internal clock.
127  *
128  * @see frames_to_skip ()
129  */
130  static void update ();
131  //@}
132 
133 private:
134 #ifndef SWIG
135  // One minute of gametime in game cycles
136  static double Minute;
137 
138  static bool running;
139 
140  // Frames to skip.
141  static u_int8 fts;
142 
143  // Timers used to calculate the delay between 2 update() calls.
144  static u_int32 timer1, timer2;
145 #endif // SWIG
146 };
147 
148 #endif // GAMETIME_H_