From 7bb45a8eaaf663cca9d0765d84b38967c3cc3275 Mon Sep 17 00:00:00 2001 From: Strangerke Date: Sat, 12 Dec 2015 19:38:00 +0100 Subject: LAB: Move timing functions to the utility class --- engines/lab/anim.cpp | 4 +- engines/lab/engine.cpp | 8 +-- engines/lab/intro.cpp | 7 +-- engines/lab/lab.h | 8 --- engines/lab/module.mk | 1 - engines/lab/processroom.cpp | 4 +- engines/lab/timing.cpp | 127 -------------------------------------------- engines/lab/utils.cpp | 93 +++++++++++++++++++++++++++++++- engines/lab/utils.h | 6 +++ 9 files changed, 110 insertions(+), 148 deletions(-) delete mode 100644 engines/lab/timing.cpp (limited to 'engines') diff --git a/engines/lab/anim.cpp b/engines/lab/anim.cpp index a461996e4a..3bed3656d3 100644 --- a/engines/lab/anim.cpp +++ b/engines/lab/anim.cpp @@ -98,8 +98,8 @@ void Anim::diffNextFrame(bool onlyDiffData) { if (!onlyDiffData) { if (_headerdata._fps) { - _vm->waitForTime(_waitSec, _waitMicros); - _vm->addCurTime(0L, _delayMicros, &_waitSec, &_waitMicros); + _vm->_utils->waitForTime(_waitSec, _waitMicros); + _vm->_utils->addCurTime(0L, _delayMicros, &_waitSec, &_waitMicros); } if (_isPal && !_noPalChange) { diff --git a/engines/lab/engine.cpp b/engines/lab/engine.cpp index 17518fb750..f0d3ab7a5b 100644 --- a/engines/lab/engine.cpp +++ b/engines/lab/engine.cpp @@ -609,7 +609,7 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo _followCrumbsFast = (code == 'r' || code == 'R'); _isCrumbTurning = false; _isCrumbWaiting = false; - getTime(&_crumbSecs, &_crumbMicros); + _utils->getTime(&_crumbSecs, &_crumbMicros); if (_alternate) { eatMessages(); @@ -930,7 +930,7 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo _followCrumbsFast = false; _isCrumbTurning = false; _isCrumbWaiting = false; - getTime(&_crumbSecs, &_crumbMicros); + _utils->getTime(&_crumbSecs, &_crumbMicros); eatMessages(); _alternate = false; @@ -1144,7 +1144,7 @@ int LabEngine::followCrumbs() { uint32 Secs; uint32 Micros; - timeDiff(_crumbSecs, _crumbMicros, &Secs, &Micros); + _utils->timeDiff(_crumbSecs, _crumbMicros, &Secs, &Micros); if (Secs != 0 || Micros != 0) return 0; @@ -1189,7 +1189,7 @@ int LabEngine::followCrumbs() { _isCrumbTurning = (moveDir != VKEY_UPARROW); _isCrumbWaiting = true; - addCurTime(theDelay / ONESECOND, theDelay % ONESECOND, &_crumbSecs, &_crumbMicros); + _utils->addCurTime(theDelay / ONESECOND, theDelay % ONESECOND, &_crumbSecs, &_crumbMicros); } return moveDir; diff --git a/engines/lab/intro.cpp b/engines/lab/intro.cpp index d0406bc192..633691c820 100644 --- a/engines/lab/intro.cpp +++ b/engines/lab/intro.cpp @@ -36,6 +36,7 @@ #include "lab/intro.h" #include "lab/music.h" #include "lab/resource.h" +#include "lab/utils.h" namespace Lab { @@ -131,7 +132,7 @@ void Intro::doPictText(const char *filename, TextFont *msgFont, bool isScreen) { return; } - _vm->getTime(&lastSecs, &lastMicros); + _vm->_utils->getTime(&lastSecs, &lastMicros); } msg = _vm->getMsg(); @@ -139,8 +140,8 @@ void Intro::doPictText(const char *filename, TextFont *msgFont, bool isScreen) { if (msg == NULL) { _vm->_music->updateMusic(); _vm->_anim->diffNextFrame(); - _vm->getTime(&secs, µs); - _vm->anyTimeDiff(lastSecs, lastMicros, secs, micros, &secs, µs); + _vm->_utils->getTime(&secs, µs); + _vm->_utils->anyTimeDiff(lastSecs, lastMicros, secs, micros, &secs, µs); if (secs > timeDelay) { if (end) { diff --git a/engines/lab/lab.h b/engines/lab/lab.h index 639983b55e..d9f59f0c0e 100644 --- a/engines/lab/lab.h +++ b/engines/lab/lab.h @@ -103,13 +103,6 @@ public: LargeSet *_conditions, *_roomsFound; - // timing.cpp - void getTime(uint32 *secs, uint32 *micros); - void addCurTime(uint32 sec, uint32 micros, uint32 *timeSec, uint32 *timeMicros); - void anyTimeDiff(uint32 sec1, uint32 micros1, uint32 sec2, uint32 micros2, uint32 *diffSecs, uint32 *diffMicros); - void timeDiff(uint32 sec, uint32 micros, uint32 *diffSec, uint32 *diffMicros); - void waitForTime(uint32 sec, uint32 micros); - private: uint32 _extraGameFeatures; bool _interfaceOff; @@ -117,7 +110,6 @@ private: bool _quitLab; bool _mainDisplay; // timing.cpp - void microDelay(uint32 secs, uint32 micros); public: EventManager *_event; diff --git a/engines/lab/module.mk b/engines/lab/module.mk index 0d05de275e..a0dcfde512 100644 --- a/engines/lab/module.mk +++ b/engines/lab/module.mk @@ -18,7 +18,6 @@ MODULE_OBJS := \ savegame.o \ special.o \ tilepuzzle.o \ - timing.o \ transitions.o \ utils.o diff --git a/engines/lab/processroom.cpp b/engines/lab/processroom.cpp index 49287aeeba..b997942879 100644 --- a/engines/lab/processroom.cpp +++ b/engines/lab/processroom.cpp @@ -425,14 +425,14 @@ void LabEngine::doActions(Action *actionList, CloseDataPtr *closePtrList) { case WAITSECS: { uint32 startSecs, startMicros, curSecs, curMicros; - addCurTime(actionList->_param1, 0, &startSecs, &startMicros); + _utils->addCurTime(actionList->_param1, 0, &startSecs, &startMicros); _graphics->screenUpdate(); while (1) { _music->updateMusic(); _anim->diffNextFrame(); - getTime(&curSecs, &curMicros); + _utils->getTime(&curSecs, &curMicros); if ((curSecs > startSecs) || ((curSecs == startSecs) && (curMicros >= startMicros))) break; diff --git a/engines/lab/timing.cpp b/engines/lab/timing.cpp deleted file mode 100644 index 1cbe6d328a..0000000000 --- a/engines/lab/timing.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* 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. - * - */ - -/* - * This code is based on Labyrinth of Time code with assistance of - * - * Copyright (c) 1993 Terra Nova Development - * Copyright (c) 2004 The Wyrmkeep Entertainment Co. - * - */ - -#include "lab/lab.h" - -namespace Lab { - -/** - * Waits for for Secs seconds and Micros microseconds to pass. - */ -void LabEngine::microDelay(uint32 secs, uint32 micros) { - uint32 waitSecs, waitMicros; - addCurTime(secs, micros, &waitSecs, &waitMicros); - - while (1) { - getTime(&secs, µs); - - if ((secs > waitSecs) || ((secs == waitSecs) && (micros >= waitMicros))) - return; - - g_system->delayMillis(10); - } -} - -/** - * Gets the current system time. - */ -void LabEngine::getTime(uint32 *secs, uint32 *micros) { - uint32 t = g_system->getMillis(); - - *secs = t / 1000; - *micros = t % 1000; -} - -/** - * Adds seconds and microseconds to current time to get a new time. - */ -void LabEngine::addCurTime(uint32 sec, uint32 micros, uint32 *timeSec, uint32 *timeMicros) { - getTime(timeSec, timeMicros); - - (*timeSec) += sec; - (*timeMicros) += micros; - - if (*timeMicros >= ONESECOND) { - (*timeSec)++; - (*timeMicros) -= ONESECOND; - } -} - -/** - * Finds the difference between time1 and time2. If time1 is later than - * time2, returns 0. - */ -void LabEngine::anyTimeDiff(uint32 sec1, uint32 micros1, uint32 sec2, uint32 micros2, uint32 *diffSecs, uint32 *diffMicros) { - *diffSecs = 0; - *diffMicros = 0; - - if (sec1 > sec2) - return; - else if ((sec1 == sec2) && (micros1 >= micros2)) - return; - - if (micros1 > micros2) { - *diffSecs = sec2 - sec1 - 1; - *diffMicros = (ONESECOND - micros1) + micros2; - } else { - *diffSecs = sec2 - sec1; - *diffMicros = micros2 - micros1; - } -} - -/** - * Finds the difference between the current time, and a future time. Returns - * 0 if the future time is actually before the current time. - */ -void LabEngine::timeDiff(uint32 sec, uint32 micros, uint32 *diffSec, uint32 *diffMicros) { - uint32 curSec, curMicros; - getTime(&curSec, &curMicros); - anyTimeDiff(curSec, curMicros, sec, micros, diffSec, diffMicros); -} - -/** - * Waits for a specified time to occur. - */ -void LabEngine::waitForTime(uint32 sec, uint32 micros) { - uint32 curSec, curMicros; - getTime(&curSec, &curMicros); - - if (curSec > sec) - return; - else if ((curSec == sec) && (curMicros >= micros)) - return; - - if (curMicros > micros) - microDelay(sec - curSec - 1, (ONESECOND - curMicros) + micros - 1); - else - microDelay(sec - curSec, micros - curMicros - 1); -} - -} // End of namespace Lab diff --git a/engines/lab/utils.cpp b/engines/lab/utils.cpp index f9a6656174..1ac083790e 100644 --- a/engines/lab/utils.cpp +++ b/engines/lab/utils.cpp @@ -433,7 +433,7 @@ void Utils::setBytesPerRow(int num) { uint16 Utils::getRandom(uint16 max) { uint32 secs, micros; - _vm->getTime(&secs, µs); + getTime(&secs, µs); return ((micros + secs) % max); } @@ -442,4 +442,95 @@ void Utils::readBlock(void *Buffer, uint32 Size, byte **File) { (*File) += Size; } +/** + * Waits for for Secs seconds and Micros microseconds to pass. + */ +void Utils::microDelay(uint32 secs, uint32 micros) { + uint32 waitSecs, waitMicros; + addCurTime(secs, micros, &waitSecs, &waitMicros); + + while (1) { + getTime(&secs, µs); + + if ((secs > waitSecs) || ((secs == waitSecs) && (micros >= waitMicros))) + return; + + g_system->delayMillis(10); + } +} + +/** + * Gets the current system time. + */ +void Utils::getTime(uint32 *secs, uint32 *micros) { + uint32 t = g_system->getMillis(); + + *secs = t / 1000; + *micros = t % 1000; +} + +/** + * Adds seconds and microseconds to current time to get a new time. + */ +void Utils::addCurTime(uint32 sec, uint32 micros, uint32 *timeSec, uint32 *timeMicros) { + getTime(timeSec, timeMicros); + + (*timeSec) += sec; + (*timeMicros) += micros; + + if (*timeMicros >= ONESECOND) { + (*timeSec)++; + (*timeMicros) -= ONESECOND; + } +} + +/** + * Finds the difference between time1 and time2. If time1 is later than + * time2, returns 0. + */ +void Utils::anyTimeDiff(uint32 sec1, uint32 micros1, uint32 sec2, uint32 micros2, uint32 *diffSecs, uint32 *diffMicros) { + *diffSecs = 0; + *diffMicros = 0; + + if (sec1 > sec2) + return; + else if ((sec1 == sec2) && (micros1 >= micros2)) + return; + + if (micros1 > micros2) { + *diffSecs = sec2 - sec1 - 1; + *diffMicros = (ONESECOND - micros1) + micros2; + } else { + *diffSecs = sec2 - sec1; + *diffMicros = micros2 - micros1; + } +} + +/** + * Finds the difference between the current time, and a future time. Returns + * 0 if the future time is actually before the current time. + */ +void Utils::timeDiff(uint32 sec, uint32 micros, uint32 *diffSec, uint32 *diffMicros) { + uint32 curSec, curMicros; + getTime(&curSec, &curMicros); + anyTimeDiff(curSec, curMicros, sec, micros, diffSec, diffMicros); +} + +/** + * Waits for a specified time to occur. + */ +void Utils::waitForTime(uint32 sec, uint32 micros) { + uint32 curSec, curMicros; + getTime(&curSec, &curMicros); + + if (curSec > sec) + return; + else if ((curSec == sec) && (curMicros >= micros)) + return; + + if (curMicros > micros) + microDelay(sec - curSec - 1, (ONESECOND - curMicros) + micros - 1); + else + microDelay(sec - curSec, micros - curMicros - 1); +} } // End of namespace Lab diff --git a/engines/lab/utils.h b/engines/lab/utils.h index da1f89af7d..9d6e1872d4 100644 --- a/engines/lab/utils.h +++ b/engines/lab/utils.h @@ -43,6 +43,7 @@ private: void VUnDiffByteByte(byte *Dest, byte *diff, uint16 bytesperrow); void VUnDiffByteWord(uint16 *Dest, uint16 *diff, uint16 bytesperrow); void VUnDiffByteLong(uint32 *Dest, uint32 *diff, uint16 bytesperrow); + void microDelay(uint32 secs, uint32 micros); public: Utils(LabEngine *vm); @@ -61,6 +62,11 @@ public: void setBytesPerRow(int num); uint16 getRandom(uint16 max); void readBlock(void *Buffer, uint32 Size, byte **File); + void addCurTime(uint32 sec, uint32 micros, uint32 *timeSec, uint32 *timeMicros); + void getTime(uint32 *secs, uint32 *micros); + void waitForTime(uint32 sec, uint32 micros); + void anyTimeDiff(uint32 sec1, uint32 micros1, uint32 sec2, uint32 micros2, uint32 *diffSecs, uint32 *diffMicros); + void timeDiff(uint32 sec, uint32 micros, uint32 *diffSec, uint32 *diffMicros); }; -- cgit v1.2.3