2002-08-04 16:30:59 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2002 The ScummVM project
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
2002-08-04 17:31:20 +00:00
|
|
|
* $Header$
|
2002-08-04 16:30:59 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
#ifndef __MORPHOS__
|
|
|
|
|
2002-08-04 16:30:59 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "scummsys.h"
|
|
|
|
#include "timer.h"
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2002-09-18 10:22:36 +00:00
|
|
|
static Engine * eng;
|
2002-08-04 16:30:59 +00:00
|
|
|
|
2002-09-18 10:22:36 +00:00
|
|
|
Timer::Timer(Engine * engine) {
|
2002-08-04 16:30:59 +00:00
|
|
|
_initialized = false;
|
|
|
|
_timerRunning = false;
|
2002-09-18 10:22:36 +00:00
|
|
|
eng = _engine = engine;
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer() {
|
2002-09-18 10:22:36 +00:00
|
|
|
release();
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
static int timer_handler (int t) {
|
2002-09-18 10:22:36 +00:00
|
|
|
eng->_timer->handler(&t);
|
2002-08-04 16:30:59 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Timer::handler(int * t) {
|
|
|
|
uint32 interval, l;
|
|
|
|
|
|
|
|
if (_timerRunning) {
|
|
|
|
_lastTime = _thisTime;
|
2002-09-18 10:22:36 +00:00
|
|
|
_thisTime = _engine->_system->get_msecs();
|
2003-01-25 12:13:41 +00:00
|
|
|
interval = 1000 * (_thisTime - _lastTime);
|
2002-08-04 16:30:59 +00:00
|
|
|
|
|
|
|
for (l = 0; l < MAX_TIMERS; l++) {
|
|
|
|
if ((_timerSlots[l].procedure) && (_timerSlots[l].interval > 0)) {
|
|
|
|
_timerSlots[l].counter -= interval;
|
|
|
|
if (_timerSlots[l].counter <= 0) {
|
|
|
|
_timerSlots[l].counter += _timerSlots[l].interval;
|
2002-09-18 10:22:36 +00:00
|
|
|
_timerSlots[l].procedure (_engine);
|
2002-08-04 16:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *t;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::init() {
|
|
|
|
int32 l;
|
|
|
|
|
2002-09-18 10:22:36 +00:00
|
|
|
if (_engine->_system == NULL) {
|
2003-03-06 16:27:06 +00:00
|
|
|
printf("Timer: OSystem not initialized !\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-08-04 16:30:59 +00:00
|
|
|
|
|
|
|
if (_initialized == true)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (l = 0; l < MAX_TIMERS; l++) {
|
|
|
|
_timerSlots[l].procedure = NULL;
|
|
|
|
_timerSlots[l].interval = 0;
|
|
|
|
_timerSlots[l].counter = 0;
|
|
|
|
}
|
|
|
|
|
2002-09-18 10:22:36 +00:00
|
|
|
_thisTime = _engine->_system->get_msecs();
|
|
|
|
_engine->_system->set_timer(10, &timer_handler);
|
2002-08-04 16:30:59 +00:00
|
|
|
|
|
|
|
_timerRunning = true;
|
|
|
|
_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::release() {
|
|
|
|
int32 l;
|
|
|
|
|
|
|
|
if (_initialized == false)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_timerRunning = false;
|
2002-09-18 10:22:36 +00:00
|
|
|
_engine->_system->set_timer(0, NULL);
|
2002-08-04 16:30:59 +00:00
|
|
|
_initialized = false;
|
|
|
|
|
|
|
|
for (l = 0; l < MAX_TIMERS; l++) {
|
|
|
|
_timerSlots[l].procedure = NULL;
|
|
|
|
_timerSlots[l].interval = 0;
|
|
|
|
_timerSlots[l].counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-14 16:18:45 +00:00
|
|
|
bool Timer::installProcedure (TimerProc procedure, int32 interval) {
|
2002-08-04 16:30:59 +00:00
|
|
|
int32 l;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
if (_initialized == false) {
|
|
|
|
printf ("Timer: is not initialized !");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_timerRunning = false;
|
|
|
|
for (l = 0; l < MAX_TIMERS; l++) {
|
|
|
|
if (!_timerSlots[l].procedure) {
|
|
|
|
_timerSlots[l].procedure = procedure;
|
|
|
|
_timerSlots[l].interval = interval;
|
|
|
|
_timerSlots[l].counter = interval;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_timerRunning = true;
|
|
|
|
if (!found) {
|
|
|
|
printf ("Can't find free slot !");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-08-14 16:18:45 +00:00
|
|
|
void Timer::releaseProcedure (TimerProc procedure) {
|
2002-08-04 16:30:59 +00:00
|
|
|
int32 l;
|
|
|
|
|
|
|
|
if (_initialized == false) {
|
|
|
|
printf ("Timer: is not initialized !");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_timerRunning = false;
|
|
|
|
for (l = 0; l < MAX_TIMERS; l++) {
|
|
|
|
if (_timerSlots[l].procedure == procedure) {
|
|
|
|
_timerSlots[l].procedure = 0;
|
|
|
|
_timerSlots[l].interval = 0;
|
|
|
|
_timerSlots[l].counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_timerRunning = true;
|
|
|
|
}
|
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
#endif
|