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 "sludge/allfiles.h"
24 #include "sludge/timing.h"
25 
26 namespace Sludge {
27 
Timer()28 Timer::Timer(){
29 	reset();
30 }
31 
reset(void)32 void Timer::reset(void) {
33 	_desiredFPS = 300;
34 	_startTime = 0;
35 	_endTime = 0;
36 	_desiredFrameTime = 0;
37 	_addNextTime = 0;
38 
39 	// FPS stats
40 	_lastFPS = -1;
41 	_thisFPS = -1;
42 	_lastSeconds = 0;
43 }
44 
init(void)45 void Timer::init(void) {
46 	_desiredFrameTime = 1000 / _desiredFPS;
47 	_startTime = g_system->getMillis();
48 }
49 
initSpecial(int t)50 void Timer::initSpecial(int t) {
51 	_desiredFrameTime = 1000 / t;
52 	_startTime = g_system->getMillis();
53 }
54 
updateFpsStats()55 void Timer::updateFpsStats() {
56 	uint32 currentSeconds = g_system->getMillis() / 1000;
57 	if (_lastSeconds != currentSeconds) {
58 		_lastSeconds = currentSeconds;
59 		_lastFPS = _thisFPS;
60 		_thisFPS = 1;
61 	} else {
62 		++_thisFPS;
63 	}
64 }
65 
waitFrame(void)66 void Timer::waitFrame(void) {
67 	uint32 timetaken;
68 
69 	for (;;) {
70 		_endTime = g_system->getMillis();
71 		timetaken = _addNextTime + _endTime - _startTime;
72 		if (timetaken >= _desiredFrameTime)
73 			break;
74 		g_system->delayMillis(1);
75 	}
76 
77 	_addNextTime = timetaken - _desiredFrameTime;
78 	if (_addNextTime > _desiredFrameTime)
79 		_addNextTime = _desiredFrameTime;
80 
81 	_startTime = _endTime;
82 
83 	// Stats
84 	updateFpsStats();
85 }
86 
87 } // End of namespace Sludge
88