1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "neverhood/palette.h"
24 #include "neverhood/resource.h"
25 #include "neverhood/screen.h"
26 
27 namespace Neverhood {
28 
29 // Palette
30 
Palette(NeverhoodEngine * vm)31 Palette::Palette(NeverhoodEngine *vm) : Entity(vm, 0) {
32 	init();
33 	memset(_palette, 0, 1024);
34 	SetUpdateHandler(&Palette::update);
35 }
36 
Palette(NeverhoodEngine * vm,byte * palette)37 Palette::Palette(NeverhoodEngine *vm, byte *palette) : Entity(vm, 0) {
38 	init();
39 	memcpy(_palette, palette, 1024);
40 	SetUpdateHandler(&Palette::update);
41 }
42 
Palette(NeverhoodEngine * vm,const char * filename)43 Palette::Palette(NeverhoodEngine *vm, const char *filename) : Entity(vm, 0) {
44 	PaletteResource paletteResource(_vm);
45 	init();
46 	paletteResource.load(calcHash(filename));
47 	paletteResource.copyPalette(_palette);
48 	SetUpdateHandler(&Palette::update);
49 }
50 
Palette(NeverhoodEngine * vm,uint32 fileHash)51 Palette::Palette(NeverhoodEngine *vm, uint32 fileHash) : Entity(vm, 0) {
52 	PaletteResource paletteResource(_vm);
53 	init();
54 	paletteResource.load(fileHash);
55 	paletteResource.copyPalette(_palette);
56 	SetUpdateHandler(&Palette::update);
57 }
58 
~Palette()59 Palette::~Palette() {
60 	_vm->_screen->unsetPaletteData(_palette);
61 	delete[] _palette;
62 	delete[] _basePalette;
63 }
64 
init()65 void Palette::init() {
66 	_status = 0;
67 	_palette = new byte[1024];
68 	_basePalette = new byte[1024];
69 	_palCounter = 0;
70 	_fadeToR = 0;
71 	_fadeToG = 0;
72 	_fadeToB = 0;
73 	_fadeStep = 0;
74 }
75 
usePalette()76 void Palette::usePalette() {
77 	_vm->_screen->setPaletteData(_palette);
78 }
79 
addPalette(const char * filename,int toIndex,int count,int fromIndex)80 void Palette::addPalette(const char *filename, int toIndex, int count, int fromIndex) {
81 	addPalette(calcHash(filename), toIndex, count, fromIndex);
82 }
83 
addPalette(uint32 fileHash,int toIndex,int count,int fromIndex)84 void Palette::addPalette(uint32 fileHash, int toIndex, int count, int fromIndex) {
85 	PaletteResource paletteResource(_vm);
86 	if (toIndex + count > 256)
87 		count = 256 - toIndex;
88 	paletteResource.load(fileHash);
89 	memcpy(_palette + toIndex * 4, paletteResource.palette() + fromIndex * 4, count * 4);
90 	_vm->_screen->testPalette(_palette);
91 }
92 
addBasePalette(uint32 fileHash,int toIndex,int count,int fromIndex)93 void Palette::addBasePalette(uint32 fileHash, int toIndex, int count, int fromIndex) {
94 	PaletteResource paletteResource(_vm);
95 	if (toIndex + count > 256)
96 		count = 256 - toIndex;
97 	paletteResource.load(fileHash);
98 	memcpy(_basePalette + toIndex * 4, paletteResource.palette() + fromIndex * 4, count * 4);
99 }
100 
copyPalette(const byte * palette,int toIndex,int count,int fromIndex)101 void Palette::copyPalette(const byte *palette, int toIndex, int count, int fromIndex) {
102 	if (toIndex + count > 256)
103 		count = 256 - toIndex;
104 	memcpy(_palette + toIndex * 4, palette + fromIndex * 4, count * 4);
105 	_vm->_screen->testPalette(_palette);
106 }
107 
copyBasePalette(int toIndex,int count,int fromIndex)108 void Palette::copyBasePalette(int toIndex, int count, int fromIndex) {
109 	if (toIndex + count > 256)
110 		count = 256 - toIndex;
111 	memcpy(_basePalette + toIndex * 4, _palette + fromIndex * 4, count * 4);
112 }
113 
startFadeToBlack(int counter)114 void Palette::startFadeToBlack(int counter) {
115 	debug(2, "Palette::startFadeToBlack(%d)", counter);
116 	if (counter == 0)
117 		counter = 1;
118 	_fadeToR = 0;
119 	_fadeToG = 0;
120 	_fadeToB = 0;
121 	_palCounter = counter;
122 	_fadeStep = calculateFadeStep(counter);
123 	_status = 1;
124 }
125 
startFadeToWhite(int counter)126 void Palette::startFadeToWhite(int counter) {
127 	debug(2, "Palette::startFadeToWhite(%d)", counter);
128 	if (counter == 0)
129 		counter = 1;
130 	_fadeToR = 255;
131 	_fadeToG = 255;
132 	_fadeToB = 255;
133 	_palCounter = counter;
134 	_fadeStep = calculateFadeStep(counter);
135 	_status = 1;
136 }
137 
startFadeToPalette(int counter)138 void Palette::startFadeToPalette(int counter) {
139 	debug(2, "Palette::startFadeToPalette(%d)", counter);
140 	if (counter == 0)
141 		counter = 1;
142 	_palCounter = counter;
143 	_fadeStep = calculateFadeStep(counter);
144 	_status = 2;
145 }
146 
fillBaseWhite(int index,int count)147 void Palette::fillBaseWhite(int index, int count) {
148 	if (index + count > 256)
149 		count = 256 - index;
150 	for (int i = 0; i < count; i++) {
151 		_basePalette[(i + index) * 4 + 0] = 0xFF;
152 		_basePalette[(i + index) * 4 + 1] = 0xFF;
153 		_basePalette[(i + index) * 4 + 2] = 0xFF;
154 		_basePalette[(i + index) * 4 + 3] = 0;
155 	}
156 }
157 
fillBaseBlack(int index,int count)158 void Palette::fillBaseBlack(int index, int count) {
159 	if (index + count > 256)
160 		count = 256 - index;
161 	for (int i = 0; i < count; i++) {
162 		_basePalette[(i + index) * 4 + 0] = 0;
163 		_basePalette[(i + index) * 4 + 1] = 0;
164 		_basePalette[(i + index) * 4 + 2] = 0;
165 		_basePalette[(i + index) * 4 + 3] = 0;
166 	}
167 }
168 
copyToBasePalette(byte * palette)169 void Palette::copyToBasePalette(byte *palette) {
170 	memcpy(_basePalette, palette, 256 * 4);
171 }
172 
update()173 void Palette::update() {
174 	debug(2, "Palette::update() _status = %d", _status);
175 	if (_status == 1) {
176 		if (_palCounter > 1) {
177 			for (int i = 0; i < 256; i++)
178 				fadeColor(_palette + i * 4, _fadeToR, _fadeToG, _fadeToB);
179 			_vm->_screen->testPalette(_palette);
180 			_palCounter--;
181 		} else {
182 			memset(_palette, 0, 1024);
183 			_status = 0;
184 		}
185 	} else if (_status == 2) {
186 		if (_palCounter > 1) {
187 			for (int i = 0; i < 256; i++)
188 				fadeColor(_palette + i * 4, _basePalette[i * 4 + 0], _basePalette[i * 4 + 1], _basePalette[i * 4 + 2]);
189 			_vm->_screen->testPalette(_palette);
190 			_palCounter--;
191 		} else {
192 			memcpy(_palette, _basePalette, 256 * 4);
193 			_status = 0;
194 		}
195 	}
196 }
197 
fadeColor(byte * rgb,byte toR,byte toG,byte toB)198 void Palette::fadeColor(byte *rgb, byte toR, byte toG, byte toB) {
199 	#define FADE(color, toColor) color += _fadeStep < toColor - color ? _fadeStep : (-_fadeStep <= toColor - color ? toColor - color : -_fadeStep)
200 	FADE(rgb[0], toR);
201 	FADE(rgb[1], toG);
202 	FADE(rgb[2], toB);
203 	#undef FADE
204 }
205 
calculateFadeStep(int counter)206 int Palette::calculateFadeStep(int counter) {
207 	int fadeStep = 255 / counter;
208 	if (255 % counter)
209 		fadeStep++;
210 	return fadeStep;
211 }
212 
213 } // End of namespace Neverhood
214