aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/sdl/timer.cpp
blob: 865c64826d76e03f247f6d869fd83b995b80a76d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "backends/platform/sdl/timer.h"
#include "backends/timer/default/default-timer.h"
#include "common/EventRecorder.h"
#include <time.h>	// for getTimeAndDate()

static Uint32 timer_handler(Uint32 interval, void *param) {
	((DefaultTimerManager *)param)->handler();
	return interval;
}

SdlSubSys_Timer::SdlSubSys_Timer()
	:
	_inited(false),
	_timerID(),
	_timer(0) {

}

SdlSubSys_Timer::~SdlSubSys_Timer() {
	if (_inited) {
		timerDone();
	}
}

void SdlSubSys_Timer::timerInit() {
	if (_inited) {
		return;
	}

	if (SDL_InitSubSystem(SDL_INIT_TIMER) == -1) {
		error("Could not initialize SDL Timer: %s", SDL_GetError());
	}

	// Create and hook up the timer manager, if none exists yet (we check for
	// this to allow subclasses to provide their own).
	if (_timer == 0) {
		// Note: We could implement a custom SDLTimerManager by using
		// SDL_AddTimer. That might yield better timer resolution, but it would
		// also change the semantics of a timer: Right now, ScummVM timers
		// *never* run in parallel, due to the way they are implemented. If we
		// switched to SDL_AddTimer, each timer might run in a separate thread.
		// However, not all our code is prepared for that, so we can't just
		// switch. Still, it's a potential future change to keep in mind.
		_timer = new DefaultTimerManager();
		_timerID = SDL_AddTimer(10, &timer_handler, _timer);
	}

	_inited = true;
}

void SdlSubSys_Timer::timerDone() {
	delete _timer;
	SDL_RemoveTimer(_timerID);
	SDL_QuitSubSystem(SDL_INIT_TIMER);

	_inited = false;
}

bool SdlSubSys_Timer::hasFeature(Feature f) {
	return false;
}

void SdlSubSys_Timer::setFeatureState(Feature f, bool enable) {
	
}

bool SdlSubSys_Timer::getFeatureState(Feature f) {
	return false;
}

uint32 SdlSubSys_Timer::getMillis() {
	uint32 millis = SDL_GetTicks();
	g_eventRec.processMillis(millis);
	return millis;
}

void SdlSubSys_Timer::delayMillis(uint msecs) {
	SDL_Delay(msecs);
}

void SdlSubSys_Timer::getTimeAndDate(TimeDate &td) const {
	time_t curTime = time(0);
	struct tm t = *localtime(&curTime);
	td.tm_sec = t.tm_sec;
	td.tm_min = t.tm_min;
	td.tm_hour = t.tm_hour;
	td.tm_mday = t.tm_mday;
	td.tm_mon = t.tm_mon;
	td.tm_year = t.tm_year;
}

Common::TimerManager *SdlSubSys_Timer::getTimerManager() {
	assert(_timer);
	return _timer;
}