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 "bladerunner/framelimiter.h"
24 
25 #include "bladerunner/bladerunner.h"
26 #include "bladerunner/time.h"
27 
28 #include "common/debug.h"
29 #include "common/system.h"
30 
31 namespace BladeRunner {
32 
Framelimiter(BladeRunnerEngine * vm,uint fps)33 Framelimiter::Framelimiter(BladeRunnerEngine *vm, uint fps) {
34 	_vm = vm;
35 
36 	reset();
37 
38 	if (fps > 0) {
39 		_enabled = true;
40 		_speedLimitMs = 1000 / fps;
41 	} else {
42 		_enabled = false;
43 	}
44 
45 	_timeFrameStart = _vm->_time->currentSystem();
46 }
47 
wait()48 void Framelimiter::wait() {
49 	// TODO: when vsync will be supported, use it
50 
51 	if (!_enabled) {
52 		return;
53 	}
54 
55 	uint32 timeNow = _vm->_time->currentSystem();
56 	uint32 frameDuration = timeNow - _timeFrameStart;
57 	if (frameDuration < _speedLimitMs) {
58 		uint32 waittime = _speedLimitMs - frameDuration;
59 		if (_vm->_noDelayMillisFramelimiter) {
60 			while (_vm->_time->currentSystem() - timeNow < waittime) { }
61 		} else {
62 			_vm->_system->delayMillis(waittime);
63 		}
64 		timeNow += waittime;
65 	}
66 	// debug("frametime %i ms", timeNow - _timeFrameStart);
67 	// using _vm->_time->currentSystem() here is slower and causes some shutters
68 	_timeFrameStart = timeNow;
69 }
70 
reset()71 void Framelimiter::reset() {
72 	_timeFrameStart = 0u;
73 }
74 
75 } // End of namespace BladeRunner
76