1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/* 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.
*
* Additional copyright for this file:
* Copyright (C) 1994-1998 Revolution Software Ltd.
*
* 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$
*/
#include "common/system.h"
#include "common/textconsole.h"
#include "graphics/palette.h"
#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/logic.h"
#include "sword2/resman.h"
#include "sword2/screen.h"
namespace Sword2 {
/**
* Start layer palette fading up
*/
void Screen::startNewPalette() {
// If the screen is still fading down then wait for black - could
// happen when everythings cached into a large memory model
waitForFade();
byte *screenFile = _vm->_resman->openResource(_thisScreen.background_layer_id);
// Don't fetch palette match table while using PSX version,
// because it is not present.
if (!Sword2Engine::isPsx())
memcpy(_paletteMatch, _vm->fetchPaletteMatchTable(screenFile), PALTABLESIZE);
_vm->fetchPalette(screenFile, _palette);
setPalette(0, 256, _palette, RDPAL_FADE);
// Indicating that it's a screen palette
_lastPaletteRes = 0;
_vm->_resman->closeResource(_thisScreen.background_layer_id);
fadeUp();
_thisScreen.new_palette = 0;
}
void Screen::setFullPalette(int32 palRes) {
// fudge for hut interior
// - unpausing should restore last palette as normal (could be screen
// palette or 'dark_palette_13')
// - but restoring the screen palette after 'dark_palette_13' should
// now work properly too!
// "Hut interior" refers to the watchman's hut in Marseille, and this
// is apparently needed for the palette to be restored properly when
// you turn the light off. (I didn't even notice the light switch!)
if (_vm->_logic->readVar(LOCATION) == 13) {
// unpausing
if (palRes == -1) {
// restore whatever palette was last set (screen
// palette or 'dark_palette_13')
palRes = _lastPaletteRes;
}
} else {
// check if we're just restoring the current screen palette
// because we might actually need to use a separate palette
// file anyway eg. for pausing & unpausing during the eclipse
// unpausing (fudged for location 13)
if (palRes == -1) {
// we really meant '0'
palRes = 0;
}
if (palRes == 0 && _lastPaletteRes)
palRes = _lastPaletteRes;
}
// If non-zero, set palette to this separate palette file. Otherwise,
// set palette to current screen palette.
if (palRes) {
byte *pal = _vm->_resman->openResource(palRes);
assert(_vm->_resman->fetchType(pal) == PALETTE_FILE);
pal += ResHeader::size();
// always set color 0 to black because most background screen
// palettes have a bright color 0 although it should come out
// as black in the game!
_palette[0] = 0;
_palette[1] = 0;
_palette[2] = 0;
for (uint i = 4, j = 3; i < 4 * 256; i += 4, j += 3) {
_palette[j + 0] = pal[i + 0];
_palette[j + 1] = pal[i + 1];
_palette[j + 2] = pal[i + 2];
}
setPalette(0, 256, _palette, RDPAL_INSTANT);
_vm->_resman->closeResource(palRes);
} else {
if (_thisScreen.background_layer_id) {
byte *data = _vm->_resman->openResource(_thisScreen.background_layer_id);
// Do not fetch palette match table when using PSX version,
// because it is not present.
if (!Sword2Engine::isPsx())
memcpy(_paletteMatch, _vm->fetchPaletteMatchTable(data), PALTABLESIZE);
_vm->fetchPalette(data, _palette);
setPalette(0, 256, _palette, RDPAL_INSTANT);
_vm->_resman->closeResource(_thisScreen.background_layer_id);
} else
error("setFullPalette(0) called, but no current screen available");
}
if (palRes != CONTROL_PANEL_PALETTE)
_lastPaletteRes = palRes;
}
/**
* Matches a color triplet to a palette index.
* @param r red color component
* @param g green color component
* @param b blue color component
* @return the palette index of the closest matching color in the palette
*/
// FIXME: This used to be inlined - probably a good idea - but the
// linker complained when I tried to use it in sprite.cpp.
uint8 Screen::quickMatch(uint8 r, uint8 g, uint8 b) {
return _paletteMatch[((int32)(r >> 2) << 12) + ((int32)(g >> 2) << 6) + (b >> 2)];
}
/**
* Sets the palette.
* @param startEntry the first color entry to set
* @param noEntries the number of color entries to set
* @param colorTable the new color entries
* @param fadeNow whether to perform the change immediately or delayed
*/
void Screen::setPalette(int16 startEntry, int16 noEntries, byte *colorTable, uint8 fadeNow) {
assert(noEntries > 0);
memmove(&_palette[3 * startEntry], colorTable, noEntries * 3);
if (fadeNow == RDPAL_INSTANT) {
setSystemPalette(_palette, startEntry, noEntries);
setNeedFullRedraw();
}
}
void Screen::dimPalette(bool dim) {
if (getFadeStatus() != RDFADE_NONE)
return;
if (dim != _dimPalette) {
_dimPalette = dim;
setSystemPalette(_palette, 0, 256);
setNeedFullRedraw();
}
}
/**
* Fades the palette up from black to the current palette.
* @param time the time it will take the palette to fade up
*/
int32 Screen::fadeUp(float time) {
if (getFadeStatus() != RDFADE_BLACK && getFadeStatus() != RDFADE_NONE)
return RDERR_FADEINCOMPLETE;
_fadeTotalTime = (int32)(time * 1000);
_fadeStatus = RDFADE_UP;
_fadeStartTime = getTick();
return RD_OK;
}
/**
* Fades the palette down to black from the current palette.
* @param time the time it will take the palette to fade down
*/
int32 Screen::fadeDown(float time) {
if (getFadeStatus() != RDFADE_BLACK && getFadeStatus() != RDFADE_NONE)
return RDERR_FADEINCOMPLETE;
_fadeTotalTime = (int32)(time * 1000);
_fadeStatus = RDFADE_DOWN;
_fadeStartTime = getTick();
return RD_OK;
}
/**
* Get the current fade status
* @return RDFADE_UP (fading up), RDFADE_DOWN (fading down), RDFADE_NONE
* (not faded), or RDFADE_BLACK (completely faded down)
*/
uint8 Screen::getFadeStatus() {
return _fadeStatus;
}
void Screen::waitForFade() {
while (getFadeStatus() != RDFADE_NONE && getFadeStatus() != RDFADE_BLACK && !_vm->shouldQuit()) {
updateDisplay();
_vm->_system->delayMillis(20);
}
}
void Screen::fadeServer() {
static int32 previousTime = 0;
byte fadePalette[256 * 3];
byte *newPalette = fadePalette;
int32 currentTime;
int16 fadeMultiplier;
int16 i;
// If we're not in the process of fading, do nothing.
if (getFadeStatus() != RDFADE_UP && getFadeStatus() != RDFADE_DOWN)
return;
// I don't know if this is necessary, but let's limit how often the
// palette is updated, just to be safe.
currentTime = getTick();
if (currentTime - previousTime <= 25)
return;
previousTime = currentTime;
if (getFadeStatus() == RDFADE_UP) {
if (currentTime >= _fadeStartTime + _fadeTotalTime) {
_fadeStatus = RDFADE_NONE;
newPalette = _palette;
} else {
fadeMultiplier = (int16)(((int32)(currentTime - _fadeStartTime) * 256) / _fadeTotalTime);
for (i = 0; i < 256; i++) {
newPalette[i * 3 + 0] = (_palette[i * 3 + 0] * fadeMultiplier) >> 8;
newPalette[i * 3 + 1] = (_palette[i * 3 + 1] * fadeMultiplier) >> 8;
newPalette[i * 3 + 2] = (_palette[i * 3 + 2] * fadeMultiplier) >> 8;
}
}
} else {
if (currentTime >= _fadeStartTime + _fadeTotalTime) {
_fadeStatus = RDFADE_BLACK;
memset(newPalette, 0, sizeof(fadePalette));
} else {
fadeMultiplier = (int16)(((int32)(_fadeTotalTime - (currentTime - _fadeStartTime)) * 256) / _fadeTotalTime);
for (i = 0; i < 256; i++) {
newPalette[i * 3 + 0] = (_palette[i * 3 + 0] * fadeMultiplier) >> 8;
newPalette[i * 3 + 1] = (_palette[i * 3 + 1] * fadeMultiplier) >> 8;
newPalette[i * 3 + 2] = (_palette[i * 3 + 2] * fadeMultiplier) >> 8;
}
}
}
setSystemPalette(newPalette, 0, 256);
setNeedFullRedraw();
}
void Screen::setSystemPalette(const byte *colors, uint start, uint num) {
const byte *palette;
if (_dimPalette) {
byte pal[256 * 3];
for (uint i = start * 3; i < 3 * (start + num); i++)
pal[i] = colors[i] / 2;
palette = pal;
} else
palette = colors;
_vm->_system->getPaletteManager()->setPalette(palette, start, num);
}
} // End of namespace Sword2
|