scummvm/common/timer.cpp

158 lines
3.3 KiB
C++
Raw Normal View History

/* 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$
*/
#ifndef __MORPHOS__
#include "stdafx.h"
#include "scummsys.h"
#include "timer.h"
static Engine * eng;
Timer::Timer(Engine * engine) {
_initialized = false;
_timerRunning = false;
eng = _engine = engine;
}
Timer::~Timer() {
release();
}
static int timer_handler (int t)
{
eng->_timer->handler(&t);
return t;
}
int Timer::handler(int * t) {
uint32 interval, l;
if (_timerRunning) {
_lastTime = _thisTime;
_thisTime = _engine->_system->get_msecs();
interval = _thisTime - _lastTime;
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;
_timerSlots[l].procedure (_engine);
}
}
}
}
return *t;
}
bool Timer::init() {
int32 l;
if (_engine->_system == NULL) {
printf("Timer: OSystem not initialized !\n");
return false;
}
if (_initialized == true)
return true;
for (l = 0; l < MAX_TIMERS; l++) {
_timerSlots[l].procedure = NULL;
_timerSlots[l].interval = 0;
_timerSlots[l].counter = 0;
}
_thisTime = _engine->_system->get_msecs();
_engine->_system->set_timer(10, &timer_handler);
_timerRunning = true;
_initialized = true;
return true;
}
void Timer::release() {
int32 l;
if (_initialized == false)
return;
_timerRunning = false;
_engine->_system->set_timer(0, NULL);
_initialized = false;
for (l = 0; l < MAX_TIMERS; l++) {
_timerSlots[l].procedure = NULL;
_timerSlots[l].interval = 0;
_timerSlots[l].counter = 0;
}
}
bool Timer::installProcedure (TimerProc procedure, int32 interval) {
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;
}
void Timer::releaseProcedure (TimerProc procedure) {
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;
}
#endif