Skip to content

Commit

Permalink
SHERLOCK: Added lightweight TsAGE object for handling logo animations
Browse files Browse the repository at this point in the history
  • Loading branch information
dreammaster committed May 31, 2015
1 parent 6796444 commit 59d81c0
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 10 deletions.
261 changes: 252 additions & 9 deletions engines/sherlock/scalpel/tsage/logo.cpp
Expand Up @@ -28,6 +28,216 @@ namespace Sherlock {
namespace Scalpel {
namespace TsAGE {

TLib *Visage::_tLib;

Visage::Visage() {
_resNum = -1;
_rlbNum = -1;
_stream = nullptr;
}

void Visage::setVisage(int resNum, int rlbNum) {
if ((_resNum != resNum) || (_rlbNum != rlbNum)) {
_resNum = resNum;
_rlbNum = rlbNum;
delete _stream;

// Games after Ringworld have an extra indirection via the visage index file
Common::SeekableReadStream *stream = _tLib->getResource(RES_VISAGE, resNum, 9999);
if (rlbNum == 0)
rlbNum = 1;

// Check how many slots there are
uint16 count = stream->readUint16LE();
if (rlbNum > count)
rlbNum = count;

// Get the flags/rlbNum to use
stream->seek((rlbNum - 1) * 4 + 2);
uint32 v = stream->readUint32LE();
int flags = v >> 30;

if (flags & 3) {
rlbNum = (int)(v & 0xff);
}
assert((flags & 3) == 0);
delete stream;

_stream = _tLib->getResource(RES_VISAGE, resNum, rlbNum);
}
}

Visage::~Visage() {
delete _stream;
}

void Visage::getFrame(ObjectSurface &s, int frameNum) {
_stream->seek(0);
int numFrames = _stream->readUint16LE();
if (frameNum > numFrames)
frameNum = numFrames;
if (frameNum > 0)
--frameNum;

_stream->seek(frameNum * 4 + 2);
int offset = _stream->readUint32LE();
_stream->seek(offset);

surfaceFromRes(s);
}

int Visage::getFrameCount() const {
_stream->seek(0);
return _stream->readUint16LE();
}

bool Visage::isLoaded() const {
return _stream != nullptr;
}

void Visage::surfaceFromRes(ObjectSurface &s) {
int frameWidth = _stream->readUint16LE();
int frameHeight = _stream->readUint16LE();
Common::Rect r(0, 0, frameWidth, frameHeight);
s.create(r.width(), r.height());

s._centroid.x = _stream->readSint16LE();
s._centroid.y = _stream->readSint16LE();

_stream->skip(1);
byte flags = _stream->readByte();
bool rleEncoded = (flags & 2) != 0;

byte *destP = (byte *)s.getPixels();

if (!rleEncoded) {
_stream->read(destP, r.width() * r.height());
} else {
Common::fill(destP, destP + (r.width() * r.height()), 0xff);

for (int yp = 0; yp < r.height(); ++yp) {
int width = r.width();
destP = (byte *)s.getBasePtr(0, yp);

while (width > 0) {
uint8 controlVal = _stream->readByte();
if ((controlVal & 0x80) == 0) {
// Copy specified number of bytes
_stream->read(destP, controlVal);
width -= controlVal;
destP += controlVal;
} else if ((controlVal & 0x40) == 0) {
// Skip a specified number of output pixels
destP += controlVal & 0x3f;
width -= controlVal & 0x3f;
} else {
// Copy a specified pixel a given number of times
controlVal &= 0x3f;
int pixel = _stream->readByte();

Common::fill(destP, destP + controlVal, pixel);
destP += controlVal;
width -= controlVal;
}
}
assert(width == 0);
}
}
}

/*--------------------------------------------------------------------------*/

ScalpelEngine *Object::_vm;

Object::Object() {
_vm = nullptr;
_animMode = ANIM_MODE_NONE;
_frame = 0;
_numFrames = 0;
_frameChange = 0;
}

void Object::setVisage(int visage, int strip) {
_visage.setVisage(visage, strip);
}

void Object::setAnimMode(AnimationMode mode) {
_animMode = mode;
_finished = false;

_updateStartFrame = _vm->_events->getFrameCounter();
if (_numFrames)
_updateStartFrame += 60 / _numFrames;
_frameChange = 1;
}

void Object::update() {
Screen &screen = *_vm->_screen;

if (_visage.isLoaded()) {
switch (_animMode) {
case ANIM_MODE_5:
if (_frame < _visage.getFrameCount())
_frame = changeFrame();
else
_finished = true;
break;

default:
break;
}

// Erase previous frame, if any
if (!_oldBounds.isEmpty())
screen.blitFrom(screen._backBuffer1, Common::Point(_oldBounds.left, _oldBounds.top), _oldBounds);

// Get the new frame
ObjectSurface s;
_visage.getFrame(s, _frame);

// Display the frame
_oldBounds = Common::Rect(_position.x, _position.y, _position.x + s.w(), _position.y + s.h());
_oldBounds.translate(-s._centroid.x, -s._centroid.y);
screen.transBlitFrom(s, Common::Point(_oldBounds.left, _oldBounds.top));
}
}

int Object::changeFrame() {
int frameNum = _frame;
uint32 currentFrame = _vm->_events->getFrameCounter();

if (_updateStartFrame <= currentFrame) {
if (_numFrames > 0) {
int v = 60 / _numFrames;
_updateStartFrame = currentFrame + v;

frameNum = getNewFrame();
}
}

return frameNum;
}

int Object::getNewFrame() {
int frameNum = _frame + _frameChange;

if (_frameChange > 0) {
if (frameNum > _visage.getFrameCount()) {
frameNum = 1;
}
} else if (frameNum < 1) {
frameNum = _visage.getFrameCount();
}

return frameNum;
}

bool Object::isAnimEnded() const {
return _finished;
}

/*----------------------------------------------------------------*/

bool Logo::show(ScalpelEngine *vm) {
Events &events = *vm->_events;
Logo *logo = new Logo(vm);
Expand All @@ -39,6 +249,9 @@ bool Logo::show(ScalpelEngine *vm) {
events.wait(2);
events.setButtonState();

for (int idx = 0; idx < 4; ++idx)
logo->_objects[idx].update();

interrupted = vm->shouldQuit() || events.kbHit() || events._pressed;
if (interrupted) {
events.clearEvents();
Expand All @@ -50,10 +263,16 @@ bool Logo::show(ScalpelEngine *vm) {
return !interrupted;
}

Logo::Logo(ScalpelEngine *vm) : _vm(vm), _lib("sf3.rlb"), _surface(vm->_screen->w(), vm->_screen->h()) {
// Initialize frame counter
Logo::Logo(ScalpelEngine *vm) : _vm(vm), _lib("sf3.rlb") {
Object::_vm = vm;
Visage::_tLib = &_lib;

// Initialize counter
_counter = 0;

// Save a copy of the original palette
_vm->_screen->getPalette(_originalPalette);

// Set up the palettes
Common::fill(&_palette1[0], &_palette1[PALETTE_SIZE], 0);
Common::fill(&_palette1[0], &_palette2[PALETTE_SIZE], 0);
Expand All @@ -67,18 +286,42 @@ Logo::Logo(ScalpelEngine *vm) : _vm(vm), _lib("sf3.rlb"), _surface(vm->_screen->
_lib.getPalette(_palette3, 14);
}

Logo::~Logo() {
// Restore the original palette
_vm->_screen->setPalette(_originalPalette);
}

bool Logo::finished() const {
return false;
return _counter >= 4;
}

void Logo::nextFrame() {

switch (_counter++) {
case 0:
// Load the background and fade it in
loadBackground();
fade(_palette1);
break;

case 1:
// First half of square, circle, and triangle arranging themselves
_objects[0].setVisage(16, 1);
_objects[0]._frame = 1;
_objects[0]._position = Common::Point(169, 107);
_objects[0]._numFrames = 7;
_objects[0].setAnimMode(ANIM_MODE_5);
break;

case 2:
// Keep waiting until first animation ends
if (!_objects[0].isAnimEnded())
--_counter;
break;

case 3:
// Keep waiting until second animation of shapes ordering themselves ends
return;

default:
break;
}
Expand All @@ -92,10 +335,10 @@ void Logo::loadBackground() {
Common::SeekableReadStream *stream = _lib.getResource(RES_BITMAP, 10, idx);

// Load it onto the surface
Common::Point pt((idx / 2) * (_surface.w() / 2), (idx % 2) * (_surface.h() / 2));
for (int y = 0; y < (_surface.h() / 2); ++y, ++pt.y) {
byte *pDest = (byte *)_surface.getBasePtr(pt.x, pt.y);
stream->read(pDest, _surface.w() / 2);
Common::Point pt((idx / 2) * (SHERLOCK_SCREEN_WIDTH / 2), (idx % 2) * (SHERLOCK_SCREEN_HEIGHT / 2));
for (int y = 0; y < (SHERLOCK_SCREEN_HEIGHT / 2); ++y, ++pt.y) {
byte *pDest = (byte *)screen._backBuffer1.getBasePtr(pt.x, pt.y);
stream->read(pDest, SHERLOCK_SCREEN_WIDTH / 2);
}

// _backgroundBounds = Rect(0, 0, READ_LE_UINT16(data), READ_LE_UINT16(data + 2));
Expand All @@ -108,7 +351,7 @@ void Logo::loadBackground() {
screen.setPalette(palette);

// Copy the surface to the screen
screen.blitFrom(_surface);
screen.blitFrom(screen._backBuffer1);
}

void Logo::fade(const byte palette[PALETTE_SIZE]) {
Expand Down

0 comments on commit 59d81c0

Please sign in to comment.