aboutsummaryrefslogtreecommitdiff
path: root/engines/neverhood/palette.cpp
blob: b45f8eeb7898ed5dae752987b91fdec8c2fa1d21 (plain)
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
/* 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.
 *
 */

#include "neverhood/palette.h"
#include "neverhood/resource.h"

namespace Neverhood {

Palette::Palette(NeverhoodEngine *vm) : Entity(vm, 0) {
	_status = 0;
	_palette = new byte[1024];
	memset(_palette, 0, 1024);
	SetUpdateHandler(&Palette::update);
}

Palette::Palette(NeverhoodEngine *vm, byte *palette) : Entity(vm, 0) {
	_status = 0;
	_palette = new byte[1024];
	memcpy(_palette, palette, 1024);
	SetUpdateHandler(&Palette::update);
}

Palette::Palette(NeverhoodEngine *vm, const char *filename) : Entity(vm, 0) {
	PaletteResource paletteResource(_vm);
	_status = 0;
	_palette = new byte[1024];
	// TODO: paletteResource.load(calcHash(filename));
	// paletteResource.copyPalette(_palette);
	SetUpdateHandler(&Palette::update);
}

Palette::Palette(NeverhoodEngine *vm, uint32 fileHash) : Entity(vm, 0) {
	PaletteResource paletteResource(_vm);
	_status = 0;
	_palette = new byte[1024];
	paletteResource.load(fileHash);
	paletteResource.copyPalette(_palette);
	SetUpdateHandler(&Palette::update);
}

Palette::~Palette() {
	// TODO: _vm->_screen->unsetPaletteData(_palette);
	delete[] _palette;
}

void Palette::usePalette() {
	// TODO: _vm->_screen->setPaletteData(_palette);
}

void Palette::addPalette(const char *filename, int toIndex, int count, int fromIndex) {
	// TODO: addPalette(calcHash(filename), toIndex, count, fromIndex);
}

void Palette::addPalette(uint32 fileHash, int toIndex, int count, int fromIndex) {
	PaletteResource paletteResource(_vm);
	if (toIndex + count > 256)
		count = 256 - toIndex;
	paletteResource.load(fileHash);
	memcpy(_palette + toIndex * 4, paletteResource.palette() + fromIndex * 4, count * 4);		
	// TODO: _vm->_screen->testPalette(_palette);
}

void Palette::startFadeToBlack(int counter) {
	if (counter == 0)
		counter = 1;
	_fadeToR = 0;
	_fadeToG = 0;
	_fadeToB = 0;
	_palCounter = counter;
	_fadeStep = 255 / counter;
	_status = 1;			
}

void Palette::startFadeToWhite(int counter) {
	if (counter == 0)
		counter = 1;
	_fadeToR = 255;
	_fadeToG = 255;
	_fadeToB = 255;
	_palCounter = counter;
	_fadeStep = 255 / counter;
	_status = 1;			
}

void Palette::update() {
	if (_status == 1) {
		memset(_palette, 0, 1024);
		_status = 0;
	} else {
		for (int i = 0; i < 256; i++) {
			fadeColor(_palette + i * 4, _fadeToR, _fadeToG, _fadeToB);
		}
		// TODO: _vm->_screen->testPalette(_palette);
		_palCounter--;
	}
}

void Palette::fadeColor(byte *rgb, byte toR, byte toG, byte toB) {
	#define FADE(color, toColor) color += _fadeStep < toColor - color ? _fadeStep : (-_fadeStep <= toColor - color ? toColor - color : -_fadeStep)
	FADE(rgb[0], toR);
	FADE(rgb[1], toG);
	FADE(rgb[2], toB);
	#undef FADE
}

} // End of namespace Neverhood