1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
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 "engines/stark/gfx/framelimiter.h"
24 
25 #include "common/util.h"
26 
27 namespace Stark {
28 namespace Gfx {
29 
FrameLimiter(OSystem * system,const uint framerate)30 FrameLimiter::FrameLimiter(OSystem *system, const uint framerate) :
31 		_system(system),
32 		_speedLimitMs(0),
33 		_startFrameTime(0),
34 		_lastFrameDurationMs(_speedLimitMs) {
35 	// The frame limiter is disabled when vsync is enabled.
36 	_enabled = !_system->getFeatureState(OSystem::kFeatureVSync) && framerate != 0;
37 
38 	if (_enabled) {
39 		_speedLimitMs = 1000 / CLIP<uint>(framerate, 0, 100);
40 	}
41 }
42 
startFrame()43 void FrameLimiter::startFrame() {
44 	uint currentTime = _system->getMillis();
45 
46 	if (_startFrameTime != 0) {
47 		_lastFrameDurationMs = currentTime - _startFrameTime;
48 	}
49 
50 	_startFrameTime = currentTime;
51 }
52 
delayBeforeSwap()53 void FrameLimiter::delayBeforeSwap() {
54 	uint endFrameTime = _system->getMillis();
55 	uint frameDuration = endFrameTime - _startFrameTime;
56 
57 	if (_enabled && frameDuration < _speedLimitMs) {
58 		_system->delayMillis(_speedLimitMs - frameDuration);
59 	}
60 }
61 
pause(bool pause)62 void FrameLimiter::pause(bool pause) {
63 	if (!pause) {
64 		// Make sure the frame duration value is consistent when resuming
65 		_startFrameTime = 0;
66 	}
67 }
68 
getLastFrameDuration() const69 uint FrameLimiter::getLastFrameDuration() const {
70 	return _lastFrameDurationMs;
71 }
72 
73 } // End of namespace Gfx
74 } // End of namespace Stark
75