1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/system.h"
24 
25 #include "sludge/timing.h"
26 
27 namespace Sludge {
28 
Timer()29 Timer::Timer(){
30 	reset();
31 }
32 
reset(void)33 void Timer::reset(void) {
34 	_desiredFPS = 300;
35 	_startTime = 0;
36 	_endTime = 0;
37 	_desiredFrameTime = 0;
38 	_addNextTime = 0;
39 
40 	// FPS stats
41 	_lastFPS = -1;
42 	_thisFPS = -1;
43 	_lastSeconds = 0;
44 }
45 
init(void)46 void Timer::init(void) {
47 	_desiredFrameTime = 1000 / _desiredFPS;
48 	_startTime = g_system->getMillis();
49 }
50 
initSpecial(int t)51 void Timer::initSpecial(int t) {
52 	_desiredFrameTime = 1000 / t;
53 	_startTime = g_system->getMillis();
54 }
55 
updateFpsStats()56 void Timer::updateFpsStats() {
57 	uint32 currentSeconds = g_system->getMillis() / 1000;
58 	if (_lastSeconds != currentSeconds) {
59 		_lastSeconds = currentSeconds;
60 		_lastFPS = _thisFPS;
61 		_thisFPS = 1;
62 	} else {
63 		++_thisFPS;
64 	}
65 }
66 
waitFrame(void)67 void Timer::waitFrame(void) {
68 	uint32 timetaken;
69 
70 	for (;;) {
71 		_endTime = g_system->getMillis();
72 		timetaken = _addNextTime + _endTime - _startTime;
73 		if (timetaken >= _desiredFrameTime)
74 			break;
75 		g_system->delayMillis(1);
76 	}
77 
78 	_addNextTime = timetaken - _desiredFrameTime;
79 	if (_addNextTime > _desiredFrameTime)
80 		_addNextTime = _desiredFrameTime;
81 
82 	_startTime = _endTime;
83 
84 	// Stats
85 	updateFpsStats();
86 }
87 
88 } // End of namespace Sludge
89