2014-02-19 01:08:58 +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.
|
|
|
|
*
|
2021-12-26 17:47:58 +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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2015-05-09 13:56:27 +00:00
|
|
|
*
|
2014-02-19 01:08:58 +00:00
|
|
|
* 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.
|
2015-05-09 13:56:27 +00:00
|
|
|
*
|
2014-02-19 01:08:58 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-19 01:08:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
|
|
|
#include "engines/util.h"
|
|
|
|
#include "graphics/palette.h"
|
|
|
|
#include "mads/mads.h"
|
|
|
|
#include "mads/msurface.h"
|
2014-03-26 01:20:44 +00:00
|
|
|
#include "mads/staticres.h"
|
2014-02-19 01:08:58 +00:00
|
|
|
|
|
|
|
namespace MADS {
|
|
|
|
|
2014-03-26 02:06:21 +00:00
|
|
|
#define VGA_COLOR_TRANS(x) ((x) * 255 / 63)
|
|
|
|
|
2014-03-01 01:37:42 +00:00
|
|
|
void RGB6::load(Common::SeekableReadStream *f) {
|
2014-03-12 01:48:16 +00:00
|
|
|
r = VGA_COLOR_TRANS(f->readByte());
|
|
|
|
g = VGA_COLOR_TRANS(f->readByte());
|
|
|
|
b = VGA_COLOR_TRANS(f->readByte());
|
2014-03-10 04:00:39 +00:00
|
|
|
_palIndex = f->readByte();
|
|
|
|
_u2 = f->readByte();
|
|
|
|
_flags = f->readByte();
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 01:08:58 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-03-10 04:00:39 +00:00
|
|
|
PaletteUsage::PaletteUsage(MADSEngine *vm) {
|
|
|
|
_vm = vm;
|
2014-04-12 15:00:29 +00:00
|
|
|
_data = nullptr;
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 15:00:29 +00:00
|
|
|
void PaletteUsage::load(Common::Array<UsageEntry> *data) {
|
|
|
|
_data = data;
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteUsage::getKeyEntries(Common::Array<RGB6> &palette) {
|
2014-04-12 15:00:29 +00:00
|
|
|
_data->clear();
|
2014-03-01 01:37:42 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < palette.size(); ++i) {
|
2014-03-10 04:00:39 +00:00
|
|
|
byte *uPtr = &palette[i]._flags;
|
2014-04-12 15:00:29 +00:00
|
|
|
if ((*uPtr & 0x10) && _data->size() < 3) {
|
|
|
|
_data->push_back(UsageEntry(i));
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 04:43:08 +00:00
|
|
|
static bool sortHelper(const PaletteUsage::UsageEntry &ue1, const PaletteUsage::UsageEntry &ue2) {
|
|
|
|
return ue1._sortValue < ue2._sortValue;
|
|
|
|
}
|
2014-03-01 01:37:42 +00:00
|
|
|
|
2014-03-16 04:43:08 +00:00
|
|
|
void PaletteUsage::prioritize(Common::Array<RGB6> &palette) {
|
2014-04-12 15:00:29 +00:00
|
|
|
for (uint i = 0; i < _data->size(); ++i) {
|
|
|
|
RGB6 &palEntry = palette[(*_data)[i]._palIndex];
|
2014-05-04 18:32:58 +00:00
|
|
|
(*_data)[i]._sortValue = _vm->_palette->rgbMerge(palEntry);
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-04-12 15:00:29 +00:00
|
|
|
Common::sort(_data->begin(), _data->end(), sortHelper);
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 05:08:16 +00:00
|
|
|
static bool rangeSorter(const PaletteUsage::UsageRange &ur1, const PaletteUsage::UsageRange &ur2) {
|
|
|
|
return ur1._v2 < ur2._v2;
|
|
|
|
}
|
|
|
|
|
2014-03-11 02:08:55 +00:00
|
|
|
int PaletteUsage::process(Common::Array<RGB6> &palette, uint flags) {
|
2014-03-10 04:00:39 +00:00
|
|
|
int palLow;
|
2014-03-11 02:08:55 +00:00
|
|
|
int palHigh = (flags & 0x800) ? 0x100 : 0xFC;
|
2014-03-10 04:00:39 +00:00
|
|
|
int palIdx;
|
|
|
|
|
2014-04-12 15:08:50 +00:00
|
|
|
PaletteUsage tempUsage(_vm);
|
|
|
|
Common::Array<UsageEntry> tempUsageData;
|
|
|
|
tempUsage.load(&tempUsageData);
|
|
|
|
|
2014-03-11 02:08:55 +00:00
|
|
|
if (flags & 0x4000) {
|
2014-03-10 04:00:39 +00:00
|
|
|
palLow = 0;
|
|
|
|
palIdx = palHigh;
|
|
|
|
} else {
|
|
|
|
palLow = _vm->_palette->_lowRange;
|
|
|
|
if ((PALETTE_COUNT - _vm->_palette->_highRange) > palHigh) {
|
|
|
|
palIdx = palHigh;
|
|
|
|
} else {
|
|
|
|
palIdx = PALETTE_COUNT - _vm->_palette->_highRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int rgbIndex = _vm->_palette->_rgbList.scan();
|
|
|
|
uint32 rgbMask = 1 << rgbIndex;
|
2014-04-12 15:00:29 +00:00
|
|
|
bool noUsageFlag = flags & 0x8000;
|
|
|
|
bool hasUsage = _data != nullptr;
|
2014-03-10 04:00:39 +00:00
|
|
|
bool flag1 = false;
|
|
|
|
|
|
|
|
if (hasUsage) {
|
2014-04-12 15:00:29 +00:00
|
|
|
if (noUsageFlag || _data->size() == 0)
|
2014-03-10 04:00:39 +00:00
|
|
|
hasUsage = false;
|
|
|
|
|
2014-04-12 15:00:29 +00:00
|
|
|
if (noUsageFlag && _data->size() > 0)
|
2014-03-10 04:00:39 +00:00
|
|
|
flag1 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUsage) {
|
2014-04-12 15:08:50 +00:00
|
|
|
tempUsage.getKeyEntries(palette);
|
|
|
|
tempUsage.prioritize(palette);
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int freeIndex;
|
2014-04-20 15:08:25 +00:00
|
|
|
int palCount = getGamePalFreeIndex(&freeIndex);
|
2014-03-16 05:08:16 +00:00
|
|
|
Common::Array<UsageRange> palRange;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
for (uint palIndex = 0; palIndex < palette.size(); ++palIndex) {
|
2014-03-16 05:08:16 +00:00
|
|
|
byte pal2 = palIndex;
|
|
|
|
byte pal1 = 0;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
if (!(palette[palIndex]._flags & 0x80)) {
|
2014-03-16 05:08:16 +00:00
|
|
|
pal1 = 0x40;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
2014-03-11 02:08:55 +00:00
|
|
|
if (palette[palIndex]._flags & 0x60) {
|
2014-03-16 05:08:16 +00:00
|
|
|
pal1 |= 0x20;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
2014-03-16 05:08:16 +00:00
|
|
|
|
|
|
|
palRange.push_back(UsageRange(pal1, pal2));
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 05:08:16 +00:00
|
|
|
Common::sort(palRange.begin(), palRange.end(), rangeSorter);
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-03-11 02:08:55 +00:00
|
|
|
int var3A = (flags & 0x4000) ? 0xffff : 0xfffe;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
for (uint palIndex = 0; palIndex < palette.size(); ++palIndex) {
|
2014-05-31 15:39:28 +00:00
|
|
|
bool changed = false;
|
2014-10-08 05:37:44 +00:00
|
|
|
int newPalIndex = 0xFF;
|
2014-03-16 05:08:16 +00:00
|
|
|
int v1 = palRange[palIndex]._v2;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
if (palette[v1]._flags & 8) {
|
2014-05-31 15:39:28 +00:00
|
|
|
changed = true;
|
2014-06-04 13:30:10 +00:00
|
|
|
newPalIndex = 0xFD;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUsage && palette[v1]._flags & 0x10) {
|
2014-05-31 15:39:28 +00:00
|
|
|
for (uint usageIndex = 0; usageIndex < tempUsage._data->size() && !changed; ++usageIndex) {
|
2014-04-12 15:08:50 +00:00
|
|
|
if ((*tempUsage._data)[usageIndex]._palIndex == palIndex) {
|
2014-05-31 15:39:28 +00:00
|
|
|
changed = true;
|
2014-04-12 15:00:29 +00:00
|
|
|
int dataIndex = MIN(usageIndex, _data->size() - 1);
|
2014-06-04 13:30:10 +00:00
|
|
|
newPalIndex = (*_data)[dataIndex]._palIndex;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag1 && palette[palIndex]._flags & 0x10) {
|
2014-05-31 15:39:28 +00:00
|
|
|
for (uint usageIndex = 0; usageIndex < _data->size() && !changed; ++usageIndex) {
|
2014-04-12 15:00:29 +00:00
|
|
|
if ((*_data)[usageIndex]._palIndex == palIndex) {
|
2014-05-31 15:39:28 +00:00
|
|
|
changed = true;
|
2014-06-04 13:30:10 +00:00
|
|
|
newPalIndex = 0xF0 + usageIndex;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
// Copy data into the high end of the main palette
|
|
|
|
RGB6 &pSrc = palette[palIndex];
|
2014-06-04 13:30:10 +00:00
|
|
|
byte *pDest = &_vm->_palette->_mainPalette[newPalIndex * 3];
|
2014-03-10 04:00:39 +00:00
|
|
|
pDest[0] = pSrc.r;
|
|
|
|
pDest[1] = pSrc.g;
|
|
|
|
pDest[2] = pSrc.b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 15:39:28 +00:00
|
|
|
if (!changed && !noUsageFlag) {
|
2015-01-31 11:42:36 +00:00
|
|
|
int bestHash = (palette[palIndex]._flags & 0x20) ||
|
|
|
|
(((flags & 0x2000) || (palette[palIndex]._flags & 0x40)) &&
|
2014-03-25 02:33:49 +00:00
|
|
|
((flags & 0x1000) || (palCount == 0))) ? 0x7fff : 1;
|
2014-03-10 04:00:39 +00:00
|
|
|
int var36 = (palette[palIndex]._flags & 0x80) ? 0 : 2;
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-03-10 04:00:39 +00:00
|
|
|
for (int idx = palLow; idx < palIdx; ++idx) {
|
2014-03-12 01:40:49 +00:00
|
|
|
uint32 v = _vm->_palette->_palFlags[idx];
|
2014-03-10 04:52:33 +00:00
|
|
|
if ((v & var3A) && !(v & var36)) {
|
2015-01-31 11:42:36 +00:00
|
|
|
int hash;
|
|
|
|
if (bestHash > 1) {
|
|
|
|
hash = rgbFactor(&_vm->_palette->_mainPalette[idx * 3], palette[palIndex]);
|
|
|
|
} else if (_vm->_palette->_mainPalette[idx * 3] != palette[palIndex].r ||
|
2014-03-10 04:52:33 +00:00
|
|
|
_vm->_palette->_mainPalette[idx * 3 + 1] != palette[palIndex].g ||
|
|
|
|
_vm->_palette->_mainPalette[idx * 3 + 2] != palette[palIndex].b) {
|
2015-01-31 11:42:36 +00:00
|
|
|
hash = 1;
|
2014-03-10 04:52:33 +00:00
|
|
|
} else {
|
2015-01-31 11:42:36 +00:00
|
|
|
hash = 0;
|
2014-03-10 04:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 11:42:36 +00:00
|
|
|
if (bestHash > hash) {
|
2014-05-31 15:39:28 +00:00
|
|
|
changed = true;
|
2014-06-04 13:30:10 +00:00
|
|
|
newPalIndex = idx;
|
2015-01-31 11:42:36 +00:00
|
|
|
bestHash = hash;
|
2014-03-10 04:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:52:33 +00:00
|
|
|
|
2014-05-31 15:39:28 +00:00
|
|
|
if (!changed && (!(flags & 0x1000) || (!(palette[palIndex]._flags & 0x60) && !(flags & 0x2000)))) {
|
|
|
|
for (int idx = freeIndex; idx < palIdx && !changed; ++idx) {
|
2014-03-12 01:40:49 +00:00
|
|
|
if (!_vm->_palette->_palFlags[idx]) {
|
2014-03-10 04:52:33 +00:00
|
|
|
--palCount;
|
|
|
|
++freeIndex;
|
2014-05-31 15:39:28 +00:00
|
|
|
changed = true;
|
2014-06-04 13:30:10 +00:00
|
|
|
newPalIndex = idx;
|
2014-03-10 04:52:33 +00:00
|
|
|
|
|
|
|
RGB6 &pSrc = palette[palIndex];
|
|
|
|
byte *pDest = &_vm->_palette->_mainPalette[idx * 3];
|
|
|
|
pDest[0] = pSrc.r;
|
|
|
|
pDest[1] = pSrc.g;
|
|
|
|
pDest[2] = pSrc.b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-05-31 15:39:28 +00:00
|
|
|
// TODO: Not sure if it's valid or not for changed flag to ever be false.
|
|
|
|
// In at least scene 318, when the doctor knocks you with the blackjack,
|
|
|
|
// the changed flag can be false
|
|
|
|
//assert(changed);
|
2014-10-08 05:37:44 +00:00
|
|
|
|
|
|
|
// CHECKME: When pressing on F1 in the first screen, newPalIndex is set to 0xFF at this point
|
|
|
|
// which is a valid value for the index. Maybe a better check would be "< 256" ?
|
|
|
|
//assert(newPalIndex != -1);
|
2014-10-28 14:19:26 +00:00
|
|
|
|
2014-04-12 15:00:29 +00:00
|
|
|
int var52 = (noUsageFlag && palette[palIndex]._u2) ? 2 : 0;
|
2014-03-10 04:52:33 +00:00
|
|
|
|
2014-06-04 13:30:10 +00:00
|
|
|
_vm->_palette->_palFlags[newPalIndex] |= var52 | rgbMask;
|
|
|
|
palette[palIndex]._palIndex = newPalIndex;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
_vm->_palette->_rgbList[rgbIndex] = true;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
return rgbIndex;
|
|
|
|
}
|
|
|
|
|
2014-03-01 01:37:42 +00:00
|
|
|
void PaletteUsage::transform(Common::Array<RGB6> &palette) {
|
|
|
|
if (!empty()) {
|
2014-04-12 15:00:29 +00:00
|
|
|
for (uint i = 0; i < _data->size(); ++i) {
|
|
|
|
int palIndex = (*_data)[i]._palIndex;
|
|
|
|
(*_data)[i]._palIndex = palette[palIndex]._palIndex;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:54:50 +00:00
|
|
|
void PaletteUsage::updateUsage(Common::Array<int> &usageList, int sceneUsageIndex) {
|
|
|
|
uint32 mask1 = 0xFFFFFFFF;
|
|
|
|
uint32 mask2 = 0;
|
|
|
|
|
|
|
|
for (uint idx = 0; idx < usageList.size(); ++idx) {
|
|
|
|
uint32 bitMask = 1 << usageList[idx];
|
|
|
|
mask1 ^= bitMask;
|
|
|
|
mask2 |= bitMask;
|
2014-03-29 08:41:55 +00:00
|
|
|
_vm->_palette->_rgbList[usageList[idx]] = false;
|
2014-03-13 00:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 mask3 = 1 << sceneUsageIndex;
|
|
|
|
|
|
|
|
for (uint idx = 0; idx < PALETTE_COUNT; ++idx) {
|
|
|
|
uint32 mask = mask2 & _vm->_palette->_palFlags[idx];
|
|
|
|
if (mask) {
|
|
|
|
_vm->_palette->_palFlags[idx] = (_vm->_palette->_palFlags[idx] &
|
|
|
|
mask1) | mask3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
_vm->_palette->_rgbList[sceneUsageIndex] = true;
|
2014-03-13 00:54:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 20:14:57 +00:00
|
|
|
void PaletteUsage::resetPalFlags(int idx) {
|
|
|
|
if (idx >= 0 && idx < 32) {
|
|
|
|
uint32 rgbMask = ~(1 << idx);
|
|
|
|
|
|
|
|
uint32 *flagP = _vm->_palette->_palFlags;
|
|
|
|
for (int i = 0; i < 256; ++i, ++flagP) {
|
|
|
|
*flagP &= rgbMask;
|
|
|
|
if (*flagP == 2)
|
|
|
|
*flagP = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-01 15:48:19 +00:00
|
|
|
_vm->_palette->_rgbList[idx] = false;
|
2014-04-12 20:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:00:39 +00:00
|
|
|
int PaletteUsage::getGamePalFreeIndex(int *palIndex) {
|
|
|
|
*palIndex = -1;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < PALETTE_COUNT; ++i) {
|
2014-03-12 01:40:49 +00:00
|
|
|
if (!_vm->_palette->_palFlags[i]) {
|
2014-03-10 04:00:39 +00:00
|
|
|
++count;
|
|
|
|
if (*palIndex < 0)
|
|
|
|
*palIndex = i;
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:52:33 +00:00
|
|
|
int PaletteUsage::rgbFactor(byte *palEntry, RGB6 &pal6) {
|
|
|
|
int total = 0;
|
|
|
|
total += (palEntry[0] - pal6.r) * (palEntry[0] - pal6.r);
|
|
|
|
total += (palEntry[1] - pal6.g) * (palEntry[1] - pal6.g);
|
|
|
|
total += (palEntry[2] - pal6.b) * (palEntry[2] - pal6.b);
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:28:34 +00:00
|
|
|
int PaletteUsage::checkRGB(const byte *rgb, int palStart, bool flag, int *palIndex) {
|
|
|
|
Palette &palette = *_vm->_palette;
|
|
|
|
bool match = false;
|
|
|
|
int result;
|
|
|
|
if (palStart >= 0) {
|
|
|
|
result = palStart;
|
|
|
|
} else {
|
|
|
|
result = -1;
|
|
|
|
for (int i = 0; i < palette._highRange; ++i) {
|
|
|
|
if (!palette._rgbList[i]) {
|
|
|
|
result = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
int mask = 1 << result;
|
|
|
|
byte *palP = &palette._mainPalette[0];
|
|
|
|
uint32 *flagsP = &palette._palFlags[0];
|
|
|
|
|
|
|
|
for (; flagsP < &palette._palFlags[PALETTE_COUNT]; ++flagsP, ++result) {
|
|
|
|
if ((!(*flagsP & 1) || flag) && !(*flagsP & 2)) {
|
|
|
|
if (!memcmp(palP, rgb, 3)) {
|
|
|
|
*flagsP |= mask;
|
2014-10-28 14:19:26 +00:00
|
|
|
|
2014-10-14 00:28:34 +00:00
|
|
|
if (palIndex)
|
|
|
|
*palIndex = result;
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
palP = &palette._mainPalette[0];
|
|
|
|
flagsP = &palette._palFlags[0];
|
|
|
|
|
|
|
|
for (int i = 0; i < PALETTE_COUNT; ++i, palP += 3, ++flagsP) {
|
|
|
|
if (!*flagsP) {
|
|
|
|
Common::copy(rgb, rgb + 3, palP);
|
|
|
|
*flagsP |= mask;
|
|
|
|
|
|
|
|
if (palIndex)
|
|
|
|
*palIndex = i;
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(match);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:00:39 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void RGBList::clear() {
|
2014-03-29 08:41:55 +00:00
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
_data[i] = false;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RGBList::reset() {
|
2014-03-29 08:41:55 +00:00
|
|
|
for (int i = 2; i < 32; i++)
|
|
|
|
_data[i] = false;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int RGBList::scan() {
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
if (!_data[i])
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
error("RGBList was full");
|
2014-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 18:32:58 +00:00
|
|
|
void RGBList::copy(RGBList &src) {
|
|
|
|
Common::copy(&src._data[0], &src._data[32], &_data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 22:58:25 +00:00
|
|
|
Fader::Fader(MADSEngine *vm)
|
|
|
|
: _vm(vm) {
|
2014-05-04 18:32:58 +00:00
|
|
|
_colorFlags[0] = _colorFlags[1] = _colorFlags[2] = true;
|
|
|
|
_colorFlags[3] = false;
|
|
|
|
_colorValues[0] = _colorValues[1] = 0;
|
|
|
|
_colorValues[2] = _colorValues[3] = 0;
|
2014-05-24 17:32:58 +00:00
|
|
|
|
|
|
|
// TODO: It would be better if the fader routines could be refactored
|
|
|
|
// to work directly with 8-bit RGB values rather than 6-bit RGB values
|
|
|
|
Common::fill(&_rgb64Map[0], &_rgb64Map[PALETTE_COUNT], 0);
|
|
|
|
for (int i = 0; i < 64; ++i)
|
|
|
|
_rgb64Map[VGA_COLOR_TRANS(i)] = i;
|
|
|
|
byte v = 0;
|
|
|
|
for (int i = 0; i < PALETTE_COUNT; ++i) {
|
|
|
|
if (_rgb64Map[i])
|
|
|
|
v = _rgb64Map[i];
|
|
|
|
else
|
|
|
|
_rgb64Map[i] = v;
|
|
|
|
}
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-05-04 22:42:34 +00:00
|
|
|
|
|
|
|
void Fader::setPalette(const byte *colors, uint start, uint num) {
|
|
|
|
g_system->getPaletteManager()->setPalette(colors, start, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fader::grabPalette(byte *colors, uint start, uint num) {
|
|
|
|
g_system->getPaletteManager()->grabPalette(colors, start, num);
|
|
|
|
}
|
|
|
|
|
2015-02-14 20:34:11 +00:00
|
|
|
void Fader::getFullPalette(byte palette[PALETTE_SIZE]) {
|
|
|
|
grabPalette(&palette[0], 0, PALETTE_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fader::setFullPalette(byte palette[PALETTE_SIZE]) {
|
|
|
|
setPalette(&palette[0], 0, PALETTE_COUNT);
|
|
|
|
}
|
|
|
|
|
2014-05-24 17:41:22 +00:00
|
|
|
void Fader::fadeOut(byte palette[PALETTE_SIZE], byte *paletteMap,
|
2014-05-04 18:32:58 +00:00
|
|
|
int baseColor, int numColors, int baseGrey, int numGreys,
|
|
|
|
int tickDelay, int steps) {
|
|
|
|
GreyEntry map[PALETTE_COUNT];
|
|
|
|
int intensity;
|
|
|
|
byte palIndex[PALETTE_COUNT][3];
|
2014-05-10 00:06:35 +00:00
|
|
|
int8 signs[PALETTE_COUNT][3];
|
2014-05-04 18:32:58 +00:00
|
|
|
|
|
|
|
mapToGreyRamp(palette, baseColor, numColors, baseGrey, numGreys, map);
|
|
|
|
|
|
|
|
for (int palCtr = baseColor; palCtr < (baseColor + numColors); ++palCtr) {
|
|
|
|
int index = palCtr - baseColor;
|
|
|
|
for (int colorCtr = 0; colorCtr < 3; ++colorCtr) {
|
|
|
|
if (_colorFlags[colorCtr]) {
|
|
|
|
int shiftSign = _colorValues[colorCtr];
|
|
|
|
if (shiftSign >= 0) {
|
|
|
|
intensity = map[index]._intensity << shiftSign;
|
|
|
|
} else {
|
|
|
|
intensity = map[index]._intensity >> ABS(shiftSign);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
intensity = _colorValues[colorCtr];
|
|
|
|
}
|
|
|
|
|
2014-05-24 17:32:58 +00:00
|
|
|
int diff = intensity - _rgb64Map[palette[palCtr * 3 + colorCtr]];
|
2014-05-04 18:32:58 +00:00
|
|
|
palIndex[palCtr][colorCtr] = (byte)ABS(diff);
|
2014-05-05 00:30:52 +00:00
|
|
|
signs[palCtr][colorCtr] = (diff == 0) ? 0 : (diff < 0 ? -1 : 1);
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 22:42:34 +00:00
|
|
|
for (int stepCtr = 0; stepCtr < steps; ++stepCtr) {
|
|
|
|
for (int palCtr = baseColor; palCtr < (baseColor + numColors); ++palCtr) {
|
|
|
|
int index = palCtr - baseColor;
|
|
|
|
for (int colorCtr = 0; colorCtr < 3; ++colorCtr) {
|
|
|
|
map[index]._accum[colorCtr] += palIndex[palCtr][colorCtr];
|
|
|
|
while (map[index]._accum[colorCtr] >= steps) {
|
|
|
|
map[index]._accum[colorCtr] -= steps;
|
2014-05-10 02:53:37 +00:00
|
|
|
|
2014-05-24 17:32:58 +00:00
|
|
|
byte rgb63 = _rgb64Map[palette[palCtr * 3 + colorCtr]] +
|
2014-05-10 02:53:37 +00:00
|
|
|
signs[palCtr][colorCtr];
|
|
|
|
palette[palCtr * 3 + colorCtr] = VGA_COLOR_TRANS(rgb63);
|
2014-05-04 22:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setFullPalette(palette);
|
|
|
|
|
|
|
|
_vm->_events->waitForNextFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paletteMap != nullptr) {
|
|
|
|
for (int palCtr = 0; palCtr < numColors; palCtr++) {
|
|
|
|
paletteMap[palCtr] = map[palCtr]._mapColor;
|
|
|
|
}
|
|
|
|
}
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 18:21:03 +00:00
|
|
|
void Fader::fadeIn(byte palette[PALETTE_SIZE], byte destPalette[PALETTE_SIZE],
|
|
|
|
int baseColor, int numColors, int baseGrey, int numGreys,
|
|
|
|
int tickDelay, int steps) {
|
|
|
|
GreyEntry map[PALETTE_COUNT];
|
2015-05-31 07:21:23 +00:00
|
|
|
byte tempPal[PALETTE_SIZE];
|
2014-05-24 18:21:03 +00:00
|
|
|
int8 signs[PALETTE_COUNT][3];
|
|
|
|
byte palIndex[PALETTE_COUNT][3];
|
|
|
|
int intensity;
|
|
|
|
|
|
|
|
Common::copy(destPalette, destPalette + PALETTE_SIZE, tempPal);
|
|
|
|
|
|
|
|
mapToGreyRamp(tempPal, baseColor, numColors, baseGrey, numGreys, map);
|
|
|
|
|
|
|
|
for (int palCtr = baseColor; palCtr < (baseColor + numColors); ++palCtr) {
|
|
|
|
int index = palCtr - baseColor;
|
|
|
|
for (int colorCtr = 0; colorCtr < 3; ++colorCtr) {
|
|
|
|
if (_colorFlags[colorCtr]) {
|
|
|
|
int shiftSign = _colorValues[colorCtr];
|
2015-01-31 12:01:23 +00:00
|
|
|
if (shiftSign >= 0)
|
2014-05-24 18:21:03 +00:00
|
|
|
intensity = map[index]._intensity << shiftSign;
|
2015-01-31 12:01:23 +00:00
|
|
|
else
|
2014-05-24 18:21:03 +00:00
|
|
|
intensity = map[index]._intensity >> abs(shiftSign);
|
2015-01-31 12:01:23 +00:00
|
|
|
} else
|
2014-05-24 18:21:03 +00:00
|
|
|
intensity = _colorValues[colorCtr];
|
|
|
|
|
|
|
|
int diff = _rgb64Map[destPalette[palCtr * 3 + colorCtr]] - intensity;
|
|
|
|
palIndex[palCtr][colorCtr] = (byte)ABS(diff);
|
|
|
|
signs[palCtr][colorCtr] = (diff == 0) ? 0 : (diff < 0 ? -1 : 1);
|
|
|
|
|
|
|
|
map[index]._accum[colorCtr] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int stepCtr = 0; stepCtr < steps; ++stepCtr) {
|
|
|
|
for (int palCtr = baseColor; palCtr < (baseColor + numColors); ++palCtr) {
|
|
|
|
int index = palCtr - baseColor;
|
|
|
|
for (int colorCtr = 0; colorCtr < 3; ++colorCtr) {
|
|
|
|
map[index]._accum[colorCtr] += palIndex[palCtr][colorCtr];
|
|
|
|
while (map[index]._accum[colorCtr] >= steps) {
|
|
|
|
map[index]._accum[colorCtr] -= steps;
|
|
|
|
|
|
|
|
byte rgb63 = _rgb64Map[palette[palCtr * 3 + colorCtr]] +
|
|
|
|
signs[palCtr][colorCtr];
|
|
|
|
palette[palCtr * 3 + colorCtr] = VGA_COLOR_TRANS(rgb63);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setFullPalette(palette);
|
|
|
|
|
|
|
|
_vm->_events->waitForNextFrame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 18:32:58 +00:00
|
|
|
void Fader::mapToGreyRamp(byte palette[PALETTE_SIZE], int baseColor, int numColors,
|
|
|
|
int baseGrey, int numGreys, GreyEntry *map) {
|
2014-05-10 00:06:35 +00:00
|
|
|
byte greyList[PALETTE_COUNT];
|
|
|
|
byte greyMapping[PALETTE_COUNT];
|
2014-05-04 18:32:58 +00:00
|
|
|
byte greyTable[64];
|
|
|
|
byte greyIntensity[64];
|
|
|
|
int intensity, shiftSign;
|
|
|
|
|
|
|
|
getGreyValues(palette, greyList, baseColor, numColors);
|
|
|
|
greyPopularity(greyList, greyTable, numColors);
|
2014-05-05 00:30:52 +00:00
|
|
|
|
2014-05-04 18:32:58 +00:00
|
|
|
for (int idx = 0; idx < numColors; ++idx) {
|
2014-05-10 00:06:35 +00:00
|
|
|
greyMapping[idx] = idx;
|
2014-05-04 18:32:58 +00:00
|
|
|
Common::fill(&map[idx]._accum[0], &map[idx]._accum[3], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int idx = 0; idx < PALETTE_COUNT; ++idx) {
|
|
|
|
map[idx]._mapColor = (byte)idx;
|
|
|
|
}
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-05-10 00:06:35 +00:00
|
|
|
// Sort the mapping lists
|
|
|
|
insertionSort(numColors, greyList, greyMapping);
|
2014-05-04 18:32:58 +00:00
|
|
|
|
2014-05-26 22:58:25 +00:00
|
|
|
// Initialize state variables
|
2014-05-04 18:32:58 +00:00
|
|
|
int greySum = 0;
|
|
|
|
int greyScan = 0;
|
|
|
|
int greyMark = 0;
|
|
|
|
int greyColors = 0;
|
|
|
|
int greyAccum = 0;
|
|
|
|
int firstColor = 0;
|
|
|
|
|
2014-05-07 02:49:57 +00:00
|
|
|
for (int greyCtr = 0; greyCtr < 64; ++greyCtr) {
|
|
|
|
for (int idx = 0; idx < greyTable[greyCtr]; ++idx) {
|
2014-05-10 00:06:35 +00:00
|
|
|
greySum += greyList[greyScan++];
|
2014-05-04 18:32:58 +00:00
|
|
|
++greyColors;
|
|
|
|
|
|
|
|
greyAccum += numGreys;
|
|
|
|
while (greyAccum >= numColors) {
|
|
|
|
greyAccum -= numColors;
|
|
|
|
if (greyColors > 0) {
|
|
|
|
greyIntensity[greyMark] = (byte)(greySum / greyColors);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int rescan = firstColor; rescan < greyScan; ++rescan) {
|
2014-05-10 00:06:35 +00:00
|
|
|
map[greyMapping[rescan]]._intensity = greyIntensity[greyMark];
|
|
|
|
map[greyMapping[rescan]]._mapColor = (byte)(greyMark + baseGrey);
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
firstColor = greyScan;
|
|
|
|
greySum = 0;
|
|
|
|
greyColors = 0;
|
|
|
|
++greyMark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 02:49:57 +00:00
|
|
|
// Set the palette range of greyscale values to be used
|
2014-05-04 18:32:58 +00:00
|
|
|
byte *palP = &palette[baseGrey * 3];
|
|
|
|
for (int greys = 0; greys < numGreys; ++greys) {
|
|
|
|
for (int color = 0; color < 3; ++color) {
|
|
|
|
if (_colorFlags[color]) {
|
|
|
|
shiftSign = (byte)_colorValues[color];
|
|
|
|
if (shiftSign >= 0) {
|
|
|
|
intensity = greyIntensity[greys] << shiftSign;
|
|
|
|
} else {
|
|
|
|
intensity = greyIntensity[greys] >> abs(shiftSign);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
intensity = _colorValues[color];
|
|
|
|
}
|
|
|
|
*palP++ = VGA_COLOR_TRANS(intensity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 08:43:23 +00:00
|
|
|
void Fader::getGreyValues(const byte palette[PALETTE_SIZE],
|
2014-05-10 00:06:35 +00:00
|
|
|
byte greyList[PALETTE_COUNT], int baseColor, int numColors) {
|
2014-05-04 18:32:58 +00:00
|
|
|
const byte *palP = &palette[baseColor * 3];
|
|
|
|
|
2014-05-05 00:30:52 +00:00
|
|
|
for (int i = 0; i < numColors; ++i, palP += 3) {
|
2014-05-04 18:32:58 +00:00
|
|
|
int v = rgbMerge(palP[0], palP[1], palP[2]);
|
2014-05-10 00:06:35 +00:00
|
|
|
greyList[i] = v >> 7;
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:06:35 +00:00
|
|
|
void Fader::greyPopularity(const byte greyList[PALETTE_COUNT],
|
2014-05-04 18:32:58 +00:00
|
|
|
byte greyTable[64], int numColors) {
|
|
|
|
Common::fill(&greyTable[0], &greyTable[64], 0);
|
2014-05-07 02:49:57 +00:00
|
|
|
for (int i = 0; i < numColors; ++i) {
|
2014-05-10 00:06:35 +00:00
|
|
|
int idx = greyList[i];
|
2014-05-05 00:30:52 +00:00
|
|
|
++greyTable[idx];
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:06:35 +00:00
|
|
|
void Fader::insertionSort(int size, byte *id, byte *value) {
|
|
|
|
bool restartFlag;
|
|
|
|
int endIndex = size - 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
restartFlag = false;
|
|
|
|
if (endIndex <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (int arrIndex = 0; arrIndex < endIndex && !restartFlag; ++arrIndex) {
|
|
|
|
byte *idP = id + arrIndex;
|
|
|
|
byte *valueP = value + arrIndex;
|
|
|
|
|
|
|
|
// Check whether the next index is out of order with the one following it
|
|
|
|
if (*idP > *(idP + 1)) {
|
|
|
|
// Found an incorrect ordering
|
|
|
|
restartFlag = true;
|
|
|
|
|
|
|
|
// Save id/value at current index
|
|
|
|
byte savedId = *idP;
|
|
|
|
byte savedValue = *valueP;
|
|
|
|
|
|
|
|
int moveCount = size - arrIndex - 1;
|
|
|
|
if (moveCount > 0) {
|
2016-10-26 20:27:19 +00:00
|
|
|
Common::copy(idP + 1, idP + moveCount + 1, idP);
|
|
|
|
Common::copy(valueP + 1, valueP + moveCount + 1, valueP);
|
2014-05-10 00:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for insert spot
|
|
|
|
int idx = 0;
|
|
|
|
if (endIndex > 0) {
|
|
|
|
bool breakFlag = false;
|
2016-10-26 20:27:19 +00:00
|
|
|
for (; idx <= endIndex - 1 && !breakFlag; ++idx) {
|
2014-05-10 00:06:35 +00:00
|
|
|
breakFlag = savedId < id[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up an insert point for entry
|
|
|
|
moveCount = size - idx - 1;
|
|
|
|
if (moveCount > 0) {
|
|
|
|
Common::copy_backward(id + idx, id + idx + moveCount, id + idx + moveCount + 1);
|
|
|
|
Common::copy_backward(value + idx, value + idx + moveCount, value + idx + moveCount + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set shifted values at the new position
|
|
|
|
id[idx] = savedId;
|
|
|
|
value[idx] = savedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (restartFlag);
|
|
|
|
}
|
|
|
|
|
2014-05-04 18:32:58 +00:00
|
|
|
int Fader::rgbMerge(RGB6 &palEntry) {
|
|
|
|
return rgbMerge(palEntry.r, palEntry.g, palEntry.b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fader::rgbMerge(byte r, byte g, byte b) {
|
2014-05-24 17:32:58 +00:00
|
|
|
return _rgb64Map[r] * 38 + _rgb64Map[g] * 76 + _rgb64Map[b] * 14;
|
2014-05-04 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 01:37:42 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-04 22:42:34 +00:00
|
|
|
Palette::Palette(MADSEngine *vm) : Fader(vm), _paletteUsage(vm) {
|
2014-03-29 08:41:55 +00:00
|
|
|
_lockFl = false;
|
2014-03-10 04:00:39 +00:00
|
|
|
_lowRange = 0;
|
|
|
|
_highRange = 0;
|
2014-02-22 05:24:39 +00:00
|
|
|
Common::fill(&_mainPalette[0], &_mainPalette[PALETTE_SIZE], 0);
|
2014-04-12 20:14:57 +00:00
|
|
|
Common::fill(&_palFlags[0], &_palFlags[PALETTE_COUNT], 0);
|
2014-02-19 01:08:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 01:45:02 +00:00
|
|
|
void Palette::setEntry(byte palIndex, byte r, byte g, byte b) {
|
2014-03-15 21:38:44 +00:00
|
|
|
_mainPalette[palIndex * 3] = VGA_COLOR_TRANS(r);
|
|
|
|
_mainPalette[palIndex * 3 + 1] = VGA_COLOR_TRANS(g);
|
|
|
|
_mainPalette[palIndex * 3 + 2] = VGA_COLOR_TRANS(b);
|
2014-03-06 01:45:02 +00:00
|
|
|
|
|
|
|
setPalette((const byte *)&_mainPalette[palIndex * 3], palIndex, 1);
|
|
|
|
}
|
|
|
|
|
2014-02-20 02:28:54 +00:00
|
|
|
uint8 Palette::palIndexFromRgb(byte r, byte g, byte b, byte *paletteData) {
|
2014-02-19 01:08:58 +00:00
|
|
|
byte index = 0;
|
|
|
|
int32 minDist = 0x7fffffff;
|
2014-02-20 02:28:54 +00:00
|
|
|
byte palData[PALETTE_SIZE];
|
2014-02-19 01:08:58 +00:00
|
|
|
|
2021-11-13 21:40:32 +00:00
|
|
|
if (paletteData == nullptr) {
|
2014-02-20 02:28:54 +00:00
|
|
|
g_system->getPaletteManager()->grabPalette(palData, 0, PALETTE_COUNT);
|
2014-02-19 01:08:58 +00:00
|
|
|
paletteData = &palData[0];
|
|
|
|
}
|
|
|
|
|
2014-02-20 02:28:54 +00:00
|
|
|
for (int palIndex = 0; palIndex < PALETTE_COUNT; ++palIndex) {
|
2014-05-19 19:54:25 +00:00
|
|
|
int Rdiff = r - paletteData[palIndex * 3];
|
|
|
|
int Gdiff = g - paletteData[palIndex * 3 + 1];
|
|
|
|
int Bdiff = b - paletteData[palIndex * 3 + 2];
|
2014-02-19 01:08:58 +00:00
|
|
|
|
|
|
|
if (Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff < minDist) {
|
|
|
|
minDist = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff;
|
|
|
|
index = (uint8)palIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (uint8)index;
|
|
|
|
}
|
|
|
|
|
2014-02-22 05:24:39 +00:00
|
|
|
void Palette::setGradient(byte *palette, int start, int count, int rgbValue1, int rgbValue2) {
|
|
|
|
int rgbCurrent = rgbValue2;
|
2014-02-22 17:17:37 +00:00
|
|
|
int rgbDiff = -(rgbValue2 - rgbValue1);
|
2014-02-22 05:24:39 +00:00
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
byte *pDest = palette + start * 3;
|
|
|
|
int endVal = count - 1;
|
|
|
|
int numLeft = count;
|
|
|
|
|
2014-05-19 19:54:25 +00:00
|
|
|
int rgbCtr = 0;
|
2014-02-22 05:24:39 +00:00
|
|
|
do {
|
|
|
|
pDest[0] = pDest[1] = pDest[2] = rgbCurrent;
|
|
|
|
|
2014-02-22 17:17:37 +00:00
|
|
|
if (numLeft > 1) {
|
2014-02-22 05:24:39 +00:00
|
|
|
rgbCtr += rgbDiff;
|
|
|
|
if (rgbCtr >= endVal) {
|
|
|
|
do {
|
|
|
|
++rgbCurrent;
|
|
|
|
rgbCtr += 1 - numLeft;
|
|
|
|
} while (rgbCtr >= endVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pDest += 3;
|
|
|
|
} while (--numLeft > 0);
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 02:28:54 +00:00
|
|
|
|
2014-02-22 05:24:39 +00:00
|
|
|
void Palette::setSystemPalette() {
|
2014-02-20 02:28:54 +00:00
|
|
|
byte palData[4 * 3];
|
|
|
|
palData[0 * 3] = palData[0 * 3 + 1] = palData[0 * 3 + 2] = 0;
|
|
|
|
palData[1 * 3] = palData[1 * 3 + 1] = palData[1 * 3 + 2] = 0x54;
|
|
|
|
palData[2 * 3] = palData[2 * 3 + 1] = palData[2 * 3 + 2] = 0xb4;
|
|
|
|
palData[3 * 3] = palData[3 * 3 + 1] = palData[3 * 3 + 2] = 0xff;
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-02-20 02:28:54 +00:00
|
|
|
setPalette(palData, 0, 4);
|
2014-02-19 01:08:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 16:13:35 +00:00
|
|
|
void Palette::resetGamePalette(int lowRange, int highRange) {
|
2014-03-12 01:40:49 +00:00
|
|
|
Common::fill((byte *)&_palFlags[0], (byte *)&_palFlags[PALETTE_COUNT], 0);
|
2014-03-12 01:32:42 +00:00
|
|
|
initVGAPalette(_mainPalette);
|
2014-02-22 16:13:35 +00:00
|
|
|
|
2014-04-20 15:08:25 +00:00
|
|
|
// Reserve the start of the palette for things like on-screen text
|
2014-02-22 16:13:35 +00:00
|
|
|
if (lowRange) {
|
2014-04-20 15:08:25 +00:00
|
|
|
Common::fill(&_palFlags[0], &_palFlags[lowRange], 1);
|
2014-02-22 16:13:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 15:08:25 +00:00
|
|
|
// Reserve the high end of the palette for dialog display
|
2014-02-22 16:13:35 +00:00
|
|
|
if (highRange) {
|
2014-04-20 15:08:25 +00:00
|
|
|
Common::fill(&_palFlags[256 - highRange], &_palFlags[256], 1);
|
2014-02-22 16:13:35 +00:00
|
|
|
}
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
_rgbList.clear();
|
2014-03-29 08:41:55 +00:00
|
|
|
_rgbList[0] = _rgbList[1] = true;
|
2014-03-11 02:08:55 +00:00
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
_lockFl = false;
|
2014-03-10 04:00:39 +00:00
|
|
|
_lowRange = lowRange;
|
|
|
|
_highRange = highRange;
|
2014-02-22 16:13:35 +00:00
|
|
|
}
|
2014-02-25 14:21:19 +00:00
|
|
|
|
2014-03-12 01:40:49 +00:00
|
|
|
void Palette::initPalette() {
|
2014-03-10 04:52:33 +00:00
|
|
|
uint32 palMask = 1;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
|
|
|
if (_vm->_game->_player._spritesLoaded && _vm->_game->_player._numSprites) {
|
|
|
|
|
|
|
|
for (int idx = 0; idx < _vm->_game->_player._numSprites; ++idx) {
|
|
|
|
SpriteAsset *asset = _vm->_game->_scene._sprites[
|
|
|
|
_vm->_game->_player._spritesStart + idx];
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-03-10 04:00:39 +00:00
|
|
|
uint32 mask = 1;
|
2014-03-10 05:00:11 +00:00
|
|
|
if (asset->_usageIndex)
|
|
|
|
mask <<= asset->_usageIndex;
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-03-10 04:52:33 +00:00
|
|
|
palMask = mask;
|
2014-03-10 04:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:52:33 +00:00
|
|
|
for (int idx = 0; idx < PALETTE_COUNT; ++idx)
|
2014-03-12 01:40:49 +00:00
|
|
|
_palFlags[idx] = palMask;
|
2014-03-10 04:00:39 +00:00
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
_lockFl = false;
|
2014-03-10 04:00:39 +00:00
|
|
|
_rgbList.reset();
|
2014-02-25 14:21:19 +00:00
|
|
|
}
|
2014-02-22 16:13:35 +00:00
|
|
|
|
2014-03-12 01:32:42 +00:00
|
|
|
void Palette::initVGAPalette(byte *palette) {
|
2014-03-12 01:01:07 +00:00
|
|
|
byte *destP = palette;
|
2014-03-26 01:20:44 +00:00
|
|
|
for (int palIndex = 0; palIndex < 16; ++palIndex) {
|
2014-03-15 12:41:17 +00:00
|
|
|
for (int byteCtr = 2; byteCtr >= 0; --byteCtr)
|
2014-05-07 02:49:57 +00:00
|
|
|
*destP++ = ((DEFAULT_VGA_LOW_PALETTE[palIndex] >> (8 * byteCtr)) & 0xff) >> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
destP = &palette[0xF0 * 3];
|
|
|
|
for (int palIndex = 0; palIndex < 16; ++palIndex) {
|
|
|
|
for (int byteCtr = 2; byteCtr >= 0; --byteCtr)
|
|
|
|
*destP++ = ((DEFAULT_VGA_HIGH_PALETTE[palIndex] >> (8 * byteCtr)) & 0xff) >> 2;
|
2014-03-15 12:41:17 +00:00
|
|
|
}
|
2014-02-22 16:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Palette::setLowRange() {
|
2014-03-26 01:20:44 +00:00
|
|
|
_mainPalette[0] = _mainPalette[1] = _mainPalette[2] = VGA_COLOR_TRANS(0);
|
|
|
|
_mainPalette[3] = _mainPalette[4] = _mainPalette[5] = VGA_COLOR_TRANS(0x15);
|
|
|
|
_mainPalette[6] = _mainPalette[7] = _mainPalette[8] = VGA_COLOR_TRANS(0x2A);
|
|
|
|
_mainPalette[9] = _mainPalette[10] = _mainPalette[11] = VGA_COLOR_TRANS(0x3F);
|
2014-02-22 16:13:35 +00:00
|
|
|
_vm->_palette->setPalette(_mainPalette, 0, 4);
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:04:26 +00:00
|
|
|
void Palette::setColorFlags(byte r, byte g, byte b) {
|
|
|
|
_colorFlags[0] = r;
|
|
|
|
_colorFlags[1] = g;
|
|
|
|
_colorFlags[2] = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Palette::setColorValues(byte r, byte g, byte b) {
|
|
|
|
_colorValues[0] = r;
|
|
|
|
_colorValues[1] = g;
|
|
|
|
_colorValues[2] = b;
|
|
|
|
}
|
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
void Palette::lock() {
|
|
|
|
if (_rgbList[31] && !_lockFl)
|
|
|
|
error("Palette Lock - Unexpected values");
|
2014-03-24 00:14:16 +00:00
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
_lockFl = true;
|
|
|
|
_rgbList[31] = true;
|
2014-03-24 00:14:16 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
if (_palFlags[i])
|
|
|
|
_palFlags[i] |= 0x80000000;
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:00:39 +00:00
|
|
|
|
2014-03-29 08:41:55 +00:00
|
|
|
void Palette::unlock() {
|
|
|
|
if (!_lockFl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
_palFlags[i] &= 0x7FFFFFFF;
|
|
|
|
|
|
|
|
_rgbList[31] = false;
|
|
|
|
_lockFl = false;
|
|
|
|
}
|
2014-04-12 21:09:05 +00:00
|
|
|
|
2014-05-02 00:54:17 +00:00
|
|
|
void Palette::refreshSceneColors() {
|
2014-04-12 21:09:05 +00:00
|
|
|
int val = 18;
|
2014-04-13 02:08:21 +00:00
|
|
|
if (_vm->_game->_scene._cyclingActive)
|
2014-04-20 02:49:14 +00:00
|
|
|
val += _vm->_game->_scene._totalCycleColors;
|
2014-04-12 21:09:05 +00:00
|
|
|
|
2014-05-02 00:54:17 +00:00
|
|
|
setPalette(_mainPalette + (val * 3), val, 256 - val);
|
2014-04-12 21:09:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 03:07:42 +00:00
|
|
|
int Palette::closestColor(const byte *matchColor, const byte *refPalette,
|
2015-02-14 20:34:11 +00:00
|
|
|
int paletteInc, int count) {
|
2015-02-09 03:07:42 +00:00
|
|
|
int bestColor = 0;
|
2015-02-09 04:54:51 +00:00
|
|
|
int bestDistance = 0x7fff;
|
2015-02-09 03:07:42 +00:00
|
|
|
|
|
|
|
for (int idx = 0; idx < count; ++idx) {
|
2015-02-14 20:34:11 +00:00
|
|
|
// Figure out figure for 'distance' between two colors
|
2015-02-09 04:54:51 +00:00
|
|
|
int distance = 0;
|
2015-02-14 20:34:11 +00:00
|
|
|
for (int rgbIdx = 0; rgbIdx < RGB_SIZE; ++rgbIdx) {
|
|
|
|
int diff = refPalette[rgbIdx] - matchColor[rgbIdx];
|
|
|
|
distance += diff * diff;
|
2015-02-09 03:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the given color is a closer match to our color, store the index
|
2015-02-14 20:34:11 +00:00
|
|
|
if (distance <= bestDistance) {
|
2015-02-09 04:54:51 +00:00
|
|
|
bestDistance = distance;
|
2015-02-09 03:07:42 +00:00
|
|
|
bestColor = idx;
|
|
|
|
}
|
|
|
|
|
2015-02-14 20:34:11 +00:00
|
|
|
refPalette += paletteInc;
|
2015-02-09 03:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bestColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-19 01:08:58 +00:00
|
|
|
} // End of namespace MADS
|