Adonthell  0.4
gametime.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001/2002 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file gametime.cc
21  *
22  * @author Kai Sterker
23  * @brief Implements the gametime class.
24  */
25 
26 #include "gametime.h"
27 #include <SDL.h>
28 
29 double gametime::Minute;
30 u_int32 gametime::timer1;
31 u_int32 gametime::timer2;
32 u_int8 gametime::fts;
33 bool gametime::running;
34 
35 // initialize the gametime class
36 void gametime::init (u_int16 rt_minutes)
37 {
38  fts = 0;
39  timer1 = 0;
40  timer2 = 0;
41 
42  running = false;
43 
44  // Number of game cycles during rt_minutes realtime minutes
45  double cycles = (60000 * rt_minutes) / (double) CYCLE_LENGTH;
46 
47  // Calculate how many game cycles make one gametime minute,
48  // so that one gametime day lasts rt_minutes realtime minutes.
49  Minute = cycles / 1440;
50 }
51 
52 // Synchronize the game's speed to the machine it's running on.
54 {
55  // We declare this variable as static to avoid having to
56  // perform the division every time.
57  // Its value corresponds to the minimum delay between
58  // two displayed frames (see FRAME_RATE).
59  static u_int16 gfx_cycle_length = 1000 / FRAME_RATE;
60 
61  // Wait a bit if our machine performed too fast!
62  while (1)
63  {
64  timer2 = SDL_GetTicks () - timer1;
65 
66  // if the mainloop was performed faster than one frame
67  // should take, we sleep for the time remaining
68  if (timer2 >= gfx_cycle_length) break;
69  else SDL_Delay (50);
70  }
71 
72  timer1 = SDL_GetTicks () - (timer2 % CYCLE_LENGTH);
73 
74  // Calculate the number of frames to skip (if the mainloop
75  // takes longer than allowed, we drop frames (up to a certain
76  // limit) to keep the game's speed constant.)
77  fts = timer2 / CYCLE_LENGTH;
78  if (fts > FTS_LIMIT) fts = FTS_LIMIT;
79 }
#define FRAME_RATE
Number of maximum displayed frames per second.
Definition: gametime.h:45
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
#define CYCLE_LENGTH
Length of a game cycle, in milliseconds.
Definition: gametime.h:37
Declares the gametime class.
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
static void update()
Call this after each run of the main loop to sync the game&#39;s speed to the machine it is running on...
Definition: gametime.cc:53
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
#define FTS_LIMIT
Defines the maximum number of frames to skip in order to keep the game&#39;s speed constant on slow machi...
Definition: gametime.h:56
static void init(u_int16 rt_minutes)
Initialize the gametime class.
Definition: gametime.cc:36