Adonthell 0.4
|
00001 /* 00002 $Id: gametime.cc,v 1.14 2005/04/16 17:56:32 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.cc 00017 * 00018 * @author Kai Sterker 00019 * @brief Implements the gametime class. 00020 */ 00021 00022 #include "gametime.h" 00023 #include <SDL/SDL.h> 00024 00025 double gametime::Minute; 00026 u_int32 gametime::timer1; 00027 u_int32 gametime::timer2; 00028 u_int8 gametime::fts; 00029 bool gametime::running; 00030 00031 // initialize the gametime class 00032 void gametime::init (u_int16 rt_minutes) 00033 { 00034 fts = 0; 00035 timer1 = 0; 00036 timer2 = 0; 00037 00038 running = false; 00039 00040 // Number of game cycles during rt_minutes realtime minutes 00041 double cycles = (60000 * rt_minutes) / (double) CYCLE_LENGTH; 00042 00043 // Calculate how many game cycles make one gametime minute, 00044 // so that one gametime day lasts rt_minutes realtime minutes. 00045 Minute = cycles / 1440; 00046 } 00047 00048 // Synchronize the game's speed to the machine it's running on. 00049 void gametime::update () 00050 { 00051 // We declare this variable as static to avoid having to 00052 // perform the division every time. 00053 // Its value corresponds to the minimum delay between 00054 // two displayed frames (see FRAME_RATE). 00055 static u_int16 gfx_cycle_length = 1000 / FRAME_RATE; 00056 00057 // Wait a bit if our machine performed too fast! 00058 while (1) 00059 { 00060 timer2 = SDL_GetTicks () - timer1; 00061 00062 // if the mainloop was performed faster than one frame 00063 // should take, we sleep for the time remaining 00064 if (timer2 >= gfx_cycle_length) break; 00065 else SDL_Delay (50); 00066 } 00067 00068 timer1 = SDL_GetTicks () - (timer2 % CYCLE_LENGTH); 00069 00070 // Calculate the number of frames to skip (if the mainloop 00071 // takes longer than allowed, we drop frames (up to a certain 00072 // limit) to keep the game's speed constant.) 00073 fts = timer2 / CYCLE_LENGTH; 00074 if (fts > FTS_LIMIT) fts = FTS_LIMIT; 00075 }