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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/* 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.
*
* 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.
*
*/
#ifndef SCI_GRAPHICS_PALETTE32_H
#define SCI_GRAPHICS_PALETTE32_H
namespace Sci {
#pragma mark HunkPalette
/**
* HunkPalette represents a raw palette resource read from disk. The data
* structure of a HunkPalette allows palettes to be smaller than 256 colors. It
* also allows multiple palettes to be stored in one HunkPalette, though in
* SCI32 games there seems to only ever be one palette per HunkPalette.
*/
class HunkPalette {
public:
HunkPalette(const SciSpan<const byte> &rawPalette);
/**
* Gets the version of the palette. Used to avoid resubmitting a HunkPalette
* which has already been submitted for the next frame.
*/
uint32 getVersion() const { return _version; }
/**
* Sets the version of the palette.
*/
void setVersion(const uint32 version) const;
/**
* Converts the hunk palette to a standard Palette.
*/
const Palette toPalette() const;
private:
enum {
/**
* The offset into the HunkPalette header of the number of palettes in
* the HunkPalette.
*/
kNumPaletteEntriesOffset = 10,
/**
* The size of the HunkPalette header.
*/
kHunkPaletteHeaderSize = 13,
/**
* The size of a palette entry header.
*/
kEntryHeaderSize = 22
};
enum {
/**
* The offset of the start color within the palette entry header.
*/
kEntryStartColorOffset = 10,
/**
* The offset of the color count within the palette entry header.
*/
kEntryNumColorsOffset = 14,
/**
* The offset of the shared used palette index flag within the palette
* entry header.
*/
kEntryUsedOffset = 16,
/**
* The offset of the flag within the palette entry header that says
* whether or not the corresponding palette data includes used flags for
* each palette index individually.
*/
kEntrySharedUsedOffset = 17,
/**
* The offset of the hunk palette version within the palette entry
* header.
*/
kEntryVersionOffset = 18
};
/**
* The header for a palette inside the HunkPalette.
*/
struct EntryHeader {
/**
* The start color.
*/
uint8 startColor;
/**
* The number of palette colors in this entry.
*/
uint16 numColors;
/**
* The default `used` flag.
*/
bool used;
/**
* Whether or not all palette entries share the same `used` value in
* `defaultFlag`.
*/
bool sharedUsed;
/**
* The palette version.
*/
uint32 version;
};
/**
* The version number from the last time this palette was submitted to
* GfxPalette32.
*/
mutable uint32 _version;
/**
* The number of palettes stored in the hunk palette. In SCI32 games this is
* always 1.
*/
uint8 _numPalettes;
/**
* The raw palette data for this hunk palette.
*/
SciSpan<const byte> _data;
/**
* Returns a struct that describes the palette held by this HunkPalette. The
* entry header is reconstructed on every call from the raw palette data.
*/
const EntryHeader getEntryHeader() const;
/**
* Returns a pointer to the palette data within the hunk palette.
*/
SciSpan<const byte> getPalPointer() const {
return _data.subspan(kHunkPaletteHeaderSize + (2 * _numPalettes));
}
};
#pragma mark -
#pragma mark PalCycler
enum PalCyclerDirection {
kPalCycleBackward = 0,
kPalCycleForward = 1
};
/**
* PalCycler represents a range of palette entries that are rotated on a timer.
*/
struct PalCycler {
/**
* The color index of this palette cycler. This value is used as the unique
* key for this PalCycler object.
*/
uint8 fromColor;
/**
* The number of palette slots which are to be cycled by this cycler.
*/
uint16 numColorsToCycle;
/**
* The current position of the first palette entry.
*/
uint8 currentCycle;
/**
* The direction of the cycler.
*/
PalCyclerDirection direction;
/**
* The last tick the cycler cycled.
*/
uint32 lastUpdateTick;
/**
* The amount of time in ticks each cycle should take to complete. In other
* words, the higher the delay, the slower the cycle animation. If delay is
* 0, the cycler does not automatically cycle and needs to be cycled
* manually by calling `doCycle`.
*/
int16 delay;
/**
* The number of times this cycler has been paused.
*/
uint16 numTimesPaused;
};
#pragma mark -
#pragma mark GfxPalette32
class GfxPalette32 {
public:
GfxPalette32(ResourceManager *resMan);
~GfxPalette32();
void saveLoadWithSerializer(Common::Serializer &s);
/**
* Gets the palette that will be use for the next frame.
*/
inline const Palette &getNextPalette() const { return _nextPalette; };
/**
* Gets the palette that is used for the current frame.
*/
inline const Palette &getCurrentPalette() const { return _currentPalette; };
/**
* Loads a palette into GfxPalette32 with the given resource ID.
*/
bool loadPalette(const GuiResourceId resourceId);
/**
* Finds the nearest color in the current palette matching the given RGB
* value.
*/
int16 matchColor(const uint8 r, const uint8 g, const uint8 b);
/**
* Submits a palette to display. Entries marked as "used" in the submitted
* palette are merged into `_sourcePalette`.
*/
void submit(const Palette &palette);
void submit(const HunkPalette &palette);
/**
* Applies all fades, cycles, remaps, and varies for the current frame to
* `nextPalette`.
*/
bool updateForFrame();
/**
* Copies all palette entries from `sourcePalette` to `nextPalette` and
* applies remaps. Unlike `updateForFrame`, this call does not apply fades,
* cycles, or varies.
*/
void updateFFrame();
/**
* Copies all entries from `nextPalette` to `currentPalette` and updates the
* backend's raw palette.
*/
void updateHardware();
private:
ResourceManager *_resMan;
/**
* The palette revision version. Increments once per game loop that changes
* the source palette.
*/
uint32 _version;
/**
* Whether or not the hardware palette needs updating.
*/
bool _needsUpdate;
/**
* The currently displayed palette.
*/
Palette _currentPalette;
/**
* The unmodified source palette loaded by kPalette. Additional palette
* entries may be mixed into the source palette by CelObj objects, which
* contain their own palettes.
*/
Palette _sourcePalette;
/**
* The palette to be used when the hardware is next updated.
* On update, `_nextPalette` is transferred to `_currentPalette`.
*/
Palette _nextPalette;
/**
* Creates and returns a new Palette object with data from the given
* resource ID.
*/
Palette getPaletteFromResource(const GuiResourceId paletteId) const;
/**
* Merges used colors in the `from` palette into the `to` palette.
*/
void mergePalette(Palette &to, const Palette &from);
/**
* Applies all varies, cycles, and fades to `_nextPalette`.
*/
void applyAll();
#pragma mark -
#pragma mark Varying
public:
/**
* Blends the `target` palette into the current palette over `time` ticks.
*
* @param target The target palette.
* @param percent The amount that the target palette should be blended into
* the source palette by the end of the vary.
* @param ticks The number of ticks that it should take for the blend to be
* completed.
* @param fromColor The first palette entry that should be blended.
* @param toColor The last palette entry that should be blended.
*/
void setVary(const Palette &target, const int16 percent, const int32 ticks, const int16 fromColor, const int16 toColor);
/**
* Gets the current vary blend amount.
*/
inline int16 getVaryPercent() const { return ABS(_varyPercent); }
/**
* Changes the percentage of the current vary to `percent`, to be completed
* over `time` ticks, if there is a currently active vary target palette.
*/
void setVaryPercent(const int16 percent, const int32 time);
/**
* Changes the amount of time, in ticks, an in-progress palette vary should
* take to finish.
*/
void setVaryTime(const int32 ticks);
/**
* Changes the vary percent and time to perform the vary.
*/
void setVaryTime(const int16 percent, const int32 ticks);
/**
* Removes the active palette vary.
*/
void varyOff();
/**
* Pauses any active palette vary.
*/
void varyPause();
/**
* Unpauses any paused palette vary.
*/
void varyOn();
/**
* Sets the target palette for the blend.
*/
void setTarget(const Palette &palette);
/**
* Sets the start palette for the blend.
*/
void setStart(const Palette &palette);
/**
* Merges a new start palette into the existing start palette.
*/
void mergeStart(const Palette &palette);
/**
* Merges a new target palette into the existing target palette.
*/
void mergeTarget(const Palette &palette);
/**
* Applies any active palette vary to `_nextPalette`.
*/
void applyVary();
void kernelPalVarySet(const GuiResourceId paletteId, const int16 percent, const int32 ticks, const int16 fromColor, const int16 toColor);
void kernelPalVaryMergeTarget(const GuiResourceId paletteId);
void kernelPalVarySetTarget(const GuiResourceId paletteId);
void kernelPalVarySetStart(const GuiResourceId paletteId);
void kernelPalVaryMergeStart(const GuiResourceId paletteId);
void kernelPalVaryPause(const bool pause);
private:
/**
* An optional palette used to provide source colors for a palette vary
* operation. If this palette is not specified, `_sourcePalette` is used
* instead.
*/
Palette *_varyStartPalette;
/**
* An optional palette used to provide target colors for a palette vary
* operation.
*/
Palette *_varyTargetPalette;
/**
* The minimum palette index that has been varied from the source palette.
*/
uint8 _varyFromColor;
/**
* The maximum palette index that has been varied from the source palette.
*/
uint8 _varyToColor;
/**
* The tick at the last time the palette vary was updated.
*/
uint32 _varyLastTick;
/**
* The amount of time that should elapse, in ticks, between each cycle of a
* palette vary animation.
*/
int32 _varyTime;
/**
* The direction of change: -1, 0, or 1.
*/
int16 _varyDirection;
/**
* The amount, in percent, that the vary color is currently blended into the
* source color.
*/
int16 _varyPercent;
/**
* The target amount that a vary color will be blended into the source
* color.
*/
int16 _varyTargetPercent;
/**
* The number of times palette varying has been paused.
*/
uint16 _varyNumTimesPaused;
#pragma mark -
#pragma mark Cycling
public:
inline const bool *getCycleMap() const { return _cycleMap; }
/**
* Cycle palette entries between `fromColor` and `toColor`, inclusive.
* Palette cyclers may not overlap. `fromColor` is used in other methods as
* the key for looking up a cycler.
*
* @param fromColor The first color in the cycle.
* @param toColor The last color in the cycle.
* @param delay The number of ticks that should elapse between cycles.
* @param direction A negative `direction` will cycle backwards instead of
* forwards. The numeric value of this argument is ignored;
* only its sign is used to determine direction.
*/
void setCycle(const uint8 fromColor, const uint8 toColor, const int16 direction, const int16 delay);
/**
* Performs a round of palette cycling.
*
* @param fromColor The color key for the cycler.
* @param speed The number of entries that should be cycled this round.
*/
void doCycle(const uint8 fromColor, const int16 speed);
/**
* Unpauses the cycler starting at `fromColor`.
*/
void cycleOn(const uint8 fromColor);
/**
* Pauses the cycler starting at `fromColor`.
*/
void cyclePause(const uint8 fromColor);
/**
* Unpauses all cyclers.
*/
void cycleAllOn();
/**
* Pauses all cyclers.
*/
void cycleAllPause();
/**
* Removes the cycler starting at `fromColor`.
*/
void cycleOff(const uint8 fromColor);
/**
* Removes all cyclers.
*/
void cycleAllOff();
private:
enum {
kNumCyclers = 10
};
PalCycler *_cyclers[kNumCyclers];
/**
* Updates the `currentCycle` of the given `cycler` by `speed` entries.
*/
void updateCycler(PalCycler &cycler, const int16 speed);
/**
* The cycle map is used to detect overlapping cyclers, and to avoid
* remapping to palette entries that are being cycled.
*
* According to SCI engine code, when two cyclers overlap, a fatal error has
* occurred and the engine will display an error and then exit.
*
* The color remapping system avoids attempts to remap to palette entries
* that are cycling because they won't be the expected color once the cycler
* updates the palette entries.
*/
bool _cycleMap[256];
/**
* Marks `numColorsToClear` colors starting at `fromColor` in the cycle
* map as inactive.
*/
void clearCycleMap(const uint16 fromColor, const uint16 numColorsToClear);
/**
* Marks `numColorsToClear` colors starting at `fromColor` in the cycle
* map as active.
*/
void setCycleMap(const uint16 fromColor, const uint16 numColorsToClear);
/**
* Gets the cycler object that starts at the given `fromColor`, or NULL if
* there is no cycler for that color.
*/
PalCycler *getCycler(const uint16 fromColor);
/**
* Advances all cyclers by one step, regardless of whether or not it is time
* to perform another cycle.
*/
void applyAllCycles();
/**
* Advances, by one step, only the cyclers whose time has come to cycle.
*/
void applyCycles();
#pragma mark -
#pragma mark Fading
public:
/**
* Sets the intensity level for a range of palette entries. An intensity of
* zero indicates total darkness. Intensity may also be set above 100
* percent to increase the intensity of a palette entry.
*/
void setFade(const uint16 percent, const uint8 fromColor, const uint16 toColor);
/**
* Resets the intensity of all palette entries to 100%.
*/
void fadeOff();
/**
* Applies intensity values to the palette entries in `_nextPalette`.
*/
void applyFade();
private:
/**
* The intensity levels of each palette entry, in percent. Defaults to 100.
*/
uint16 _fadeTable[256];
#pragma mark -
#pragma mark Gamma correction
public:
enum {
/**
* The number of available gamma corrections.
*/
numGammaTables = 6
};
/**
* Sets the gamma correction level, from 0 (off) to `numGammaTables`,
* inclusive.
*/
void setGamma(const int16 level) {
_gammaLevel = CLIP<int16>(level, 0, numGammaTables) - 1;
_gammaChanged = true;
}
private:
/**
* The current gamma correction level. -1 means no correction.
*/
int8 _gammaLevel;
/**
* Whether the gamma correction has changed since the last call to update
* the hardware palette.
*/
bool _gammaChanged;
};
} // End of namespace Sci
#endif
|