aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/palanim.cpp
blob: 50686beb6fe0b1d13a43ffd024713685934ddac2 (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
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 Ivan Dubrov
 * Copyright (C) 2004-2006 The ScummVM project
 *
 * 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 "gob/gob.h"
#include "gob/palanim.h"
#include "gob/global.h"
#include "gob/util.h"
#include "gob/video.h"

namespace Gob {

PalAnim::PalAnim(GobEngine *vm) : _vm(vm) {
	_fadeValue = 1;
	for (int i = 0; i < 256; i++) {
		_toFadeRed[i] = 0;
		_toFadeGreen[i] = 0;
		_toFadeBlue[i] = 0;
	}

	_palArray[0] = _vm->_global->_redPalette;
	_palArray[1] = _vm->_global->_greenPalette;
	_palArray[2] = _vm->_global->_bluePalette;
	_fadeArray[0] = _toFadeRed;
	_fadeArray[1] = _toFadeGreen;
	_fadeArray[2] = _toFadeBlue;
}

char PalAnim::fadeColor(int16 from, int16 to) {
	if ((from - _fadeValue) > to)
		return from - _fadeValue;
	else if ((from + _fadeValue) < to)
		return from + _fadeValue;
	else return to;
}

bool PalAnim::fadeStepColor(int color) {
	bool stop = true;
	char colors[3];

	for (int i = 0; i < 16; i++) {
		colors[0] = _palArray[0][i];
		colors[1] = _palArray[1][i];
		colors[2] = _palArray[2][i];
		colors[color] = fadeColor(_palArray[color][i], _fadeArray[color][i]);

		_vm->_video->setPalElem(i, colors[0], colors[1], colors[2],
				-1, _vm->_global->_videoMode);

		if (_palArray[color][i] != _fadeArray[color][i])
			stop = false;
	}

	return stop;
}

bool PalAnim::fadeStep(int16 oper) {
	bool stop = true;
	byte newRed;
	byte newGreen;
	byte newBlue;

	if (_vm->_global->_colorCount != 256)
		error("PalAnim::fadeStep(): Only 256 color mode is supported!");

	if (oper == 0) {
		if (_vm->_global->_setAllPalette) {
			if (_vm->_global->_inVM != 0)
				error("PalAnim::fadeStep(): _vm->_global->_inVM != 0 not supported.");

			for (int i = 0; i < 256; i++) {
				newRed = fadeColor(_vm->_global->_redPalette[i], _toFadeRed[i]);
				newGreen = fadeColor(_vm->_global->_greenPalette[i], _toFadeGreen[i]);
				newBlue = fadeColor(_vm->_global->_bluePalette[i], _toFadeBlue[i]);

				if ((_vm->_global->_redPalette[i] != newRed) ||
				    (_vm->_global->_greenPalette[i] != newGreen) ||
				    (_vm->_global->_bluePalette[i] != newBlue)) {

					_vm->_video->setPalElem(i, newRed, newGreen, newBlue, 0, 0x13);

					_vm->_global->_redPalette[i] = newRed;
					_vm->_global->_greenPalette[i] = newGreen;
					_vm->_global->_bluePalette[i] = newBlue;
					stop = false;
				}
			}
		} else {
			for (int i = 0; i < 16; i++) {

				_vm->_video->setPalElem(i,
						fadeColor(_vm->_global->_redPalette[i], _toFadeRed[i]),
						fadeColor(_vm->_global->_greenPalette[i], _toFadeGreen[i]),
						fadeColor(_vm->_global->_bluePalette[i], _toFadeBlue[i]),
						-1, _vm->_global->_videoMode);

				if ((_vm->_global->_redPalette[i] != _toFadeRed[i]) ||
				    (_vm->_global->_greenPalette[i] != _toFadeGreen[i]) ||
				    (_vm->_global->_bluePalette[i] != _toFadeBlue[i]))
					stop = false;
			}
		}
	} else if ((oper > 0) && (oper < 4))
		stop = fadeStepColor(oper - 1);

	return stop;
}

void PalAnim::fade(Video::PalDesc *palDesc, int16 fadeV, int16 allColors) {
	bool stop;
	int16 i;

	if (_vm->_quitRequested)
		return;

	_fadeValue = (fadeV < 0) ? -fadeV : 2;

	if (_vm->_global->_colorCount < 256) {
		if (palDesc)
			_vm->_video->setFullPalette(palDesc);
		return;
	}

	if (!_vm->_global->_setAllPalette) {
		if (!palDesc) {
			for (i = 0; i < 16; i++) {
				_toFadeRed[i] = 0;
				_toFadeGreen[i] = 0;
				_toFadeBlue[i] = 0;
			}
		} else {
			for (i = 0; i < 16; i++) {
				_toFadeRed[i] = palDesc->vgaPal[i].red;
				_toFadeGreen[i] = palDesc->vgaPal[i].green;
				_toFadeBlue[i] = palDesc->vgaPal[i].blue;
			}
		}
	} else {
		if (_vm->_global->_inVM != 0)
			error("PalAnim::fade(): _vm->_global->_inVM != 0 is not supported");

		if (!palDesc) {
			for (i = 0; i < 256; i++) {
				_toFadeRed[i] = 0;
				_toFadeGreen[i] = 0;
				_toFadeBlue[i] = 0;
			}
		} else {
			for (i = 0; i < 256; i++) {
				_toFadeRed[i] = palDesc->vgaPal[i].red;
				_toFadeGreen[i] = palDesc->vgaPal[i].green;
				_toFadeBlue[i] = palDesc->vgaPal[i].blue;
			}
		}
	}

	if (allColors == 0) {
		do {
			stop = fadeStep(0);
			_vm->_video->waitRetrace();

			if (fadeV > 0)
				_vm->_util->delay(fadeV);
		} while (!stop);

		if (palDesc)
			_vm->_video->setFullPalette(palDesc);
		else
			_vm->_util->clearPalette();
	}

	if (allColors == 1) {
		do {
			_vm->_video->waitRetrace();
			stop = fadeStep(1);
		} while (!stop);

		do {
			_vm->_video->waitRetrace();
			stop = fadeStep(2);
		} while (!stop);

		do {
			_vm->_video->waitRetrace();
			stop = fadeStep(3);
		} while (!stop);

		if (palDesc)
			_vm->_video->setFullPalette(palDesc);
		else
			_vm->_util->clearPalette();
	}
}

} // End of namespace Gob