aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/timing.cpp
blob: c291f57a6b2150b2d4f0ed1fc88f5c79c99abec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "sludge/allfiles.h"
#include "sludge/timing.h"

namespace Sludge {

Timer::Timer(){
	reset();
}

void Timer::reset(void) {
	_desiredFPS = 300;
	_startTime = 0;
	_endTime = 0;
	_desiredFrameTime = 0;
	_addNextTime = 0;

	// FPS stats
	_lastFPS = -1;
	_thisFPS = -1;
	_lastSeconds = 0;
}

void Timer::init(void) {
	_desiredFrameTime = 1000 / _desiredFPS;
	_startTime = g_system->getMillis();
}

void Timer::initSpecial(int t) {
	_desiredFrameTime = 1000 / t;
	_startTime = g_system->getMillis();
}

void Timer::updateFpsStats() {
	uint32 currentSeconds = g_system->getMillis() / 1000;
	if (_lastSeconds != currentSeconds) {
		_lastSeconds = currentSeconds;
		_lastFPS = _thisFPS;
		_thisFPS = 1;
	} else {
		++_thisFPS;
	}
}

void Timer::waitFrame(void) {
	uint32 timetaken;

	for (;;) {
		_endTime = g_system->getMillis();
		timetaken = _addNextTime + _endTime - _startTime;
		if (timetaken >= _desiredFrameTime)
			break;
		g_system->delayMillis(1);
	}

	_addNextTime = timetaken - _desiredFrameTime;
	if (_addNextTime > _desiredFrameTime)
		_addNextTime = _desiredFrameTime;

	_startTime = _endTime;

	// Stats
	updateFpsStats();
}

} // End of namespace Sludge