GOB: Move the background saving into its own class BackBuffer

This commit is contained in:
Sven Hesse 2012-07-06 06:50:04 +02:00
parent 67d7c3f11f
commit 9af01cd584
5 changed files with 182 additions and 62 deletions

View File

@ -28,23 +28,20 @@
namespace Gob {
ANIObject::ANIObject(const ANIFile &ani) : _ani(&ani), _cmp(0),
_visible(false), _paused(false), _mode(kModeContinuous),
_x(0), _y(0), _background(0), _drawn(false) {
_visible(false), _paused(false), _mode(kModeContinuous), _x(0), _y(0) {
setAnimation(0);
setPosition();
}
ANIObject::ANIObject(const CMPFile &cmp) : _ani(0), _cmp(&cmp),
_visible(false), _paused(false), _mode(kModeContinuous),
_x(0), _y(0), _background(0), _drawn(false) {
_visible(false), _paused(false), _mode(kModeContinuous), _x(0), _y(0) {
setAnimation(0);
setPosition();
}
ANIObject::~ANIObject() {
delete _background;
}
void ANIObject::setVisible(bool visible) {
@ -188,46 +185,36 @@ bool ANIObject::draw(Surface &dest, int16 &left, int16 &top,
bool ANIObject::drawCMP(Surface &dest, int16 &left, int16 &top,
int16 &right, int16 &bottom) {
if (!_background) {
if (!hasBuffer()) {
uint16 width, height;
_cmp->getMaxSize(width, height);
_background = new Surface(width, height, dest.getBPP());
resizeBuffer(width, height);
}
const uint16 cR = _cmp->getWidth (_animation) - 1;
const uint16 cB = _cmp->getHeight(_animation) - 1;
left = _x;
top = _y;
right = _x + _cmp->getWidth (_animation) - 1;
bottom = _y + _cmp->getHeight(_animation) - 1;
_backgroundLeft = CLIP<int16>( + _x, 0, dest.getWidth () - 1);
_backgroundTop = CLIP<int16>( + _y, 0, dest.getHeight() - 1);
_backgroundRight = CLIP<int16>(cR + _x, 0, dest.getWidth () - 1);
_backgroundBottom = CLIP<int16>(cB + _y, 0, dest.getHeight() - 1);
_background->blit(dest, _backgroundLeft , _backgroundTop,
_backgroundRight, _backgroundBottom, 0, 0);
if (!saveScreen(dest, left, top, right, bottom))
return false;
_cmp->draw(dest, _animation, _x, _y, 0);
_drawn = true;
left = _backgroundLeft;
top = _backgroundTop;
right = _backgroundRight;
bottom = _backgroundBottom;
return true;
}
bool ANIObject::drawANI(Surface &dest, int16 &left, int16 &top,
int16 &right, int16 &bottom) {
if (!_background) {
if (!hasBuffer()) {
uint16 width, height;
_ani->getMaxSize(width, height);
_background = new Surface(width, height, dest.getBPP());
resizeBuffer(width, height);
}
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
@ -236,45 +223,23 @@ bool ANIObject::drawANI(Surface &dest, int16 &left, int16 &top,
const ANIFile::FrameArea &area = animation.frameAreas[_frame];
_backgroundLeft = CLIP<int16>(area.left + _x, 0, dest.getWidth () - 1);
_backgroundTop = CLIP<int16>(area.top + _y, 0, dest.getHeight() - 1);
_backgroundRight = CLIP<int16>(area.right + _x, 0, dest.getWidth () - 1);
_backgroundBottom = CLIP<int16>(area.bottom + _y, 0, dest.getHeight() - 1);
left = _x + area.left;
top = _y + area.top;
right = _x + area.right;
bottom = _y + area.bottom;
_background->blit(dest, _backgroundLeft , _backgroundTop,
_backgroundRight, _backgroundBottom, 0, 0);
if (!saveScreen(dest, left, top, right, bottom))
return false;
_ani->draw(dest, _animation, _frame, _x, _y);
_drawn = true;
left = _backgroundLeft;
top = _backgroundTop;
right = _backgroundRight;
bottom = _backgroundBottom;
return true;
}
bool ANIObject::clear(Surface &dest, int16 &left, int16 &top,
int16 &right, int16 &bottom) {
if (!_drawn)
return false;
const int16 bgRight = _backgroundRight - _backgroundLeft;
const int16 bgBottom = _backgroundBottom - _backgroundTop;
dest.blit(*_background, 0, 0, bgRight, bgBottom, _backgroundLeft, _backgroundTop);
_drawn = false;
left = _backgroundLeft;
top = _backgroundTop;
right = _backgroundRight;
bottom = _backgroundBottom;
return true;
return restoreScreen(dest, left, top, right, bottom);
}
void ANIObject::advance() {

View File

@ -25,6 +25,8 @@
#include "common/system.h"
#include "gob/backbuffer.h"
namespace Gob {
class ANIFile;
@ -32,7 +34,7 @@ class CMPFile;
class Surface;
/** An ANI object, controlling an animation within an ANI file. */
class ANIObject {
class ANIObject : public BackBuffer {
public:
enum Mode {
kModeContinuous, ///< Play the animation continuously.
@ -118,13 +120,6 @@ private:
int16 _x; ///< The current X position.
int16 _y; ///< The current Y position.
Surface *_background; ///< The saved background.
bool _drawn; ///< Was the animation drawn?
int16 _backgroundLeft; ///< The left position of the saved background.
int16 _backgroundTop; ///< The top of the saved background.
int16 _backgroundRight; ///< The right position of the saved background.
int16 _backgroundBottom; ///< The bottom position of the saved background.
bool drawCMP(Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom);
bool drawANI(Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom);

100
engines/gob/backbuffer.cpp Normal file
View File

@ -0,0 +1,100 @@
/* 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.
*
*/
#include "common/util.h"
#include "gob/backbuffer.h"
#include "gob/surface.h"
namespace Gob {
BackBuffer::BackBuffer() : _background(0), _saved(false) {
}
BackBuffer::~BackBuffer() {
delete _background;
}
bool BackBuffer::hasBuffer() const {
return _background != 0;
}
bool BackBuffer::hasSavedBackground() const {
return _saved;
}
void BackBuffer::trashBuffer() {
_saved = false;
}
void BackBuffer::resizeBuffer(uint16 width, uint16 height) {
trashBuffer();
if (_background && (_background->getWidth() == width) && (_background->getHeight() == height))
return;
delete _background;
_background = new Surface(width, height, 1);
}
bool BackBuffer::saveScreen(const Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom) {
if (!_background)
return false;
const int16 width = MIN<int16>(right - left + 1, _background->getWidth ());
const int16 height = MIN<int16>(bottom - top + 1, _background->getHeight());
if ((width <= 0) || (height <= 0))
return false;
right = left + width - 1;
bottom = top + height - 1;
_saveLeft = left;
_saveTop = top;
_saveRight = right;
_saveBottom = bottom;
_background->blit(dest, _saveLeft, _saveTop, _saveRight, _saveBottom, 0, 0);
_saved = true;
return true;
}
bool BackBuffer::restoreScreen(Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom) {
if (!_saved)
return false;
left = _saveLeft;
top = _saveTop;
right = _saveRight;
bottom = _saveBottom;
dest.blit(*_background, 0, 0, right - left, bottom - top, left, top);
_saved = false;
return true;
}
} // End of namespace Gob

59
engines/gob/backbuffer.h Normal file
View File

@ -0,0 +1,59 @@
/* 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.
*
*/
#ifndef GOB_BACKBUFFER_H
#define GOB_BACKBUFFER_H
#include "common/system.h"
namespace Gob {
class Surface;
class BackBuffer {
public:
BackBuffer();
~BackBuffer();
protected:
void trashBuffer();
void resizeBuffer(uint16 width, uint16 height);
bool saveScreen (const Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom);
bool restoreScreen( Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom);
bool hasBuffer() const;
bool hasSavedBackground() const;
private:
Surface *_background; ///< The saved background.
bool _saved; ///< Was the background saved?
int16 _saveLeft; ///< The left position of the saved background.
int16 _saveTop; ///< The top of the saved background.
int16 _saveRight; ///< The right position of the saved background.
int16 _saveBottom; ///< The bottom position of the saved background.
};
} // End of namespace Gob
#endif // GOB_BACKBUFFER_H

View File

@ -3,6 +3,7 @@ MODULE := engines/gob
MODULE_OBJS := \
anifile.o \
aniobject.o \
backbuffer.o \
cheater.o \
cheater_geisha.o \
cmpfile.o \