aboutsummaryrefslogtreecommitdiff
path: root/saga/palanim.cpp
blob: 1f2747b5d6500466aae122d70d6ecd2ade3f1bd7 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// Palette animation module
#include "saga/saga.h"
#include "saga/gfx.h"

#include "saga/events_mod.h"
#include "saga/game_mod.h"

#include "saga/palanim_mod.h"
#include "saga/palanim.h"

namespace Saga {

static PALANIM_DATA PAnimData;

int PALANIM_Load(const byte *resdata, size_t resdata_len) {
	void *test_p;

	uint16 i;

	if (PAnimData.loaded) {
		PALANIM_Free();
	}

	if (resdata == NULL) {
		return R_FAILURE;
	}

	MemoryReadStream readS(resdata, resdata_len);

	if (GAME_GetGameType() == R_GAMETYPE_IHNM) {
		return R_SUCCESS;
	}

	PAnimData.entry_count = readS.readUint16LE();

	debug(0, "PALANIM_Load(): Loading %d PALANIM entries.", PAnimData.entry_count);

	test_p = calloc(PAnimData.entry_count, sizeof(PALANIM_ENTRY));
	if (test_p == NULL) {
		warning("PALANIM_Load(): Allocation failure");
		return R_MEM;
	}

	PAnimData.entries = (PALANIM_ENTRY *)test_p;

	for (i = 0; i < PAnimData.entry_count; i++) {
		int color_count;
		int pal_count;
		int p, c;

		color_count = readS.readUint16LE();
		pal_count = readS.readUint16LE();

		PAnimData.entries[i].pal_count = pal_count;
		PAnimData.entries[i].color_count = color_count;

		debug(2, "PALANIM_Load(): Entry %d: Loading %d palette indices.\n", i, pal_count);

		test_p = calloc(1, sizeof(char) * pal_count);
		if (test_p == NULL) {
			warning("PALANIM_Load(): Allocation failure");
			return R_MEM;
		}

		PAnimData.entries[i].pal_index = (byte *)test_p;

		debug(2, "PALANIM_Load(): Entry %d: Loading %d SAGA_COLOR structures.", i, color_count);

		test_p = calloc(1, sizeof(R_COLOR) * color_count);
		if (test_p == NULL) {
			warning("PALANIM_Load(): Allocation failure");
			return R_MEM;
		}

		PAnimData.entries[i].colors = (R_COLOR *)test_p;

		for (p = 0; p < pal_count; p++) {
			PAnimData.entries[i].pal_index[p] = readS.readByte();
		}

		for (c = 0; c < color_count; c++) {
			PAnimData.entries[i].colors[c].red = readS.readByte();
			PAnimData.entries[i].colors[c].green = readS.readByte();
			PAnimData.entries[i].colors[c].blue = readS.readByte();
		}
	}

	PAnimData.loaded = 1;
	return R_SUCCESS;
}

int PALANIM_CycleStart() {
	R_EVENT event;

	if (!PAnimData.loaded) {
		return R_FAILURE;
	}

	event.type = R_ONESHOT_EVENT;
	event.code = R_PALANIM_EVENT;
	event.op = EVENT_CYCLESTEP;
	event.time = PALANIM_CYCLETIME;

	EVENT_Queue(&event);

	return R_SUCCESS;
}

int PALANIM_CycleStep(int vectortime) {
	R_SURFACE *back_buf;

	static PALENTRY pal[256];
	uint16 pal_index;
	uint16 col_index;

	uint16 i, j;
	uint16 cycle;
	uint16 cycle_limit;

	R_EVENT event;

	if (!PAnimData.loaded) {
		return R_FAILURE;
	}

	_vm->_gfx->getCurrentPal(pal);
	back_buf = _vm->_gfx->getBackBuffer();

	for (i = 0; i < PAnimData.entry_count; i++) {
		cycle = PAnimData.entries[i].cycle;
		cycle_limit = PAnimData.entries[i].color_count;
		for (j = 0; j < PAnimData.entries[i].pal_count; j++) {
			pal_index = (unsigned char)PAnimData.entries[i].pal_index[j];
			col_index = (cycle + j) % cycle_limit;
			pal[pal_index].red = (byte) PAnimData.entries[i].colors[col_index].red;
			pal[pal_index].green = (byte) PAnimData.entries[i].colors[col_index].green;
			pal[pal_index].blue = (byte) PAnimData.entries[i].colors[col_index].blue;
		}

		PAnimData.entries[i].cycle++;

		if (PAnimData.entries[i].cycle == cycle_limit) {
			PAnimData.entries[i].cycle = 0;
		}
	}

	_vm->_gfx->setPalette(back_buf, pal);

	event.type = R_ONESHOT_EVENT;
	event.code = R_PALANIM_EVENT;
	event.op = EVENT_CYCLESTEP;
	event.time = vectortime + PALANIM_CYCLETIME;

	EVENT_Queue(&event);

	return R_SUCCESS;
}

int PALANIM_Free() {
	uint16 i;

	if (!PAnimData.loaded) {
		return R_FAILURE;
	}

	for (i = 0; i < PAnimData.entry_count; i++) {
		debug(2, "PALANIM_Free(): Entry %d: Freeing colors.", i);
		free(PAnimData.entries[i].colors);
		debug(2, "PALANIM_Free(): Entry %d: Freeing indices.", i);
		free(PAnimData.entries[i].pal_index);
	}

	debug(0, "PALANIM_Free(): Freeing entries.");

	free(PAnimData.entries);

	PAnimData.loaded = 0;

	return R_SUCCESS;
}

} // End of namespace Saga