2004-01-06 13:44:17 +00:00
|
|
|
/* Copyright (C) 1994-2004 Revolution Software Ltd
|
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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-10-28 19:51:30 +00:00
|
|
|
#include "sword2/sword2.h"
|
2004-02-05 14:19:07 +00:00
|
|
|
#include "sword2/driver/d_draw.h"
|
2003-10-04 00:52:27 +00:00
|
|
|
|
|
|
|
namespace Sword2 {
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
uint8 Graphics::getMatch(uint8 r, uint8 g, uint8 b) {
|
2003-07-28 01:47:41 +00:00
|
|
|
int32 diff;
|
|
|
|
int32 min;
|
|
|
|
int16 diffred, diffgreen, diffblue;
|
|
|
|
int16 i;
|
|
|
|
uint8 minIndex;
|
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
diffred = _palCopy[0][0] - r;
|
|
|
|
diffgreen = _palCopy[0][1] - g;
|
|
|
|
diffblue = _palCopy[0][2] - b;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
diff = diffred * diffred + diffgreen * diffgreen + diffblue * diffblue;
|
|
|
|
min = diff;
|
|
|
|
minIndex = 0;
|
2003-08-27 07:01:05 +00:00
|
|
|
if (diff > 0) {
|
2003-10-07 07:07:47 +00:00
|
|
|
for (i = 1; i < 256; i++) {
|
2003-10-15 06:40:31 +00:00
|
|
|
diffred = _palCopy[i][0] - r;
|
|
|
|
diffgreen = _palCopy[i][1] - g;
|
|
|
|
diffblue = _palCopy[i][2] - b;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
diff = diffred * diffred + diffgreen * diffgreen + diffblue * diffblue;
|
2003-08-27 07:01:05 +00:00
|
|
|
if (diff < min) {
|
2003-07-28 01:47:41 +00:00
|
|
|
min = diff;
|
|
|
|
minIndex = (uint8) i;
|
|
|
|
if (min == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here, minIndex is the index of the matchpalette which is closest.
|
2003-09-27 15:30:17 +00:00
|
|
|
return minIndex;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Sets or creates a table of palette indices which will be searched later for
|
|
|
|
* a quick palette match.
|
|
|
|
* @param data either the palette match table, or NULL to create a new table
|
|
|
|
* from the current palCopy
|
|
|
|
*/
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
void Graphics::updatePaletteMatchTable(uint8 *data) {
|
2003-08-27 07:01:05 +00:00
|
|
|
if (!data) {
|
2003-07-28 01:47:41 +00:00
|
|
|
int16 red, green, blue;
|
|
|
|
uint8 *p;
|
|
|
|
|
2003-08-27 07:01:05 +00:00
|
|
|
// Create palette match table
|
2003-09-27 15:30:17 +00:00
|
|
|
|
|
|
|
// FIXME: Does this case ever happen?
|
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
p = &_paletteMatch[0];
|
2003-08-27 07:01:05 +00:00
|
|
|
for (red = 0; red < 256; red += 4) {
|
|
|
|
for (green = 0; green < 256; green += 4) {
|
|
|
|
for (blue = 0; blue < 256; blue += 4) {
|
2003-10-15 06:40:31 +00:00
|
|
|
*p++ = getMatch((uint8) red, (uint8) green, (uint8) blue);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-27 07:01:05 +00:00
|
|
|
} else {
|
2003-10-07 07:07:47 +00:00
|
|
|
// The provided data is the new palette match table
|
2003-10-15 06:40:31 +00:00
|
|
|
memcpy(_paletteMatch, data, PALTABLESIZE);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Matches a colour triplet to a palette index.
|
|
|
|
* @param r red colour component
|
|
|
|
* @param g green colour component
|
|
|
|
* @param b blue colour component
|
|
|
|
* @return the palette index of the closest matching colour in the palette
|
|
|
|
*/
|
|
|
|
|
2003-08-22 07:04:50 +00:00
|
|
|
// FIXME: This used to be inlined - probably a good idea - but the
|
|
|
|
// linker complained when I tried to use it in sprite.cpp.
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
uint8 Graphics::quickMatch(uint8 r, uint8 g, uint8 b) {
|
2003-10-15 06:40:31 +00:00
|
|
|
return _paletteMatch[((int32) (r >> 2) << 12) + ((int32) (g >> 2) << 6) + (b >> 2)];
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the palette.
|
|
|
|
* @param startEntry the first colour entry to set
|
|
|
|
* @param noEntries the number of colour entries to set
|
|
|
|
* @param colourTable the new colour entries
|
|
|
|
*/
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
void Graphics::setPalette(int16 startEntry, int16 noEntries, uint8 *colourTable, uint8 fadeNow) {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (noEntries) {
|
|
|
|
memcpy(&_palCopy[startEntry][0], colourTable, noEntries * 4);
|
2004-01-04 15:11:30 +00:00
|
|
|
if (fadeNow == RDPAL_INSTANT) {
|
2003-11-16 14:18:29 +00:00
|
|
|
_vm->_system->set_palette((const byte *) _palCopy, startEntry, noEntries);
|
2004-01-04 15:11:30 +00:00
|
|
|
setNeedFullRedraw();
|
|
|
|
}
|
|
|
|
} else {
|
2003-11-16 14:18:29 +00:00
|
|
|
_vm->_system->set_palette((const byte *) _palCopy, 0, 256);
|
2004-01-04 15:11:30 +00:00
|
|
|
setNeedFullRedraw();
|
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
void Graphics::dimPalette(void) {
|
2003-10-15 06:40:31 +00:00
|
|
|
byte *p = (byte *) _palCopy;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-07 07:07:47 +00:00
|
|
|
for (int i = 0; i < 256 * 4; i++)
|
2003-08-27 07:01:05 +00:00
|
|
|
p[i] /= 2;
|
2003-10-07 07:07:47 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_vm->_system->set_palette(p, 0, 256);
|
2004-01-04 15:11:30 +00:00
|
|
|
setNeedFullRedraw();
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Fades the palette up from black to the current palette.
|
|
|
|
* @param time the time it will take the palette to fade up
|
|
|
|
*/
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
int32 Graphics::fadeUp(float time) {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (getFadeStatus() != RDFADE_BLACK && getFadeStatus() != RDFADE_NONE)
|
2003-08-27 07:01:05 +00:00
|
|
|
return RDERR_FADEINCOMPLETE;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
_fadeTotalTime = (int32) (time * 1000);
|
|
|
|
_fadeStatus = RDFADE_UP;
|
2003-11-16 14:18:29 +00:00
|
|
|
_fadeStartTime = _vm->_system->get_msecs();
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-07-28 07:22:40 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Fades the palette down to black from the current palette.
|
|
|
|
* @param time the time it will take the palette to fade down
|
|
|
|
*/
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
int32 Graphics::fadeDown(float time) {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (getFadeStatus() != RDFADE_BLACK && getFadeStatus() != RDFADE_NONE)
|
2003-08-27 07:01:05 +00:00
|
|
|
return RDERR_FADEINCOMPLETE;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
_fadeTotalTime = (int32) (time * 1000);
|
|
|
|
_fadeStatus = RDFADE_DOWN;
|
2003-11-16 14:18:29 +00:00
|
|
|
_fadeStartTime = _vm->_system->get_msecs();
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-07-28 07:22:40 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
/**
|
|
|
|
* Get the current fade status
|
|
|
|
* @return RDFADE_UP (fading up), RDFADE_DOWN (fading down), RDFADE_NONE
|
|
|
|
* (not faded), or RDFADE_BLACK (completely faded down)
|
|
|
|
*/
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
uint8 Graphics::getFadeStatus(void) {
|
2003-10-15 06:40:31 +00:00
|
|
|
return _fadeStatus;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
void Graphics::waitForFade(void) {
|
2003-10-15 06:40:31 +00:00
|
|
|
while (getFadeStatus() != RDFADE_NONE && getFadeStatus() != RDFADE_BLACK) {
|
|
|
|
updateDisplay();
|
2003-11-16 14:18:29 +00:00
|
|
|
_vm->_system->delay_msecs(20);
|
2003-09-09 12:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-11 07:43:02 +00:00
|
|
|
void Graphics::fadeServer(void) {
|
2003-08-25 06:13:28 +00:00
|
|
|
static int32 previousTime = 0;
|
2003-10-15 06:40:31 +00:00
|
|
|
const byte *newPalette = (const byte *) _fadePalette;
|
2003-07-31 14:51:02 +00:00
|
|
|
int32 currentTime;
|
|
|
|
int16 fadeMultiplier;
|
|
|
|
int16 i;
|
|
|
|
|
2003-08-25 06:13:28 +00:00
|
|
|
// This used to be called through a timer, but is now called from
|
|
|
|
// ServiceWindows() instead, since that's the only place where we
|
|
|
|
// actually update the screen.
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-08-25 06:13:28 +00:00
|
|
|
// If we're not in the process of fading, do nothing.
|
2003-10-15 06:40:31 +00:00
|
|
|
if (getFadeStatus() != RDFADE_UP && getFadeStatus() != RDFADE_DOWN)
|
2003-08-25 06:13:28 +00:00
|
|
|
return;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-08-25 06:13:28 +00:00
|
|
|
// I don't know if this is necessary, but let's limit how often the
|
|
|
|
// palette is updated, just to be safe.
|
2003-11-16 14:18:29 +00:00
|
|
|
currentTime = _vm->_system->get_msecs();
|
2003-08-25 06:13:28 +00:00
|
|
|
if (currentTime - previousTime <= 25)
|
2003-07-28 01:47:41 +00:00
|
|
|
return;
|
|
|
|
|
2003-08-25 06:13:28 +00:00
|
|
|
previousTime = currentTime;
|
|
|
|
|
2003-10-15 06:40:31 +00:00
|
|
|
if (getFadeStatus() == RDFADE_UP) {
|
|
|
|
if (currentTime >= _fadeStartTime + _fadeTotalTime) {
|
|
|
|
_fadeStatus = RDFADE_NONE;
|
|
|
|
newPalette = (const byte *) _palCopy;
|
2003-08-25 06:13:28 +00:00
|
|
|
} else {
|
2003-10-15 06:40:31 +00:00
|
|
|
fadeMultiplier = (int16) (((int32) (currentTime - _fadeStartTime) * 256) / _fadeTotalTime);
|
2003-08-25 06:13:28 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2003-10-15 06:40:31 +00:00
|
|
|
_fadePalette[i][0] = (_palCopy[i][0] * fadeMultiplier) >> 8;
|
|
|
|
_fadePalette[i][1] = (_palCopy[i][1] * fadeMultiplier) >> 8;
|
|
|
|
_fadePalette[i][2] = (_palCopy[i][2] * fadeMultiplier) >> 8;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-25 06:13:28 +00:00
|
|
|
} else {
|
2003-10-15 06:40:31 +00:00
|
|
|
if (currentTime >= _fadeStartTime + _fadeTotalTime) {
|
|
|
|
_fadeStatus = RDFADE_BLACK;
|
|
|
|
memset(_fadePalette, 0, sizeof(_fadePalette));
|
2003-08-25 06:13:28 +00:00
|
|
|
} else {
|
2003-10-15 06:40:31 +00:00
|
|
|
fadeMultiplier = (int16) (((int32) (_fadeTotalTime - (currentTime - _fadeStartTime)) * 256) / _fadeTotalTime);
|
2003-08-25 06:13:28 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2003-10-15 06:40:31 +00:00
|
|
|
_fadePalette[i][0] = (_palCopy[i][0] * fadeMultiplier) >> 8;
|
|
|
|
_fadePalette[i][1] = (_palCopy[i][1] * fadeMultiplier) >> 8;
|
|
|
|
_fadePalette[i][2] = (_palCopy[i][2] * fadeMultiplier) >> 8;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-25 06:13:28 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_vm->_system->set_palette(newPalette, 0, 256);
|
2004-01-04 15:11:30 +00:00
|
|
|
setNeedFullRedraw();
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-10-04 00:52:27 +00:00
|
|
|
|
|
|
|
} // End of namespace Sword2
|