2005-01-17 10:57:15 +00:00
|
|
|
/* Copyright (C) 1994-1998 Revolution Software Ltd.
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2003-2006 The ScummVM project
|
2003-07-28 01:47:41 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-07-28 01:47:41 +00:00
|
|
|
*
|
2006-02-09 15:12:44 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2003-07-28 01:47:41 +00:00
|
|
|
*/
|
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
#include "common/stdafx.h"
|
2005-02-27 16:11:19 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
#include "sword2/sword2.h"
|
2005-02-27 16:11:19 +00:00
|
|
|
#include "sword2/defs.h"
|
2006-02-12 19:33:45 +00:00
|
|
|
#include "sword2/screen.h"
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-04 00:52:27 +00:00
|
|
|
namespace Sword2 {
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* This function takes a sprite and creates a mirror image of it.
|
|
|
|
* @param dst destination buffer
|
|
|
|
* @param src source buffer
|
|
|
|
* @param w width of the sprite
|
|
|
|
* @param h height of the sprite
|
|
|
|
*/
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
void Screen::mirrorSprite(byte *dst, byte *src, int16 w, int16 h) {
|
2003-10-15 06:40:31 +00:00
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
2003-09-23 17:24:45 +00:00
|
|
|
*dst++ = *(src + w - x - 1);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
src += w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* This function takes a compressed frame of a sprite with up to 256 colours
|
|
|
|
* and decompresses it.
|
2005-02-19 14:02:16 +00:00
|
|
|
* @param dst destination buffer
|
|
|
|
* @param src source buffer
|
2003-09-28 14:13:57 +00:00
|
|
|
* @param decompSize the expected size of the decompressed sprite
|
|
|
|
*/
|
2003-09-23 17:24:45 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
int32 Screen::decompressRLE256(byte *dst, byte *src, int32 decompSize) {
|
2003-07-28 01:47:41 +00:00
|
|
|
// PARAMETERS:
|
2003-09-23 17:24:45 +00:00
|
|
|
// source points to the start of the sprite data for input
|
2003-07-28 01:47:41 +00:00
|
|
|
// decompSize gives size of decompressed data in bytes
|
2003-09-23 17:24:45 +00:00
|
|
|
// dest points to start of destination buffer for decompressed
|
|
|
|
// data
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
byte headerByte; // block header byte
|
2005-02-19 14:02:16 +00:00
|
|
|
byte *endDest = dst + decompSize; // pointer to byte after end of decomp buffer
|
2003-07-28 01:47:41 +00:00
|
|
|
int32 rv;
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
while (1) {
|
2003-07-28 01:47:41 +00:00
|
|
|
// FLAT block
|
2003-09-23 17:24:45 +00:00
|
|
|
// read FLAT block header & increment 'scan' to first pixel
|
|
|
|
// of block
|
2005-02-19 14:02:16 +00:00
|
|
|
headerByte = *src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if this isn't a zero-length block
|
|
|
|
if (headerByte) {
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst + headerByte > endDest) {
|
2003-07-28 01:47:41 +00:00
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// set the next 'headerByte' pixels to the next colour
|
|
|
|
// at 'source'
|
2005-02-19 14:02:16 +00:00
|
|
|
memset(dst, *src, headerByte);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// increment destination pointer to just after this
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
dst += headerByte;
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// increment source pointer to just after this colour
|
2005-02-19 14:02:16 +00:00
|
|
|
src++;
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// if we've decompressed all of the data
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst == endDest) {
|
2003-09-23 17:24:45 +00:00
|
|
|
rv = 0; // return "OK"
|
2003-07-28 01:47:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-09-23 17:24:45 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
// RAW block
|
2003-09-23 17:24:45 +00:00
|
|
|
// read RAW block header & increment 'scan' to first pixel of
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
headerByte = *src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if this isn't a zero-length block
|
|
|
|
if (headerByte) {
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst + headerByte > endDest) {
|
2003-07-28 01:47:41 +00:00
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// copy the next 'headerByte' pixels from source to
|
|
|
|
// destination
|
2005-02-19 14:02:16 +00:00
|
|
|
memcpy(dst, src, headerByte);
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// increment destination pointer to just after this
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
dst += headerByte;
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// increment source pointer to just after this block
|
2005-02-19 14:02:16 +00:00
|
|
|
src += headerByte;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if we've decompressed all of the data
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst == endDest) {
|
2003-09-23 17:24:45 +00:00
|
|
|
rv = 0; // return "OK"
|
2003-07-28 01:47:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
return rv;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Unwinds a run of 16-colour data into 256-colour palette data.
|
|
|
|
*/
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
void Screen::unwindRaw16(byte *dst, byte *src, uint8 blockSize, byte *colTable) {
|
2003-09-23 17:24:45 +00:00
|
|
|
// for each pair of pixels
|
|
|
|
while (blockSize > 1) {
|
|
|
|
// 1st colour = number in table at position given by upper
|
|
|
|
// nibble of source byte
|
2005-02-19 14:02:16 +00:00
|
|
|
*dst++ = colTable[(*src) >> 4];
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// 2nd colour = number in table at position given by lower
|
|
|
|
// nibble of source byte
|
2005-02-19 14:02:16 +00:00
|
|
|
*dst++ = colTable[(*src) & 0x0f];
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// point to next source byte
|
2005-02-19 14:02:16 +00:00
|
|
|
src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// decrement count of how many pixels left to read
|
|
|
|
blockSize -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's a final odd pixel
|
|
|
|
if (blockSize) {
|
|
|
|
// colour = number in table at position given by upper nibble
|
|
|
|
// of source byte
|
2005-02-19 14:02:16 +00:00
|
|
|
*dst++ = colTable[(*src) >> 4];
|
2003-09-23 17:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* This function takes a compressed frame of a sprite (with up to 16 colours)
|
|
|
|
* and decompresses it.
|
2005-02-19 14:02:16 +00:00
|
|
|
* @param dst destination buffer
|
|
|
|
* @param src source buffer
|
2003-09-28 14:13:57 +00:00
|
|
|
* @param decompSize the expected size of the uncompressed sprite
|
|
|
|
* @param colTable mapping from the 16 encoded colours to the current palette
|
|
|
|
*/
|
2003-09-23 17:24:45 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
int32 Screen::decompressRLE16(byte *dst, byte *src, int32 decompSize, byte *colTable) {
|
2004-04-23 07:02:11 +00:00
|
|
|
byte headerByte; // block header byte
|
2005-02-19 14:02:16 +00:00
|
|
|
byte *endDest = dst + decompSize; // pointer to byte after end of decomp buffer
|
2003-07-28 01:47:41 +00:00
|
|
|
int32 rv;
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
while (1) {
|
2003-07-28 01:47:41 +00:00
|
|
|
// FLAT block
|
2003-09-23 17:24:45 +00:00
|
|
|
// read FLAT block header & increment 'scan' to first pixel
|
|
|
|
// of block
|
2005-02-19 14:02:16 +00:00
|
|
|
headerByte = *src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if this isn't a zero-length block
|
|
|
|
if (headerByte) {
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst + headerByte > endDest) {
|
2003-07-28 01:47:41 +00:00
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// set the next 'headerByte' pixels to the next
|
|
|
|
// colour at 'source'
|
2005-02-19 14:02:16 +00:00
|
|
|
memset(dst, *src, headerByte);
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// increment destination pointer to just after this
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
dst += headerByte;
|
2003-09-23 17:24:45 +00:00
|
|
|
|
|
|
|
// increment source pointer to just after this colour
|
2005-02-19 14:02:16 +00:00
|
|
|
src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if we've decompressed all of the data
|
2005-07-30 21:11:48 +00:00
|
|
|
if (dst == endDest) {
|
2003-09-23 17:24:45 +00:00
|
|
|
rv = 0; // return "OK"
|
2003-07-28 01:47:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-09-23 17:24:45 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
// RAW block
|
2003-09-23 17:24:45 +00:00
|
|
|
// read RAW block header & increment 'scan' to first pixel of
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
headerByte = *src++;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if this isn't a zero-length block
|
|
|
|
if (headerByte) {
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst + headerByte > endDest) {
|
2003-07-28 01:47:41 +00:00
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// copy the next 'headerByte' pixels from source to
|
|
|
|
// destination (NB. 2 pixels per byte)
|
2005-02-19 14:02:16 +00:00
|
|
|
unwindRaw16(dst, src, headerByte, colTable);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// increment destination pointer to just after this
|
|
|
|
// block
|
2005-02-19 14:02:16 +00:00
|
|
|
dst += headerByte;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// increment source pointer to just after this block
|
|
|
|
// (NB. headerByte gives pixels, so /2 for bytes)
|
2005-02-19 14:02:16 +00:00
|
|
|
src += (headerByte + 1) / 2;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// if we've decompressed all of the data
|
2005-02-19 14:02:16 +00:00
|
|
|
if (dst >= endDest) {
|
2003-09-23 17:24:45 +00:00
|
|
|
rv = 0; // return "OK"
|
|
|
|
break;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
return rv;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Creates a sprite surface. Sprite surfaces are used by the in-game dialogs
|
|
|
|
* and for displaying cutscene subtitles, which makes them much easier to draw
|
|
|
|
* than standard sprites.
|
|
|
|
* @param s information about how to decode the sprite
|
|
|
|
* @param sprite the buffer that will be created to store the surface
|
|
|
|
* @return RD_OK, or an error code
|
|
|
|
*/
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
int32 Screen::createSurface(SpriteInfo *s, byte **sprite) {
|
2005-05-12 13:12:15 +00:00
|
|
|
*sprite = (byte *)malloc(s->w * s->h);
|
2003-08-23 13:02:21 +00:00
|
|
|
if (!*sprite)
|
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-12-10 08:01:58 +00:00
|
|
|
// Surfaces are either uncompressed or RLE256-compressed. No need to
|
|
|
|
// test for anything else.
|
|
|
|
|
2003-08-23 13:02:21 +00:00
|
|
|
if (s->type & RDSPR_NOCOMPRESSION) {
|
|
|
|
memcpy(*sprite, s->data, s->w * s->h);
|
2003-12-10 08:01:58 +00:00
|
|
|
} else if (decompressRLE256(*sprite, s->data, s->w * s->h)) {
|
|
|
|
free(*sprite);
|
|
|
|
return RDERR_DECOMPRESSION;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-09-08 06:38:16 +00:00
|
|
|
|
2003-08-23 13:02:21 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Draws the sprite surface created earlier.
|
|
|
|
* @param s information about how to place the sprite
|
|
|
|
* @param surface pointer to the surface created earlier
|
|
|
|
* @param clipRect the clipping rectangle
|
|
|
|
*/
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
void Screen::drawSurface(SpriteInfo *s, byte *surface, Common::Rect *clipRect) {
|
2003-10-02 17:43:02 +00:00
|
|
|
Common::Rect rd, rs;
|
2003-12-10 08:01:58 +00:00
|
|
|
uint16 x, y;
|
2004-04-23 07:02:11 +00:00
|
|
|
byte *src, *dst;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
rs.left = 0;
|
|
|
|
rs.right = s->w;
|
|
|
|
rs.top = 0;
|
|
|
|
rs.bottom = s->h;
|
2003-08-23 13:02:21 +00:00
|
|
|
|
2003-12-10 08:01:58 +00:00
|
|
|
rd.left = s->x;
|
2003-08-23 13:02:21 +00:00
|
|
|
rd.right = rd.left + rs.right;
|
2003-12-10 08:01:58 +00:00
|
|
|
rd.top = s->y;
|
2003-08-23 13:02:21 +00:00
|
|
|
rd.bottom = rd.top + rs.bottom;
|
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
Common::Rect defClipRect(0, 0, _screenWide, _screenDeep);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
if (!clipRect) {
|
|
|
|
clipRect = &defClipRect;
|
|
|
|
}
|
2003-08-23 13:02:21 +00:00
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
if (clipRect->left > rd.left) {
|
|
|
|
rs.left += (clipRect->left - rd.left);
|
|
|
|
rd.left = clipRect->left;
|
|
|
|
}
|
2003-08-23 13:02:21 +00:00
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
if (clipRect->top > rd.top) {
|
|
|
|
rs.top += (clipRect->top - rd.top);
|
|
|
|
rd.top = clipRect->top;
|
|
|
|
}
|
2003-09-08 06:38:16 +00:00
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
if (clipRect->right < rd.right) {
|
|
|
|
rd.right = clipRect->right;
|
2003-09-08 06:38:16 +00:00
|
|
|
}
|
|
|
|
|
2005-12-20 08:56:48 +00:00
|
|
|
if (clipRect->bottom < rd.bottom) {
|
|
|
|
rd.bottom = clipRect->bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rd.width() <= 0 || rd.height() <= 0)
|
|
|
|
return;
|
|
|
|
|
2003-12-10 08:01:58 +00:00
|
|
|
src = surface + rs.top * s->w + rs.left;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst = _buffer + _screenWide * rd.top + rd.left;
|
2003-08-23 13:02:21 +00:00
|
|
|
|
2003-09-08 06:38:16 +00:00
|
|
|
// Surfaces are always transparent.
|
|
|
|
|
|
|
|
for (y = 0; y < rd.height(); y++) {
|
|
|
|
for (x = 0; x < rd.width(); x++) {
|
|
|
|
if (src[x])
|
|
|
|
dst[x] = src[x];
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-12-10 08:01:58 +00:00
|
|
|
src += s->w;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst += _screenWide;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
updateRect(&rd);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Destroys a surface.
|
|
|
|
*/
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
void Screen::deleteSurface(byte *surface) {
|
2003-08-23 13:02:21 +00:00
|
|
|
free(surface);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Draws a sprite onto the screen. The type of the sprite can be a combination
|
|
|
|
* of the following flags, some of which are mutually exclusive:
|
|
|
|
* RDSPR_DISPLAYALIGN The sprite is drawn relative to the top left corner
|
|
|
|
* of the screen
|
|
|
|
* RDSPR_FLIP The sprite is mirrored
|
|
|
|
* RDSPR_TRANS The sprite has a transparent colour zero
|
|
|
|
* RDSPR_BLEND The sprite is translucent
|
|
|
|
* RDSPR_SHADOW The sprite is affected by the light mask. (Scaled
|
|
|
|
* sprites always are.)
|
|
|
|
* RDSPR_NOCOMPRESSION The sprite data is not compressed
|
|
|
|
* RDSPR_RLE16 The sprite data is a 16-colour compressed sprite
|
|
|
|
* RDSPR_RLE256 The sprite data is a 256-colour compressed sprite
|
|
|
|
* @param s all the information needed to draw the sprite
|
|
|
|
* @warning Sprites will only be drawn onto the background, not over menubar
|
|
|
|
* areas.
|
|
|
|
*/
|
|
|
|
|
2003-09-23 17:24:45 +00:00
|
|
|
// FIXME: I'm sure this could be optimized. There's plenty of data copying and
|
|
|
|
// mallocing here.
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
int32 Screen::drawSprite(SpriteInfo *s) {
|
2004-04-23 07:02:11 +00:00
|
|
|
byte *src, *dst;
|
|
|
|
byte *sprite, *newSprite;
|
2003-08-20 06:51:35 +00:00
|
|
|
uint16 scale;
|
2003-08-19 08:47:09 +00:00
|
|
|
int16 i, j;
|
2003-08-20 06:51:35 +00:00
|
|
|
uint16 srcPitch;
|
|
|
|
bool freeSprite = false;
|
2003-10-02 17:43:02 +00:00
|
|
|
Common::Rect rd, rs;
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// Decompression and mirroring
|
|
|
|
// -----------------------------------------------------------------
|
2003-08-19 08:47:09 +00:00
|
|
|
|
|
|
|
if (s->type & RDSPR_NOCOMPRESSION)
|
|
|
|
sprite = s->data;
|
|
|
|
else {
|
2005-05-12 13:12:15 +00:00
|
|
|
sprite = (byte *)malloc(s->w * s->h);
|
2003-08-20 06:51:35 +00:00
|
|
|
freeSprite = true;
|
2003-08-19 08:47:09 +00:00
|
|
|
if (!sprite)
|
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-12-10 08:01:58 +00:00
|
|
|
if ((s->type & 0xff00) == RDSPR_RLE16) {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (decompressRLE16(sprite, s->data, s->w * s->h, s->colourTable)) {
|
2003-08-20 06:51:35 +00:00
|
|
|
free(sprite);
|
2003-08-19 08:47:09 +00:00
|
|
|
return RDERR_DECOMPRESSION;
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
2003-08-19 08:47:09 +00:00
|
|
|
} else {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (decompressRLE256(sprite, s->data, s->w * s->h)) {
|
2003-08-20 06:51:35 +00:00
|
|
|
free(sprite);
|
2003-08-19 08:47:09 +00:00
|
|
|
return RDERR_DECOMPRESSION;
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
if (s->type & RDSPR_FLIP) {
|
2005-05-12 13:12:15 +00:00
|
|
|
newSprite = (byte *)malloc(s->w * s->h);
|
2003-08-20 06:51:35 +00:00
|
|
|
if (newSprite == NULL) {
|
|
|
|
if (freeSprite)
|
2003-08-19 08:47:09 +00:00
|
|
|
free(sprite);
|
2003-08-20 06:51:35 +00:00
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-10-15 06:40:31 +00:00
|
|
|
mirrorSprite(newSprite, sprite, s->w, s->h);
|
2003-08-20 06:51:35 +00:00
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
sprite = newSprite;
|
|
|
|
freeSprite = true;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// Positioning and clipping.
|
|
|
|
// -----------------------------------------------------------------
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2005-05-02 09:20:27 +00:00
|
|
|
int16 spriteX = s->x;
|
|
|
|
int16 spriteY = s->y;
|
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
if (!(s->type & RDSPR_DISPLAYALIGN)) {
|
2005-05-02 09:20:27 +00:00
|
|
|
spriteX += _parallaxScrollX;
|
|
|
|
spriteY += _parallaxScrollY;
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2005-05-02 09:20:27 +00:00
|
|
|
spriteY += MENUDEEP;
|
2003-08-20 06:51:35 +00:00
|
|
|
|
|
|
|
// A scale factor 0 or 256 means don't scale. Why do they use two
|
|
|
|
// different values to mean the same thing? Normalize it here for
|
|
|
|
// convenience.
|
|
|
|
|
|
|
|
scale = (s->scale == 0) ? 256 : s->scale;
|
|
|
|
|
|
|
|
rs.top = 0;
|
|
|
|
rs.left = 0;
|
|
|
|
|
|
|
|
if (scale != 256) {
|
|
|
|
rs.right = s->scaledWidth;
|
|
|
|
rs.bottom = s->scaledHeight;
|
|
|
|
srcPitch = s->scaledWidth;
|
|
|
|
} else {
|
2003-08-19 08:47:09 +00:00
|
|
|
rs.right = s->w;
|
2003-08-20 06:51:35 +00:00
|
|
|
rs.bottom = s->h;
|
|
|
|
srcPitch = s->w;
|
|
|
|
}
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2005-05-02 09:20:27 +00:00
|
|
|
rd.top = spriteY;
|
|
|
|
rd.left = spriteX;
|
2003-08-20 06:51:35 +00:00
|
|
|
|
|
|
|
if (!(s->type & RDSPR_DISPLAYALIGN)) {
|
2003-10-15 06:40:31 +00:00
|
|
|
rd.top -= _scrollY;
|
|
|
|
rd.left -= _scrollX;
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rd.right = rd.left + rs.right;
|
|
|
|
rd.bottom = rd.top + rs.bottom;
|
|
|
|
|
2003-08-30 10:23:40 +00:00
|
|
|
// Check if the sprite would end up completely outside the screen.
|
|
|
|
|
2005-05-02 09:20:27 +00:00
|
|
|
if (rd.left > RENDERWIDE || rd.top > RENDERDEEP + MENUDEEP || rd.right < 0 || rd.bottom < MENUDEEP) {
|
2003-08-30 10:23:40 +00:00
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
return RD_OK;
|
|
|
|
}
|
|
|
|
|
2005-05-02 09:20:27 +00:00
|
|
|
if (rd.top < MENUDEEP) {
|
|
|
|
rs.top = MENUDEEP - rd.top;
|
|
|
|
rd.top = MENUDEEP;
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
2005-05-02 09:20:27 +00:00
|
|
|
if (rd.bottom > RENDERDEEP + MENUDEEP) {
|
|
|
|
rd.bottom = RENDERDEEP + MENUDEEP;
|
2003-08-30 10:23:40 +00:00
|
|
|
rs.bottom = rs.top + (rd.bottom - rd.top);
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
|
|
|
if (rd.left < 0) {
|
2003-08-30 10:23:40 +00:00
|
|
|
rs.left = -rd.left;
|
2003-08-20 06:51:35 +00:00
|
|
|
rd.left = 0;
|
|
|
|
}
|
2005-05-02 09:20:27 +00:00
|
|
|
if (rd.right > RENDERWIDE) {
|
|
|
|
rd.right = RENDERWIDE;
|
2003-08-30 10:23:40 +00:00
|
|
|
rs.right = rs.left + (rd.right - rd.left);
|
2003-08-20 06:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// Scaling
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
if (scale != 256) {
|
|
|
|
if (s->scaledWidth > SCALE_MAXWIDTH || s->scaledHeight > SCALE_MAXHEIGHT) {
|
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
return RDERR_NOTIMPLEMENTED;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-08-20 06:51:35 +00:00
|
|
|
|
2005-05-12 13:12:15 +00:00
|
|
|
newSprite = (byte *)malloc(s->scaledWidth * s->scaledHeight);
|
2003-08-20 06:51:35 +00:00
|
|
|
if (newSprite == NULL) {
|
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-08-20 06:51:35 +00:00
|
|
|
|
2004-06-06 15:40:31 +00:00
|
|
|
if (_renderCaps & RDBLTFX_EDGEBLEND)
|
|
|
|
scaleImageGood(newSprite, s->scaledWidth, s->scaledWidth, s->scaledHeight, sprite, s->w, s->w, s->h, _buffer + _screenWide * rd.top + rd.left);
|
|
|
|
else
|
|
|
|
scaleImageFast(newSprite, s->scaledWidth, s->scaledWidth, s->scaledHeight, sprite, s->w, s->w, s->h);
|
2003-08-19 08:47:09 +00:00
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
sprite = newSprite;
|
|
|
|
freeSprite = true;
|
|
|
|
}
|
|
|
|
|
2003-08-22 07:04:50 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// Light masking
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// The light mask is an optional layer that covers the entire room
|
2003-08-30 10:23:40 +00:00
|
|
|
// and which is used to simulate light and shadows. Scaled sprites
|
|
|
|
// (actors, presumably) are always affected.
|
2003-08-22 07:04:50 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
if ((_renderCaps & RDBLTFX_SHADOWBLEND) && _lightMask && (scale != 256 || (s->type & RDSPR_SHADOW))) {
|
2004-04-23 07:02:11 +00:00
|
|
|
byte *lightMap;
|
2003-08-22 07:04:50 +00:00
|
|
|
|
2004-06-07 05:57:00 +00:00
|
|
|
// Make sure that we never apply the shadow to the original
|
|
|
|
// resource data. This could only ever happen in the
|
|
|
|
// RDSPR_NOCOMPRESSION case.
|
|
|
|
|
2003-08-22 07:04:50 +00:00
|
|
|
if (!freeSprite) {
|
2005-05-12 13:12:15 +00:00
|
|
|
newSprite = (byte *)malloc(s->w * s->h);
|
2003-08-22 07:04:50 +00:00
|
|
|
memcpy(newSprite, sprite, s->w * s->h);
|
|
|
|
sprite = newSprite;
|
|
|
|
freeSprite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = sprite + rs.top * srcPitch + rs.left;
|
2005-05-02 09:20:27 +00:00
|
|
|
lightMap = _lightMask + (rd.top + _scrollY - MENUDEEP) * _locationWide + rd.left + _scrollX;
|
2003-08-22 07:04:50 +00:00
|
|
|
|
2003-09-08 06:38:16 +00:00
|
|
|
for (i = 0; i < rs.height(); i++) {
|
|
|
|
for (j = 0; j < rs.width(); j++) {
|
2003-08-22 07:04:50 +00:00
|
|
|
if (src[j] && lightMap[j]) {
|
2004-06-09 06:33:29 +00:00
|
|
|
uint8 r = ((32 - lightMap[j]) * _palette[src[j] * 4 + 0]) >> 5;
|
|
|
|
uint8 g = ((32 - lightMap[j]) * _palette[src[j] * 4 + 1]) >> 5;
|
|
|
|
uint8 b = ((32 - lightMap[j]) * _palette[src[j] * 4 + 2]) >> 5;
|
2003-10-15 06:40:31 +00:00
|
|
|
src[j] = quickMatch(r, g, b);
|
2003-08-22 07:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
src += srcPitch;
|
2003-10-15 06:40:31 +00:00
|
|
|
lightMap += _locationWide;
|
2003-08-22 07:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// Drawing
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2003-08-22 07:04:50 +00:00
|
|
|
src = sprite + rs.top * srcPitch + rs.left;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst = _buffer + _screenWide * rd.top + rd.left;
|
2003-08-20 06:51:35 +00:00
|
|
|
|
|
|
|
if (s->type & RDSPR_BLEND) {
|
2003-12-09 08:09:18 +00:00
|
|
|
// The original code had two different blending cases. One for
|
|
|
|
// s->blend & 0x01 and one for s->blend & 0x02. However, the
|
|
|
|
// only values that actually appear in the cluster files are
|
|
|
|
// 0, 513 and 1025 so the s->blend & 0x02 case was never used.
|
|
|
|
// Which is just as well since that code made no sense to me.
|
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
if (!(_renderCaps & RDBLTFX_SPRITEBLEND)) {
|
2003-09-08 06:38:16 +00:00
|
|
|
for (i = 0; i < rs.height(); i++) {
|
|
|
|
for (j = 0; j < rs.width(); j++) {
|
2003-08-26 06:53:00 +00:00
|
|
|
if (src[j] && ((i & 1) == (j & 1)))
|
|
|
|
dst[j] = src[j];
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-08-20 06:51:35 +00:00
|
|
|
src += srcPitch;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst += _screenWide;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
2003-08-26 06:53:00 +00:00
|
|
|
} else {
|
2003-12-09 08:09:18 +00:00
|
|
|
uint8 n = s->blend >> 8;
|
|
|
|
|
|
|
|
for (i = 0; i < rs.height(); i++) {
|
|
|
|
for (j = 0; j < rs.width(); j++) {
|
|
|
|
if (src[j]) {
|
2004-06-09 06:33:29 +00:00
|
|
|
uint8 r1 = _palette[src[j] * 4 + 0];
|
|
|
|
uint8 g1 = _palette[src[j] * 4 + 1];
|
|
|
|
uint8 b1 = _palette[src[j] * 4 + 2];
|
|
|
|
uint8 r2 = _palette[dst[j] * 4 + 0];
|
|
|
|
uint8 g2 = _palette[dst[j] * 4 + 1];
|
|
|
|
uint8 b2 = _palette[dst[j] * 4 + 2];
|
2003-12-09 08:09:18 +00:00
|
|
|
|
|
|
|
uint8 r = (r1 * n + r2 * (8 - n)) >> 3;
|
|
|
|
uint8 g = (g1 * n + g2 * (8 - n)) >> 3;
|
|
|
|
uint8 b = (b1 * n + b2 * (8 - n)) >> 3;
|
|
|
|
dst[j] = quickMatch(r, g, b);
|
2003-08-26 06:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
2003-12-09 08:09:18 +00:00
|
|
|
src += srcPitch;
|
|
|
|
dst += _screenWide;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->type & RDSPR_TRANS) {
|
2003-09-08 06:38:16 +00:00
|
|
|
for (i = 0; i < rs.height(); i++) {
|
|
|
|
for (j = 0; j < rs.width(); j++) {
|
2003-08-19 08:47:09 +00:00
|
|
|
if (src[j])
|
|
|
|
dst[j] = src[j];
|
|
|
|
}
|
2003-08-20 06:51:35 +00:00
|
|
|
src += srcPitch;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst += _screenWide;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-09-08 06:38:16 +00:00
|
|
|
for (i = 0; i < rs.height(); i++) {
|
|
|
|
memcpy(dst, src, rs.width());
|
2003-08-20 06:51:35 +00:00
|
|
|
src += srcPitch;
|
2003-10-15 06:40:31 +00:00
|
|
|
dst += _screenWide;
|
2003-08-19 08:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-20 06:51:35 +00:00
|
|
|
if (freeSprite)
|
|
|
|
free(sprite);
|
|
|
|
|
2004-05-09 13:24:07 +00:00
|
|
|
markAsDirty(rd.left, rd.top, rd.right - 1, rd.bottom - 1);
|
2003-09-23 17:24:45 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Opens the light masking sprite for a room.
|
|
|
|
*/
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
int32 Screen::openLightMask(SpriteInfo *s) {
|
2003-08-26 06:53:00 +00:00
|
|
|
// FIXME: The light mask is only needed on higher graphics detail
|
|
|
|
// settings, so to save memory we could simply ignore it on lower
|
|
|
|
// settings. But then we need to figure out how to ensure that it
|
|
|
|
// is properly loaded if the user changes the settings in mid-game.
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
if (_lightMask)
|
2003-08-26 06:53:00 +00:00
|
|
|
return RDERR_NOTCLOSED;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2005-05-12 13:12:15 +00:00
|
|
|
_lightMask = (byte *)malloc(s->w * s->h);
|
2003-10-15 06:40:31 +00:00
|
|
|
if (!_lightMask)
|
2003-08-26 06:53:00 +00:00
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
if (decompressRLE256(_lightMask, s->data, s->w * s->h))
|
2003-08-26 06:53:00 +00:00
|
|
|
return RDERR_DECOMPRESSION;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-08-26 06:53:00 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Closes the light masking sprite for a room.
|
|
|
|
*/
|
|
|
|
|
2005-05-02 05:41:01 +00:00
|
|
|
int32 Screen::closeLightMask() {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (!_lightMask)
|
2003-08-26 06:53:00 +00:00
|
|
|
return RDERR_NOTOPEN;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
free(_lightMask);
|
|
|
|
_lightMask = NULL;
|
2003-08-26 06:53:00 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-10-04 00:52:27 +00:00
|
|
|
|
|
|
|
} // End of namespace Sword2
|