scummvm/engines/lab/anim.cpp

348 lines
7.9 KiB
C++
Raw Normal View History

2015-12-01 20:10:42 +01:00
/* 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"
#include "lab/anim.h"
#include "lab/dispman.h"
#include "lab/eventman.h"
#include "lab/music.h"
#include "lab/utils.h"
2015-12-01 20:10:42 +01:00
namespace Lab {
Anim::Anim(LabEngine *vm) : _vm(vm) {
_lastBlockHeader = 0;
2015-12-02 00:34:51 +01:00
_numChunks = 1;
_headerdata._width = 0;
_headerdata._height = 0;
_headerdata._fps = 0;
_headerdata._flags = 0;
_delayMicros = 0;
_continuous = false;
_isPlaying = false;
_isAnim = false;
_isPal = false;
_noPalChange = false;
_donePal = false;
_frameNum = 0;
_playOnce = false;
_diffFile = nullptr;
_diffFileStart = 0;
2015-12-02 00:34:51 +01:00
_size = 0;
_scrollScreenBuffer = nullptr;
2015-12-02 00:34:51 +01:00
_waitForEffect = false;
_stopPlayingEnd = false;
_sampleSpeed = 0;
_doBlack = false;
2015-12-01 20:10:42 +01:00
for (int i = 0; i < 3 * 256; i++)
2015-12-02 00:34:51 +01:00
_diffPalette[i] = 0;
2015-12-24 16:51:02 +01:00
2015-12-24 16:53:32 +01:00
_outputBuffer = nullptr; // output to screen
2015-12-01 20:10:42 +01:00
}
Anim::~Anim() {
delete[] _vm->_anim->_scrollScreenBuffer;
_vm->_anim->_scrollScreenBuffer = nullptr;
}
2015-12-24 16:51:02 +01:00
void Anim::setOutputBuffer(byte *memoryBuffer) {
2015-12-24 16:53:32 +01:00
_outputBuffer = memoryBuffer;
2015-12-24 16:51:02 +01:00
}
uint16 Anim::getDIFFHeight() {
return _headerdata._height;
}
2015-12-24 16:51:02 +01:00
2015-12-08 11:29:23 +02:00
void Anim::diffNextFrame(bool onlyDiffData) {
if (_lastBlockHeader == 65535)
2015-12-08 11:27:34 +01:00
// Already done.
2015-12-01 20:10:42 +01:00
return;
2015-12-24 16:53:32 +01:00
bool drawOnScreen = false;
byte *startOfBuf = _outputBuffer;
int bufPitch = _vm->_graphics->_screenWidth;
if (!startOfBuf) {
startOfBuf = _vm->_graphics->getCurrentDrawingBuffer();
2015-12-24 16:53:32 +01:00
drawOnScreen = true;
}
byte *endOfBuf = startOfBuf + (int)_headerdata._width * _headerdata._height;
2015-12-01 20:10:42 +01:00
2015-12-24 16:02:22 +01:00
int curBit = 0;
2015-12-01 20:10:42 +01:00
while (1) {
byte *buf = startOfBuf + 0x10000 * curBit;
2015-12-24 16:02:22 +01:00
if (buf >= endOfBuf) {
2015-12-08 11:29:23 +02:00
if (!onlyDiffData) {
2015-12-02 00:34:51 +01:00
if (_headerdata._fps) {
2015-12-23 18:56:35 +01:00
uint32 targetMillis = _vm->_system->getMillis() + _delayMicros;
while (_vm->_system->getMillis() < targetMillis)
_vm->_system->delayMillis(10);
2015-12-01 20:10:42 +01:00
}
2015-12-02 00:34:51 +01:00
if (_isPal && !_noPalChange) {
2015-12-06 14:36:49 +01:00
_vm->_graphics->setPalette(_diffPalette, 256);
2015-12-02 00:34:51 +01:00
_isPal = false;
2015-12-01 20:10:42 +01:00
}
2015-12-02 00:34:51 +01:00
_donePal = true;
2015-12-01 20:10:42 +01:00
}
2015-12-08 11:29:23 +02:00
if (_isPal && !_noPalChange && !onlyDiffData && !_donePal) {
2015-12-06 14:36:49 +01:00
_vm->_graphics->setPalette(_diffPalette, 256);
2015-12-02 00:34:51 +01:00
_isPal = false;
2015-12-01 20:10:42 +01:00
}
2015-12-02 00:34:51 +01:00
_donePal = false;
2015-12-01 20:10:42 +01:00
2015-12-02 00:34:51 +01:00
_frameNum++;
2015-12-01 20:10:42 +01:00
if ((_frameNum == 1) && (_continuous || !_playOnce))
_diffFileStart = _diffFile->pos();
2015-12-01 20:10:42 +01:00
2015-12-02 00:34:51 +01:00
_isAnim = (_frameNum >= 3) && (!_playOnce);
2015-12-01 20:10:42 +01:00
if (drawOnScreen)
_vm->_graphics->screenUpdate();
2015-12-01 20:10:42 +01:00
2015-12-08 11:27:34 +01:00
// done with the next frame.
return;
2015-12-01 20:10:42 +01:00
}
2015-12-27 21:12:41 +02:00
_vm->updateEvents();
_lastBlockHeader = _diffFile->readUint32LE();
_size = _diffFile->readUint32LE();
2015-12-01 20:10:42 +01:00
uint32 curPos = 0;
2015-12-01 20:10:42 +01:00
switch (_lastBlockHeader) {
case 8:
_diffFile->read(_diffPalette, _size);
2015-12-02 00:34:51 +01:00
_isPal = true;
2015-12-01 20:10:42 +01:00
break;
case 10:
if (onlyDiffData) {
2015-12-24 16:02:22 +01:00
if (curBit > 0)
error("diffNextFrame: attempt to read screen to non-zero plane (%d)", curBit);
delete[] _scrollScreenBuffer;
_scrollScreenBuffer = new byte[_headerdata._width * _headerdata._height];
_diffFile->read(_scrollScreenBuffer, _size);
} else {
2015-12-24 16:02:22 +01:00
_diffFile->read(buf, _size);
}
2015-12-24 16:02:22 +01:00
curBit++;
2015-12-01 20:10:42 +01:00
break;
case 11:
curPos = _diffFile->pos();
_diffFile->skip(4);
2015-12-24 16:02:22 +01:00
_vm->_utils->runLengthDecode(buf, _diffFile);
curBit++;
_diffFile->seek(curPos + _size, SEEK_SET);
2015-12-01 20:10:42 +01:00
break;
case 12:
curPos = _diffFile->pos();
_diffFile->skip(4);
_vm->_utils->verticalRunLengthDecode(buf, _diffFile, bufPitch);
2015-12-24 16:02:22 +01:00
curBit++;
_diffFile->seek(curPos + _size, SEEK_SET);
2015-12-01 20:10:42 +01:00
break;
case 20:
curPos = _diffFile->pos();
_vm->_utils->unDiff(buf, buf, _diffFile, bufPitch, false);
2015-12-24 16:02:22 +01:00
curBit++;
_diffFile->seek(curPos + _size, SEEK_SET);
2015-12-01 20:10:42 +01:00
break;
case 21:
curPos = _diffFile->pos();
_vm->_utils->unDiff(buf, buf, _diffFile, bufPitch, true);
2015-12-24 16:02:22 +01:00
curBit++;
_diffFile->seek(curPos + _size, SEEK_SET);
2015-12-01 20:10:42 +01:00
break;
case 25:
case 26:
2015-12-24 16:02:22 +01:00
curBit++;
2015-12-01 20:10:42 +01:00
break;
case 30:
case 31:
2015-12-02 00:34:51 +01:00
if (_waitForEffect) {
2015-12-01 20:10:42 +01:00
while (_vm->_music->isSoundEffectActive()) {
2015-12-27 21:12:41 +02:00
_vm->updateEvents();
2015-12-01 20:10:42 +01:00
_vm->waitTOF();
}
}
_size -= 8;
2015-12-01 20:10:42 +01:00
_diffFile->skip(4);
_sampleSpeed = _diffFile->readUint16LE();
_diffFile->skip(2);
2015-12-01 20:10:42 +01:00
// Sound effects embedded in animations are started here. These are
// usually animation-specific, like door opening sounds, and are not looped
_vm->_music->playSoundEffect(_sampleSpeed, _size, false, _diffFile);
2015-12-01 20:10:42 +01:00
break;
case 65535:
2015-12-02 00:34:51 +01:00
if ((_frameNum == 1) || _playOnce || _stopPlayingEnd) {
bool didTOF = false;
2015-12-01 20:10:42 +01:00
2015-12-02 00:34:51 +01:00
if (_waitForEffect) {
2015-12-01 20:10:42 +01:00
while (_vm->_music->isSoundEffectActive()) {
2015-12-27 21:12:41 +02:00
_vm->updateEvents();
2015-12-01 20:10:42 +01:00
_vm->waitTOF();
if (drawOnScreen)
didTOF = true;
2015-12-01 20:10:42 +01:00
}
}
2015-12-02 00:34:51 +01:00
_isPlaying = false;
2015-12-01 20:10:42 +01:00
if (!didTOF)
_vm->_graphics->screenUpdate();
2015-12-01 20:10:42 +01:00
return;
}
2015-12-08 11:27:34 +01:00
// Random frame number so it never gets back to 2
_frameNum = 4;
_diffFile->seek(_diffFileStart, SEEK_SET);
2015-12-01 20:10:42 +01:00
break;
default:
_diffFile->skip(_size);
2015-12-01 20:10:42 +01:00
break;
}
}
}
2015-12-10 03:15:13 +02:00
void Anim::stopDiff() {
if (_isPlaying && _isAnim)
_vm->_graphics->blackScreen();
}
void Anim::stopDiffEnd() {
if (!_isPlaying)
return;
_stopPlayingEnd = true;
while (_isPlaying) {
2015-12-27 21:12:41 +02:00
_vm->updateEvents();
diffNextFrame();
2015-12-10 03:15:13 +02:00
}
}
void Anim::readDiff(Common::File *diffFile, bool playOnce, bool onlyDiffData) {
2015-12-10 03:15:13 +02:00
_playOnce = playOnce;
_delayMicros = 0;
2015-12-02 00:34:51 +01:00
_frameNum = 0;
2015-12-08 09:19:00 +01:00
_numChunks = 1;
_donePal = false;
2015-12-02 00:34:51 +01:00
_stopPlayingEnd = false;
2015-12-08 09:19:00 +01:00
_isPlaying = true;
2015-12-02 00:34:51 +01:00
if (_doBlack) {
_doBlack = false;
_vm->_graphics->blackScreen();
2015-12-01 20:10:42 +01:00
}
_diffFile = diffFile;
2015-12-01 20:10:42 +01:00
2015-12-02 00:34:51 +01:00
_continuous = false;
2015-12-01 20:10:42 +01:00
if (!_diffFile)
return;
uint32 magicBytes = _diffFile->readUint32LE();
if (magicBytes != 1219009121) {
2015-12-02 00:34:51 +01:00
_isPlaying = false;
2015-12-01 20:10:42 +01:00
return;
}
uint32 signature3 = _diffFile->readUint32LE();
_size = _diffFile->readUint32LE();
2015-12-01 20:10:42 +01:00
if (signature3 != 0)
return;
// sizeof(headerdata) != 18, but the padding might be at the end
// 2 bytes, version, unused.
_diffFile->skip(2);
_headerdata._width = _diffFile->readUint16LE();
_headerdata._height = _diffFile->readUint16LE();
// 1 byte, depth, unused
_diffFile->skip(1);
_headerdata._fps = _diffFile->readByte();
// HACK: The original game defines a 1 second delay when changing screens, which is
// very annoying. We first removed the delay, but it looked wrong when changing screens
// as it was possible to see that something was displayed, without being able to tell
// what it was. A shorter delay (150ms) makes it acceptable during gameplay and
// readable. The big question is: do we need that message?
2015-12-23 18:56:35 +01:00
_vm->_system->delayMillis(150);
if (_headerdata._fps == 1)
_headerdata._fps = 0;
// 4 + 2 bytes, buffer size and machine, unused
_diffFile->skip(6);
_headerdata._flags = _diffFile->readUint32LE();
2015-12-02 00:34:51 +01:00
_diffFile->skip(_size - 18);
2015-12-02 00:34:51 +01:00
_continuous = CONTINUOUS & _headerdata._flags;
_vm->_utils->setBytesPerRow(_headerdata._width);
2015-12-02 00:34:51 +01:00
delete[] _scrollScreenBuffer;
_scrollScreenBuffer = nullptr;
2015-12-01 20:10:42 +01:00
2015-12-02 00:34:51 +01:00
if (_headerdata._fps)
2015-12-13 20:27:34 +02:00
_delayMicros = 1000 / _headerdata._fps;
2015-12-01 20:10:42 +01:00
_lastBlockHeader = signature3;
2015-12-02 00:34:51 +01:00
if (_playOnce) {
while (_lastBlockHeader != 65535)
2015-12-08 11:29:23 +02:00
diffNextFrame(onlyDiffData);
} else
2015-12-10 03:15:13 +02:00
diffNextFrame(onlyDiffData);
2015-12-01 20:10:42 +01:00
}
} // End of namespace Lab