LAB: Get rid of getTime(), readBlock() and getRandom()

Also, simplify some time-related functions
This commit is contained in:
Filippos Karapetis 2015-12-13 04:12:29 +02:00 committed by Willem Jan Palenstijn
parent a01f068969
commit 1b0a7db0b6
8 changed files with 48 additions and 74 deletions

View file

@ -141,18 +141,20 @@ void Anim::diffNextFrame(bool onlyDiffData) {
switch (_header) {
case 8:
_vm->_utils->readBlock(_diffPalette, _size, &_diffFile);
memcpy(_diffPalette, _diffFile, _size);
_diffFile += _size;
_isPal = true;
break;
case 10:
_rawDiffBM._planes[_curBit] = _diffFile;
if (onlyDiffData)
if (onlyDiffData) {
_diffFile += _size;
else
_vm->_utils->readBlock(DrawBitMap->_planes[_curBit], _size, &_diffFile);
} else {
memcpy(DrawBitMap->_planes[_curBit], _diffFile, _size);
_diffFile += _size;
}
_curBit++;
break;

View file

@ -609,7 +609,9 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo
_followCrumbsFast = (code == 'r' || code == 'R');
_isCrumbTurning = false;
_isCrumbWaiting = false;
_utils->getTime(&_crumbSecs, &_crumbMicros);
uint32 t = g_system->getMillis();
_crumbSecs = t / 1000;
_crumbMicros = t % 1000;
if (_alternate) {
eatMessages();
@ -930,7 +932,9 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo
_followCrumbsFast = false;
_isCrumbTurning = false;
_isCrumbWaiting = false;
_utils->getTime(&_crumbSecs, &_crumbMicros);
uint32 t = g_system->getMillis();
_crumbSecs = t / 1000;
_crumbMicros = t % 1000;
eatMessages();
_alternate = false;

View file

@ -132,7 +132,9 @@ void Intro::doPictText(const char *filename, TextFont *msgFont, bool isScreen) {
return;
}
_vm->_utils->getTime(&lastSecs, &lastMicros);
uint32 t = g_system->getMillis();
lastSecs = t / 1000;
lastMicros = t % 1000;
}
msg = _vm->getMsg();
@ -140,7 +142,10 @@ void Intro::doPictText(const char *filename, TextFont *msgFont, bool isScreen) {
if (msg == NULL) {
_vm->_music->updateMusic();
_vm->_anim->diffNextFrame();
_vm->_utils->getTime(&secs, &micros);
uint32 t = g_system->getMillis();
secs = t / 1000;
micros = t % 1000;
_vm->_utils->anyTimeDiff(lastSecs, lastMicros, secs, micros, &secs, &micros);
if (secs > timeDelay) {

View file

@ -51,7 +51,7 @@ namespace Lab {
LabEngine *g_lab;
LabEngine::LabEngine(OSystem *syst, const ADGameDescription *gameDesc)
: Engine(syst), _gameDescription(gameDesc), _extraGameFeatures(0) {
: Engine(syst), _gameDescription(gameDesc), _extraGameFeatures(0), _rnd("lab") {
g_lab = this;
_lastWaitTOFTicks = 0;

View file

@ -32,6 +32,7 @@
#define LAB_H
#include "common/system.h"
#include "common/random.h"
#include "common/rect.h"
#include "engines/engine.h"
@ -114,6 +115,8 @@ private:
InventoryData *_inventory;
MapData *_maps;
Common::RandomSource _rnd;
public:
bool _alternate;
bool _droppingCrumbs;

View file

@ -370,7 +370,7 @@ void LabEngine::doActions(Action *actionList, CloseDataPtr *closePtrList) {
case SHOWMESSAGES: {
char **str = (char **)actionList->_data;
_graphics->_doNotDrawMessage = false;
_graphics->drawMessage(str[_utils->getRandom(actionList->_param1)]);
_graphics->drawMessage(str[_rnd.getRandomNumber(actionList->_param1)]);
_graphics->_doNotDrawMessage = true;
}
break;
@ -424,18 +424,13 @@ void LabEngine::doActions(Action *actionList, CloseDataPtr *closePtrList) {
break;
case WAITSECS: {
uint32 startSecs, startMicros, curSecs, curMicros;
_utils->addCurTime(actionList->_param1, 0, &startSecs, &startMicros);
uint32 targetMillis = g_system->getMillis() + actionList->_param1 * 1000;
_graphics->screenUpdate();
while (1) {
while (g_system->getMillis() < targetMillis) {
_music->updateMusic();
_anim->diffNextFrame();
_utils->getTime(&curSecs, &curMicros);
if ((curSecs > startSecs) || ((curSecs == startSecs) && (curMicros >= startMicros)))
break;
}
}
break;

View file

@ -427,56 +427,13 @@ void Utils::setBytesPerRow(int num) {
_dataBytesPerRow = num;
}
/**
* Generates a random number.
*/
uint16 Utils::getRandom(uint16 max) {
uint32 secs, micros;
getTime(&secs, &micros);
return ((micros + secs) % max);
}
void Utils::readBlock(void *Buffer, uint32 Size, byte **File) {
memcpy(Buffer, *File, (size_t)Size);
(*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, &micros);
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;
uint32 t = g_system->getMillis();
*timeSec = (t / 1000) + sec;
*timeMicros = (t % 1000) + micros;
if (*timeMicros >= ONESECOND) {
(*timeSec)++;
@ -511,21 +468,32 @@ void Utils::anyTimeDiff(uint32 sec1, uint32 micros1, uint32 sec2, uint32 micros2
* 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);
uint32 t = g_system->getMillis();
uint32 curSec = t / 1000;
uint32 curMicros = t % 1000;
anyTimeDiff(curSec, curMicros, sec, micros, diffSec, diffMicros);
}
/**
* Waits for Secs seconds and Micros microseconds to pass.
*/
void Utils::microDelay(uint32 secs, uint32 micros) {
uint32 targetMillis = g_system->getMillis() + secs * 1000 + micros;
while (g_system->getMillis() < targetMillis)
g_system->delayMillis(10);
}
/**
* Waits for a specified time to occur.
*/
void Utils::waitForTime(uint32 sec, uint32 micros) {
uint32 curSec, curMicros;
getTime(&curSec, &curMicros);
uint32 targetMillis = sec * 1000 + micros;
uint32 t = g_system->getMillis();
uint32 curSec = t / 1000;
uint32 curMicros = t % 1000;
if (curSec > sec)
return;
else if ((curSec == sec) && (curMicros >= micros))
if (t >= targetMillis)
return;
if (curMicros > micros)

View file

@ -60,10 +60,7 @@ public:
void runLengthDecode(byte *dest, byte *source);
void VRunLengthDecode(byte *dest, byte *source, uint16 bytesPerRow);
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);