diff options
author | Matthew Hoops | 2011-09-19 19:07:11 -0400 |
---|---|---|
committer | Matthew Hoops | 2011-09-19 19:07:11 -0400 |
commit | 6ed2679cf1442cb647351792c97b126195a649e7 (patch) | |
tree | 1473db9cb7bcb5d3142ed805ccf3cc91d484fe6f | |
parent | 8f6e6030999e7263062461eaf6e0fe378a3892ba (diff) | |
download | scummvm-rg350-6ed2679cf1442cb647351792c97b126195a649e7.tar.gz scummvm-rg350-6ed2679cf1442cb647351792c97b126195a649e7.tar.bz2 scummvm-rg350-6ed2679cf1442cb647351792c97b126195a649e7.zip |
PEGASUS: Add the Surface classes
Renamed from GWorld; I'm trying to distance us from QuickDraw as much as possible :P
-rw-r--r-- | engines/pegasus/elements.cpp | 172 | ||||
-rw-r--r-- | engines/pegasus/elements.h | 60 | ||||
-rw-r--r-- | engines/pegasus/graphics.cpp | 5 | ||||
-rw-r--r-- | engines/pegasus/module.mk | 1 | ||||
-rwxr-xr-x | engines/pegasus/surface.cpp | 221 | ||||
-rwxr-xr-x | engines/pegasus/surface.h | 119 |
6 files changed, 573 insertions, 5 deletions
diff --git a/engines/pegasus/elements.cpp b/engines/pegasus/elements.cpp index f9881e7aaf..faf46f780f 100644 --- a/engines/pegasus/elements.cpp +++ b/engines/pegasus/elements.cpp @@ -28,6 +28,7 @@ #include "pegasus/elements.h" #include "pegasus/graphics.h" +#include "pegasus/surface.h" namespace Pegasus { @@ -70,7 +71,10 @@ void DisplayElement::stopDisplaying() { } void DisplayElement::setBounds(const tCoordType left, const tCoordType top, const tCoordType right, const tCoordType bottom) { - _bounds = Common::Rect(left, top, right, bottom); + _bounds.left = left; + _bounds.top = top; + _bounds.right = right; + _bounds.bottom = bottom; } void DisplayElement::getBounds(Common::Rect &r) const { @@ -354,4 +358,170 @@ bool FrameSequence::isSequenceOpen() const { return _numFrames != 0; } +Sprite::Sprite(const tDisplayElementID id) : DisplayElement(id) { + _numFrames = 0; + _currentFrameNum = 0xffffffff; + _currentFrame = 0; +} + +Sprite::~Sprite() { + discardFrames(); +} + +void Sprite::discardFrames() { + if (!_frameArray.empty()) { + for (uint32 i = 0; i < _numFrames; i++) { + SpriteFrame *frame = _frameArray[i].frame; + frame->_referenceCount--; + if (frame->_referenceCount == 0) + delete frame; + } + + _frameArray.clear(); + _numFrames = 0; + _currentFrame = 0; + _currentFrameNum = 0xffffffff; + setBounds(0, 0, 0, 0); + } +} + +void Sprite::addPICTResourceFrame(const tResIDType pictID, bool transparent, const tCoordType left, const tCoordType top) { + SpriteFrame *frame = new SpriteFrame(); + frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, pictID, transparent); + addFrame(frame, left, top); +} + +uint32 Sprite::addFrame(SpriteFrame *frame, const tCoordType left, const tCoordType top) { + SpriteFrameRec frameRecord; + frameRecord.frame = frame; + frameRecord.frameLeft = left; + frameRecord.frameTop = top; + _frameArray.push_back(frameRecord); + _numFrames++; + frame->_referenceCount++; + + Common::Rect frameBounds; + frame->getSurfaceBounds(frameBounds); + + // 9/3/96 + // BB Should this be + left or - left? + frameBounds.moveTo(_bounds.left + left, _bounds.top + top); + + frameBounds.extend(_bounds); + + if (_bounds != frameBounds) + setBounds(frameBounds); + + return _numFrames - 1; +} + +void Sprite::removeFrame(const uint32 frameNum) { + _frameArray[frameNum].frame->_referenceCount--; + if (_frameArray[frameNum].frame->_referenceCount == 0) + delete _frameArray[frameNum].frame; + + // Calculate the new bounds + Common::Rect frameBounds; + for (uint32 i = 0; i < _numFrames; i++) { + if (i == frameNum) + continue; + + Common::Rect r; + _frameArray[i].frame->getSurfaceBounds(r); + r.translate(_frameArray[i].frameLeft, _frameArray[i].frameTop); + frameBounds.extend(r); + } + + _frameArray.remove_at(frameNum); + + frameBounds.moveTo(_bounds.left, _bounds.top); + setBounds(frameBounds); + + if (_currentFrameNum == frameNum) + triggerRedraw(); + else if (_currentFrameNum != 0xffffffff && _currentFrameNum > frameNum) + --_currentFrameNum; +} + +void Sprite::setCurrentFrameIndex(const int32 frameNum) { + if (frameNum < 0) { + if (_currentFrameNum != 0xffffffff) { + _currentFrameNum = 0xffffffff; + _currentFrame = 0; + triggerRedraw(); + } + } else if (_numFrames > 0) { + uint32 f = frameNum % _numFrames; + if (f != _currentFrameNum) { + _currentFrameNum = f; + _currentFrame = &_frameArray[f]; + triggerRedraw(); + } + } +} + +SpriteFrame *Sprite::getFrame(const int32 index) { + if (index < 0 || (uint32)index >= _numFrames) + return 0; + + return _frameArray[index].frame; +} + +void Sprite::draw(const Common::Rect &r) { + if (_currentFrame) { + Common::Rect frameBounds; + _currentFrame->frame->getSurfaceBounds(frameBounds); + + frameBounds.translate(_bounds.left + _currentFrame->frameLeft, _bounds.top + _currentFrame->frameTop); + Common::Rect r1 = frameBounds.findIntersectingRect(r); + + Common::Rect r2 = frameBounds; + r2.translate(frameBounds.left - _bounds.left - _currentFrame->frameLeft, frameBounds.top - _bounds.top - _currentFrame->frameTop); + + _currentFrame->frame->drawImage(r2, r1); + } +} + +SpriteSequence::SpriteSequence(const tDisplayElementID id, const tDisplayElementID spriteID) : + FrameSequence(id), _sprite(spriteID), _transparent(false) { +} + +void SpriteSequence::openFrameSequence() { + if (!isSequenceOpen()) { + FrameSequence::openFrameSequence(); + + if (isSequenceOpen()) { + uint32 numFrames = getNumFrames(); + + for (uint32 i = 0; i < numFrames; ++i) { + SpriteFrame *frame = new SpriteFrame(); + frame->initFromPICTResource(_resFork, i + 0x80, _transparent); + _sprite.addFrame(frame, 0, 0); + } + + _sprite.setBounds(_bounds); + } + } +} + +void SpriteSequence::closeFrameSequence() { + if (isSequenceOpen()) { + FrameSequence::closeFrameSequence(); + _sprite.discardFrames(); + } +} + +void SpriteSequence::setBounds(const Common::Rect &bounds) { + FrameSequence::setBounds(bounds); + _sprite.setBounds(_bounds); +} + +void SpriteSequence::draw(const Common::Rect &r) { + _sprite.draw(r); +} + +void SpriteSequence::newFrame(const uint16 frame) { + _sprite.setCurrentFrameIndex(frame); +} + } // End of namespace Pegasus diff --git a/engines/pegasus/elements.h b/engines/pegasus/elements.h index 460b9a71d9..97f982a6d9 100644 --- a/engines/pegasus/elements.h +++ b/engines/pegasus/elements.h @@ -193,6 +193,66 @@ protected: uint16 _currentFrameNum; }; +class SpriteFrame; + +class Sprite : public DisplayElement { +friend class SpriteFrame; +public: + Sprite(const tDisplayElementID); + virtual ~Sprite(); + + virtual void addPICTResourceFrame(const tResIDType, const bool, const tCoordType, const tCoordType); + virtual uint32 addFrame(SpriteFrame *, const tCoordType, const tCoordType); + virtual void removeFrame(const uint32); + virtual void discardFrames(); + + // Setting the current frame. + // If the index is negative, sets the current frame to NULL and hides the sprite. + // If the index is larger than the number of frames in the sprite, the number + // is treated modulo the number of frames. + virtual void setCurrentFrameIndex(const int32); + virtual uint32 getCurrentFrameIndex() const { return _currentFrameNum; } + + virtual SpriteFrame *getFrame(const int32); + + virtual void draw(const Common::Rect &); + + uint32 getNumFrames() const { return _numFrames; } + +protected: + struct SpriteFrameRec { + SpriteFrame *frame; + tCoordType frameLeft; + tCoordType frameTop; + }; + + uint32 _numFrames; + uint32 _currentFrameNum; + SpriteFrameRec *_currentFrame; + Common::Array<SpriteFrameRec> _frameArray; +}; + +class SpriteSequence : public FrameSequence { +public: + SpriteSequence(const tDisplayElementID id, const tDisplayElementID spriteID); + virtual ~SpriteSequence() {} + + void useTransparent(bool transparent) { _transparent = transparent; } + + virtual void openFrameSequence(); + virtual void closeFrameSequence(); + + virtual void draw(const Common::Rect &); + + virtual void setBounds(const Common::Rect &); + +protected: + virtual void newFrame(const uint16); + + bool _transparent; + Sprite _sprite; +}; + } // End of namespace Pegasus #endif diff --git a/engines/pegasus/graphics.cpp b/engines/pegasus/graphics.cpp index d5d6350ca3..a30b2c8dfd 100644 --- a/engines/pegasus/graphics.cpp +++ b/engines/pegasus/graphics.cpp @@ -195,10 +195,7 @@ void GraphicsManager::invalRect(const Common::Rect &rect) { _dirtyRect = rect; } else { // Expand our dirty rect to include rect - _dirtyRect.left = MIN<int>(_dirtyRect.left, rect.left); - _dirtyRect.top = MIN<int>(_dirtyRect.top, rect.top); - _dirtyRect.right = MAX<int>(_dirtyRect.right, rect.right); - _dirtyRect.bottom = MAX<int>(_dirtyRect.bottom, rect.bottom); + _dirtyRect.extend(rect); } } diff --git a/engines/pegasus/module.mk b/engines/pegasus/module.mk index 01c069e1ee..5c77fac6ad 100644 --- a/engines/pegasus/module.mk +++ b/engines/pegasus/module.mk @@ -16,6 +16,7 @@ MODULE_OBJS = \ overview.o \ pegasus.o \ sound.o \ + surface.o \ timers.o \ transition.o \ util.o \ diff --git a/engines/pegasus/surface.cpp b/engines/pegasus/surface.cpp new file mode 100755 index 0000000000..32a8aa0df6 --- /dev/null +++ b/engines/pegasus/surface.cpp @@ -0,0 +1,221 @@ +/* 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. + * + * Additional copyright for this file: + * Copyright (C) 1995-1997 Presto Studios, Inc. + * + * 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. + * + */ + +#include "common/file.h" +#include "common/macresman.h" +#include "common/stream.h" +#include "common/system.h" +#include "graphics/pict.h" +#include "graphics/surface.h" +#include "video/video_decoder.h" + +#include "pegasus/pegasus.h" +#include "pegasus/surface.h" + +namespace Pegasus { + +Surface::Surface() { + _ownsSurface = false; + _surface = 0; +} + +Surface::~Surface() { + deallocateSurface(); +} + +void Surface::deallocateSurface() { + if (_surface) { + if (_ownsSurface) { + _surface->free(); + delete _surface; + } + + _surface = 0; + _bounds = Common::Rect(); + _ownsSurface = false; + } +} + +void Surface::shareSurface(Surface *surface) { + deallocateSurface(); + + if (surface) { + _ownsSurface = false; + _surface = surface->getSurface(); + surface->getSurfaceBounds(_bounds); + } +} + +void Surface::allocateSurface(const Common::Rect &bounds) { + deallocateSurface(); + + if (bounds.isEmpty()) + return; + + _bounds = bounds; + _surface = new Graphics::Surface(); + _surface->create(bounds.width(), bounds.height(), g_system->getScreenFormat()); + _ownsSurface = true; +} + +void Surface::getImageFromPICTFile(const Common::String &fileName) { + Common::File pict; + if (!pict.open(fileName)) + error("Could not open picture '%s'", fileName.c_str()); + + getImageFromPICTStream(&pict); +} + +void Surface::getImageFromPICTResource(Common::MacResManager *resFork, uint16 id) { + Common::SeekableReadStream *res = resFork->getResource(MKTAG('P', 'I', 'C', 'T'), id); + if (!res) + error("Could not open PICT resource %d from '%s'", id, resFork->getBaseFileName().c_str()); + + getImageFromPICTStream(res); + delete res; +} + +void Surface::getImageFromPICTStream(Common::SeekableReadStream *stream) { + Graphics::PictDecoder pict(g_system->getScreenFormat()); + byte pal[256 * 3]; + + Graphics::Surface *surface = pict.decodeImage(stream, pal); + + // Create the surface if not present + if (!_surface) + _surface = new Graphics::Surface(); + + // Update + if (surface->format.bytesPerPixel == 1) { + // Convert to true color + _surface->create(surface->w, surface->h, g_system->getScreenFormat()); + + for (int y = 0; y < surface->h; y++) { + for (int x = 0; x < surface->w; x++) { + byte index = *((byte *)surface->getBasePtr(x, y)); + uint32 color = _surface->format.RGBToColor(pal[index * 3], pal[index * 3 + 1], pal[index * 3 + 2]); + if (_surface->format.bytesPerPixel == 2) + *((uint16 *)_surface->getBasePtr(x, y)) = color; + else + *((uint32 *)_surface->getBasePtr(x, y)) = color; + } + } + } else { + // Just a copy + _surface->copyFrom(*surface); + } + + _ownsSurface = true; + _bounds = Common::Rect(0, 0, _surface->w, _surface->h); +} + +void Surface::copyToCurrentPort() const { + copyToCurrentPort(_bounds); +} + +void Surface::copyToCurrentPortTransparent() const { + copyToCurrentPortTransparent(_bounds); +} + +void Surface::copyToCurrentPort(const Common::Rect &rect) const { + copyToCurrentPort(rect, rect); +} + +void Surface::copyToCurrentPortTransparent(const Common::Rect &rect) const { + copyToCurrentPortTransparent(rect, rect); +} + +void Surface::copyToCurrentPort(const Common::Rect &srcRect, const Common::Rect &dstRect) const { + Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea(); + byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top); + byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top); + + int lineSize = srcRect.width() * _surface->format.bytesPerPixel; + + for (int y = 0; y < srcRect.height(); y++) { + memcpy(dst, src, lineSize); + src += _surface->pitch - lineSize; + dst += screen->pitch - lineSize; + } +} + +void Surface::copyToCurrentPortTransparent(const Common::Rect &srcRect, const Common::Rect &dstRect) const { + // HACK: Seems we're truncating some color data somewhere... + uint32 transColor1 = g_system->getScreenFormat().RGBToColor(0xff, 0xff, 0xff); + uint32 transColor2 = g_system->getScreenFormat().RGBToColor(0xf8, 0xf8, 0xf8); + + Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea(); + byte *src = (byte *)_surface->getBasePtr(srcRect.left, srcRect.top); + byte *dst = (byte *)screen->getBasePtr(dstRect.left, dstRect.top); + + int lineSize = srcRect.width() * _surface->format.bytesPerPixel; + + for (int y = 0; y < srcRect.height(); y++) { + for (int x = 0; x < srcRect.width(); x++) { + if (g_system->getScreenFormat().bytesPerPixel == 2) { + uint16 color = READ_UINT16(src); + if (color != transColor1 && color != transColor2) + memcpy(dst, src, 2); + } else if (g_system->getScreenFormat().bytesPerPixel == 4) { + uint32 color = READ_UINT32(src); + if (color != transColor1 && color != transColor2) + memcpy(dst, src, 4); + } + + src += g_system->getScreenFormat().bytesPerPixel; + dst += g_system->getScreenFormat().bytesPerPixel; + } + + src += _surface->pitch - lineSize; + dst += screen->pitch - lineSize; + } +} + +PixelImage::PixelImage() { + _transparent = false; +} + +void PixelImage::drawImage(const Common::Rect &sourceBounds, const Common::Rect &destBounds) { + if (!isSurfaceValid()) + return; + + // Draw from sourceBounds to destBounds based on _transparent + if (_transparent) + copyToCurrentPortTransparent(sourceBounds, destBounds); + else + copyToCurrentPort(sourceBounds, destBounds); +} + +void Frame::initFromPICTFile(const Common::String &fileName, bool transparent) { + getImageFromPICTFile(fileName); + _transparent = transparent; +} + +void Frame::initFromPICTResource(Common::MacResManager *resFork, uint16 id, bool transparent) { + getImageFromPICTResource(resFork, id); + _transparent = transparent; +} + +} // End of namespace Pegasus diff --git a/engines/pegasus/surface.h b/engines/pegasus/surface.h new file mode 100755 index 0000000000..1b63702960 --- /dev/null +++ b/engines/pegasus/surface.h @@ -0,0 +1,119 @@ +/* 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. + * + * Additional copyright for this file: + * Copyright (C) 1995-1997 Presto Studios, Inc. + * + * 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. + * + */ + +#ifndef PEGASUS_SURFACE_H +#define PEGASUS_SURFACE_H + +#include "common/rect.h" +#include "common/str.h" + +#include "pegasus/types.h" + +namespace Common { + class MacResManager; +} + +namespace Graphics { + struct Surface; +} + +namespace Video { + class SeekableVideoDecoder; +} + +namespace Pegasus { + +// Surface bounds are always normalized. + +class Surface { +public: + Surface(); + virtual ~Surface(); + + virtual void allocateSurface(const Common::Rect &); + virtual void deallocateSurface(); + virtual void shareSurface(Surface *surface); + bool isSurfaceValid() const { return _surface != 0; } + + Graphics::Surface *getSurface() const { return _surface; } + void getSurfaceBounds(Common::Rect &r) { r = _bounds; } + + // None of the CopyToCurrentPort functions do any sanity checks. + // For speed, they just call CopyBits. + // It's up to clients to make sure that the GWorld is valid. + void copyToCurrentPort() const; + void copyToCurrentPortTransparent() const; + void copyToCurrentPort(const Common::Rect &) const; + void copyToCurrentPortTransparent(const Common::Rect &) const; + void copyToCurrentPort(const Common::Rect &, const Common::Rect &) const; + void copyToCurrentPortTransparent(const Common::Rect &, const Common::Rect &) const; + + virtual void getImageFromPICTFile(const Common::String &fileName); + virtual void getImageFromPICTResource(Common::MacResManager *resFork, uint16 id); + +protected: + bool _ownsSurface; + Graphics::Surface *_surface; + Common::Rect _bounds; + +private: + void getImageFromPICTStream(Common::SeekableReadStream *stream); +}; + +class PixelImage : public Surface { +public: + PixelImage(); + virtual ~PixelImage() {} + + void drawImage(const Common::Rect &, const Common::Rect &); + +protected: + virtual void setTransparent(bool transparent) { _transparent = transparent; } + + bool _transparent; +}; + +class Frame : public PixelImage { +public: + Frame() {} + virtual ~Frame() {} + + virtual void initFromPICTFile(const Common::String &fileName, bool transparent = false); + virtual void initFromPICTResource(Common::MacResManager *resFork, uint16 id, bool transparent = false); +}; + +class SpriteFrame : public Frame { +friend class Sprite; +public: + SpriteFrame() { _referenceCount = 0; } + virtual ~SpriteFrame() {} + +protected: + uint32 _referenceCount; +}; + +} // End of namespace Pegasus + +#endif |