removed unused test level
[supertux.git] / lib / special / frame_rate.cpp
1 // 
2 //  SuperTux
3 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 2
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program 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 this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 #include <config.h>
20
21 #include "SDL.h"
22
23 #include "frame_rate.h"
24 #include "timer.h"
25
26 using namespace SuperTux;
27
28 FrameRate::FrameRate(double fps)
29 {
30   set_fps(fps);
31   set_frame_limit(true);
32 }
33
34 void FrameRate::start()
35 {
36   update_time = last_update_time = Ticks::get();  
37 }
38
39 void FrameRate::set_fps(double fps)
40 {
41   frame_ms = static_cast<unsigned int>(1000.f/fps);
42 }
43
44 void FrameRate::set_frame_limit(bool set_limit)
45 {
46   frame_limit = set_limit;
47 }
48
49 double FrameRate::get()
50 {
51   return ((double)(update_time-last_update_time))/(double)frame_ms;
52 }
53
54 void FrameRate::update()
55 {
56   /* Set the time of the last update and the time of the current update */
57   last_update_time = update_time;
58   update_time      = Ticks::get();
59
60   /* Pause till next frame, if the machine running the game is too fast: */
61   /* FIXME: Works great for in OpenGl mode, where the CPU doesn't have to do that much. But
62      the results in SDL mode aren't perfect (thought the 100 FPS are reached), even on an AMD2500+. */
63   if(frame_limit && last_update_time >= update_time - (frame_ms+2))
64     {
65       SDL_Delay(frame_ms);
66       update_time = Ticks::get();
67     }
68 }
69
70 void FrameRate::smooth_hanger()
71 {
72       if( (update_time - last_update_time) > frame_ms*100)
73         update_time = last_update_time = Ticks::get();
74 }