2009-06-14 12:44:12 +00: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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
2009-06-19 00:00:31 +00:00
|
|
|
|
2009-06-14 12:44:12 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
|
2009-06-19 00:00:31 +00:00
|
|
|
#include "draci/draci.h"
|
2009-06-14 12:44:12 +00:00
|
|
|
#include "draci/sprite.h"
|
2009-07-01 01:11:48 +00:00
|
|
|
#include "draci/font.h"
|
2009-06-14 12:44:12 +00:00
|
|
|
|
2009-07-20 17:25:57 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2009-06-14 12:44:12 +00:00
|
|
|
namespace Draci {
|
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
const Displacement kNoDisplacement = { 0, 0, 1.0, 1.0 };
|
|
|
|
|
2009-06-25 14:03:57 +00:00
|
|
|
/**
|
|
|
|
* @brief Transforms an image from column-wise to row-wise
|
2009-09-30 10:45:14 +00:00
|
|
|
* @param img pointer to the buffer containing the image data
|
|
|
|
* width width of the image in the buffer
|
|
|
|
* height height of the image in the buffer
|
2009-06-25 14:03:57 +00:00
|
|
|
*/
|
|
|
|
static void transformToRows(byte *img, uint16 width, uint16 height) {
|
|
|
|
byte *buf = new byte[width * height];
|
|
|
|
byte *tmp = buf;
|
|
|
|
memcpy(buf, img, width * height);
|
2009-09-30 10:45:14 +00:00
|
|
|
|
2009-06-25 14:03:57 +00:00
|
|
|
for (uint16 i = 0; i < width; ++i) {
|
|
|
|
for (uint16 j = 0; j < height; ++j) {
|
|
|
|
img[j * width + i] = *tmp++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 10:45:14 +00:00
|
|
|
|
2009-06-25 14:03:57 +00:00
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
2009-06-14 12:44:12 +00:00
|
|
|
/**
|
|
|
|
* Constructor for loading sprites from a raw data buffer, one byte per pixel.
|
2009-07-04 18:29:01 +00:00
|
|
|
*/
|
2009-09-25 08:13:39 +00:00
|
|
|
Sprite::Sprite(const byte *raw_data, uint16 width, uint16 height, int x, int y,
|
2009-09-30 10:45:14 +00:00
|
|
|
bool columnwise) : _data(NULL) {
|
2009-07-04 18:29:01 +00:00
|
|
|
|
2009-06-30 22:31:29 +00:00
|
|
|
_width = width;
|
|
|
|
_height = height;
|
2009-07-22 04:42:33 +00:00
|
|
|
|
|
|
|
_scaledWidth = _width;
|
|
|
|
_scaledHeight = _height;
|
|
|
|
|
2009-06-30 22:31:29 +00:00
|
|
|
_x = x;
|
|
|
|
_y = y;
|
2009-07-22 04:42:33 +00:00
|
|
|
|
2009-07-12 19:32:01 +00:00
|
|
|
_delay = 0;
|
2009-07-04 18:29:01 +00:00
|
|
|
|
2009-07-03 17:39:13 +00:00
|
|
|
_mirror = false;
|
|
|
|
|
2009-09-25 08:13:39 +00:00
|
|
|
byte *data = new byte[width * height];
|
2009-06-22 20:13:25 +00:00
|
|
|
|
2009-09-25 08:13:39 +00:00
|
|
|
memcpy(data, raw_data, width * height);
|
2009-06-25 14:03:57 +00:00
|
|
|
|
|
|
|
// If the sprite is stored column-wise, transform it to row-wise
|
|
|
|
if (columnwise) {
|
2009-09-25 08:13:39 +00:00
|
|
|
transformToRows(data, width, height);
|
2009-09-30 10:45:14 +00:00
|
|
|
}
|
2009-09-25 08:13:39 +00:00
|
|
|
_data = data;
|
2009-06-14 13:51:07 +00:00
|
|
|
}
|
2009-06-14 12:44:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for loading sprites from a sprite-formatted buffer, one byte per
|
2009-09-30 10:45:14 +00:00
|
|
|
* pixel.
|
2009-06-14 12:44:12 +00:00
|
|
|
*/
|
2009-09-25 08:13:39 +00:00
|
|
|
Sprite::Sprite(const byte *sprite_data, uint16 length, int x, int y, bool columnwise)
|
2009-09-30 10:45:14 +00:00
|
|
|
: _data(NULL) {
|
2009-07-04 18:29:01 +00:00
|
|
|
|
2009-06-30 22:31:29 +00:00
|
|
|
_x = x;
|
|
|
|
_y = y;
|
2009-07-22 04:42:33 +00:00
|
|
|
|
2009-07-12 19:32:01 +00:00
|
|
|
_delay = 0;
|
2009-07-04 18:29:01 +00:00
|
|
|
|
2009-09-30 10:45:14 +00:00
|
|
|
_mirror = false;
|
2009-07-03 17:39:13 +00:00
|
|
|
|
2009-06-14 13:51:07 +00:00
|
|
|
Common::MemoryReadStream reader(sprite_data, length);
|
2009-06-14 12:44:12 +00:00
|
|
|
|
2009-07-04 18:29:01 +00:00
|
|
|
_width = reader.readSint16LE();
|
|
|
|
_height = reader.readSint16LE();
|
2009-06-14 12:44:12 +00:00
|
|
|
|
2009-07-22 04:42:33 +00:00
|
|
|
_scaledWidth = _width;
|
|
|
|
_scaledHeight = _height;
|
|
|
|
|
2009-09-25 08:13:39 +00:00
|
|
|
byte *data = new byte[_width * _height];
|
2009-06-22 20:13:25 +00:00
|
|
|
|
2009-09-25 08:13:39 +00:00
|
|
|
reader.read(data, _width * _height);
|
2009-06-25 14:03:57 +00:00
|
|
|
|
|
|
|
// If the sprite is stored column-wise, transform it to row-wise
|
|
|
|
if (columnwise) {
|
2009-09-25 08:13:39 +00:00
|
|
|
transformToRows(data, _width, _height);
|
2009-09-30 10:45:14 +00:00
|
|
|
}
|
2009-09-25 08:13:39 +00:00
|
|
|
_data = data;
|
2009-06-14 13:51:07 +00:00
|
|
|
}
|
2009-06-14 12:44:12 +00:00
|
|
|
|
|
|
|
Sprite::~Sprite() {
|
|
|
|
delete[] _data;
|
|
|
|
}
|
2009-06-19 00:00:31 +00:00
|
|
|
|
2009-07-03 17:39:13 +00:00
|
|
|
void Sprite::setMirrorOn() {
|
|
|
|
_mirror = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite::setMirrorOff() {
|
|
|
|
_mirror = false;
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
int Sprite::getPixel(int x, int y, const Displacement &displacement) const {
|
|
|
|
Common::Rect rect = getRect(displacement);
|
2009-07-27 04:14:59 +00:00
|
|
|
|
|
|
|
int dy = y - rect.top;
|
|
|
|
int dx = x - rect.left;
|
|
|
|
|
|
|
|
// Calculate scaling factors
|
2009-09-27 20:49:59 +00:00
|
|
|
double scaleX = double(rect.width()) / _width;
|
|
|
|
double scaleY = double(rect.height()) / _height;
|
2009-07-27 04:14:59 +00:00
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
int sy = scummvm_lround(dy / scaleY);
|
|
|
|
int sx = scummvm_lround(dx / scaleX);
|
2009-07-27 04:14:59 +00:00
|
|
|
|
|
|
|
if (_mirror)
|
|
|
|
return _data[sy * _width + (_width - sx)];
|
|
|
|
else
|
|
|
|
return _data[sy * _width + sx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
void Sprite::drawReScaled(Surface *surface, bool markDirty, const Displacement &displacement) const {
|
2009-09-29 06:36:39 +00:00
|
|
|
const Common::Rect destRect(getRect(displacement));
|
|
|
|
const Common::Rect surfaceRect(0, 0, surface->w, surface->h);
|
2009-07-20 17:25:57 +00:00
|
|
|
Common::Rect clippedDestRect(destRect);
|
|
|
|
clippedDestRect.clip(surfaceRect);
|
|
|
|
|
|
|
|
// Calculate by how much we need to adjust the source rectangle to account for cropping
|
2009-09-29 06:36:39 +00:00
|
|
|
const Common::Point croppedBy(clippedDestRect.left - destRect.left, clippedDestRect.top - destRect.top);
|
2009-07-20 17:25:57 +00:00
|
|
|
|
|
|
|
// Get pointers to source and destination buffers
|
|
|
|
byte *dst = (byte *)surface->getBasePtr(clippedDestRect.left, clippedDestRect.top);
|
|
|
|
|
|
|
|
const int transparent = surface->getTransparentColour();
|
|
|
|
|
|
|
|
// Calculate how many rows and columns we need to draw
|
2009-07-25 18:33:20 +00:00
|
|
|
const int rows = clippedDestRect.height();
|
|
|
|
const int columns = clippedDestRect.width();
|
2009-07-20 17:25:57 +00:00
|
|
|
|
2009-09-29 06:36:39 +00:00
|
|
|
// Precalculate column indexes
|
2009-07-20 17:25:57 +00:00
|
|
|
int *columnIndices = new int[columns];
|
2009-09-29 06:36:39 +00:00
|
|
|
if (!_mirror) {
|
|
|
|
for (int j = 0; j < columns; ++j) {
|
|
|
|
columnIndices[j] = (j + croppedBy.x) * _width / destRect.width();
|
|
|
|
}
|
|
|
|
} else {
|
2009-09-30 10:45:14 +00:00
|
|
|
// Draw the sprite mirrored if the _mirror flag is set
|
2009-09-29 06:36:39 +00:00
|
|
|
for (int j = 0; j < columns; ++j) {
|
|
|
|
columnIndices[j] = _width - 1 - (j + croppedBy.x) * _width / destRect.width();
|
|
|
|
}
|
2009-07-20 17:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Blit the sprite to the surface
|
|
|
|
for (int i = 0; i < rows; ++i) {
|
2009-09-29 06:36:39 +00:00
|
|
|
// Compute the index of current row to be drawn
|
|
|
|
const int row = (i + croppedBy.y) * _height / destRect.height();
|
|
|
|
const byte *row_data = _data + row * _width;
|
2009-09-30 10:45:14 +00:00
|
|
|
|
2009-07-22 04:50:11 +00:00
|
|
|
for (int j = 0; j < columns; ++j) {
|
2009-07-20 17:25:57 +00:00
|
|
|
|
|
|
|
// Fetch index of current column to be drawn
|
2009-09-29 06:36:39 +00:00
|
|
|
const byte src = row_data[columnIndices[j]];
|
2009-07-20 17:25:57 +00:00
|
|
|
|
|
|
|
// Don't blit if the pixel is transparent on the target surface
|
2009-09-29 06:36:39 +00:00
|
|
|
if (src != transparent) {
|
|
|
|
dst[j] = src;
|
2009-07-20 17:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next row
|
|
|
|
dst += surface->pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the sprite's rectangle dirty
|
2009-09-30 10:45:14 +00:00
|
|
|
if (markDirty) {
|
2009-07-20 17:25:57 +00:00
|
|
|
surface->markDirtyRect(destRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] columnIndices;
|
|
|
|
}
|
|
|
|
|
2009-06-22 20:13:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Draws the sprite to a Draci::Surface
|
2009-07-04 23:05:13 +00:00
|
|
|
* @param surface Pointer to a Draci::Surface
|
2009-06-22 20:13:25 +00:00
|
|
|
*
|
|
|
|
* Draws the sprite to a Draci::Surface and marks its rectangle on the surface as dirty.
|
2009-07-04 23:05:13 +00:00
|
|
|
* It is safe to call it for sprites that would overflow the surface.
|
2009-06-22 20:13:25 +00:00
|
|
|
*/
|
2009-09-27 20:49:59 +00:00
|
|
|
void Sprite::draw(Surface *surface, bool markDirty, int relX, int relY) const {
|
2009-09-29 06:36:39 +00:00
|
|
|
// TODO: refactor like drawReScaled()
|
|
|
|
|
2009-07-04 23:05:13 +00:00
|
|
|
Common::Rect sourceRect(0, 0, _width, _height);
|
2009-09-27 20:49:59 +00:00
|
|
|
Common::Rect destRect(_x + relX, _y + relY, _x + relX + _width, _y + relY + _height);
|
2009-07-04 23:05:13 +00:00
|
|
|
Common::Rect surfaceRect(0, 0, surface->w, surface->h);
|
|
|
|
Common::Rect clippedDestRect(destRect);
|
2009-07-04 18:29:01 +00:00
|
|
|
|
2009-07-04 23:05:13 +00:00
|
|
|
clippedDestRect.clip(surfaceRect);
|
2009-07-03 17:39:13 +00:00
|
|
|
|
2009-07-20 17:25:57 +00:00
|
|
|
// Calculate by how much we need to adjust the source rectangle to account for cropping
|
|
|
|
const int adjustLeft = clippedDestRect.left - destRect.left;
|
|
|
|
const int adjustRight = clippedDestRect.right - destRect.right;
|
|
|
|
const int adjustTop = clippedDestRect.top - destRect.top;
|
2009-09-30 10:45:14 +00:00
|
|
|
const int adjustBottom = clippedDestRect.bottom - destRect.bottom;
|
2009-07-03 17:39:13 +00:00
|
|
|
|
2009-07-20 17:25:57 +00:00
|
|
|
// Resize source rectangle
|
2009-07-04 23:05:13 +00:00
|
|
|
sourceRect.left += adjustLeft;
|
|
|
|
sourceRect.right += adjustRight;
|
|
|
|
sourceRect.top += adjustTop;
|
|
|
|
sourceRect.bottom += adjustBottom;
|
2009-07-03 17:39:13 +00:00
|
|
|
|
2009-07-20 17:25:57 +00:00
|
|
|
// Get pointers to source and destination buffers
|
2009-07-04 23:05:13 +00:00
|
|
|
byte *dst = (byte *)surface->getBasePtr(clippedDestRect.left, clippedDestRect.top);
|
2009-09-25 08:13:39 +00:00
|
|
|
const byte *src = _data;
|
2009-07-04 23:05:13 +00:00
|
|
|
|
2009-07-07 15:37:50 +00:00
|
|
|
const int transparent = surface->getTransparentColour();
|
2009-07-06 19:41:13 +00:00
|
|
|
|
2009-07-04 23:05:13 +00:00
|
|
|
// Blit the sprite to the surface
|
|
|
|
for (int i = sourceRect.top; i < sourceRect.bottom; ++i) {
|
|
|
|
for (int j = sourceRect.left; j < sourceRect.right; ++j) {
|
|
|
|
|
|
|
|
// Don't blit if the pixel is transparent on the target surface
|
2009-07-07 15:37:50 +00:00
|
|
|
if (src[i * _width + j] != transparent) {
|
2009-07-04 23:05:13 +00:00
|
|
|
|
2009-09-30 10:45:14 +00:00
|
|
|
// Draw the sprite mirrored if the _mirror flag is set
|
2009-07-04 23:05:13 +00:00
|
|
|
if (_mirror) {
|
|
|
|
dst[sourceRect.right - j - 1] = src[i * _width + j];
|
2009-09-30 10:45:14 +00:00
|
|
|
} else {
|
2009-07-04 23:05:13 +00:00
|
|
|
dst[j] = src[i * _width + j];
|
|
|
|
}
|
2009-09-30 10:45:14 +00:00
|
|
|
}
|
2009-06-19 00:00:31 +00:00
|
|
|
}
|
2009-07-03 17:39:13 +00:00
|
|
|
|
2009-07-20 17:25:57 +00:00
|
|
|
// Advance to next row
|
2009-06-19 00:00:31 +00:00
|
|
|
dst += surface->pitch;
|
|
|
|
}
|
|
|
|
|
2009-06-22 20:13:25 +00:00
|
|
|
// Mark the sprite's rectangle dirty
|
2009-09-30 10:45:14 +00:00
|
|
|
if (markDirty) {
|
2009-07-04 23:05:13 +00:00
|
|
|
surface->markDirtyRect(destRect);
|
2009-07-03 16:41:11 +00:00
|
|
|
}
|
2009-06-19 00:00:31 +00:00
|
|
|
}
|
2009-07-27 04:14:59 +00:00
|
|
|
|
2009-07-01 01:11:48 +00:00
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
Common::Rect Sprite::getRect(const Displacement &displacement) const {
|
|
|
|
return Common::Rect(_x + displacement.relX, _y + displacement.relY,
|
2009-09-30 10:45:14 +00:00
|
|
|
_x + displacement.relX + (int) (_scaledWidth * displacement.extraScaleX),
|
|
|
|
_y + displacement.relY + (int) (_scaledHeight * displacement.extraScaleY));
|
2009-07-20 17:25:57 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 17:33:00 +00:00
|
|
|
Text::Text(const Common::String &str, const Font *font, byte fontColour,
|
2009-09-30 10:45:14 +00:00
|
|
|
int x, int y, uint spacing) {
|
2009-07-01 01:18:22 +00:00
|
|
|
_x = x;
|
|
|
|
_y = y;
|
2009-07-12 19:32:01 +00:00
|
|
|
_delay = 0;
|
2009-09-30 10:45:14 +00:00
|
|
|
|
2009-07-27 04:14:59 +00:00
|
|
|
_text = str;
|
2009-08-02 05:09:55 +00:00
|
|
|
|
|
|
|
_length = 0;
|
|
|
|
for (uint i = 0; i < _text.size(); ++i) {
|
|
|
|
if (_text[i] != '|') {
|
|
|
|
++_length;
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 10:45:14 +00:00
|
|
|
|
2009-07-01 01:11:48 +00:00
|
|
|
_spacing = spacing;
|
|
|
|
_colour = fontColour;
|
|
|
|
|
|
|
|
_font = font;
|
2009-07-01 16:14:04 +00:00
|
|
|
|
|
|
|
_width = _font->getStringWidth(str, _spacing);
|
2009-07-27 04:14:59 +00:00
|
|
|
_height = _font->getStringHeight(str);
|
2009-07-22 04:42:33 +00:00
|
|
|
|
|
|
|
_scaledWidth = _width;
|
|
|
|
_scaledHeight = _height;
|
2009-07-01 01:11:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 01:43:20 +00:00
|
|
|
void Text::setText(const Common::String &str) {
|
2009-07-01 16:14:04 +00:00
|
|
|
_width = _font->getStringWidth(str, _spacing);
|
2009-07-27 04:14:59 +00:00
|
|
|
_height = _font->getStringHeight(str);
|
2009-07-01 16:14:04 +00:00
|
|
|
|
2009-07-27 04:14:59 +00:00
|
|
|
_text = str;
|
2009-08-02 05:09:55 +00:00
|
|
|
|
|
|
|
_length = 0;
|
|
|
|
for (uint i = 0; i < _text.size(); ++i) {
|
|
|
|
if (_text[i] != '|') {
|
|
|
|
++_length;
|
|
|
|
}
|
|
|
|
}
|
2009-07-01 01:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Text::setColour(byte fontColour) {
|
|
|
|
_colour = fontColour;
|
2009-07-01 01:11:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 16:15:06 +00:00
|
|
|
void Text::setSpacing(uint spacing) {
|
|
|
|
_spacing = spacing;
|
|
|
|
}
|
|
|
|
|
2009-09-25 08:13:39 +00:00
|
|
|
uint Text::getLength() const {
|
2009-08-02 05:09:55 +00:00
|
|
|
return _length;
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:49:59 +00:00
|
|
|
void Text::draw(Surface *surface, bool markDirty, int relX, int relY) const {
|
|
|
|
_font->drawString(surface, _text, _x + relX, _y + relY, _colour, _spacing);
|
2009-07-03 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-26 00:04:12 +00:00
|
|
|
// TODO: Handle scaled parameter properly by implementing Text scaling
|
2009-09-27 20:49:59 +00:00
|
|
|
Common::Rect Text::getRect(const Displacement &displacement) const {
|
|
|
|
return Common::Rect(_x + displacement.relX, _y + displacement.relY, _x + displacement.relX + _width, _y + displacement.relY + _height);
|
2009-07-03 19:02:08 +00:00
|
|
|
}
|
2009-08-09 03:59:39 +00:00
|
|
|
|
2009-09-25 17:33:00 +00:00
|
|
|
void Text::setFont(const Font *font) {
|
2009-08-09 03:59:39 +00:00
|
|
|
_font = font;
|
|
|
|
|
|
|
|
_width = _font->getStringWidth(_text, _spacing);
|
|
|
|
_height = _font->getStringHeight(_text);
|
|
|
|
}
|
2009-09-30 10:45:14 +00:00
|
|
|
|
|
|
|
} // End of namespace Draci
|
2009-06-14 12:44:12 +00:00
|
|
|
|