aboutsummaryrefslogtreecommitdiff
path: root/engines/sword2/header.cpp
blob: 6ab88d159dae69df1637b0d0389d50937f66749c (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
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "sword2/header.h"
#include "sword2/object.h"
#include "sword2/screen.h"
#include "sword2/sword2.h"

#include "common/memstream.h"
#include "common/endian.h"

namespace Sword2 {

void ResHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	fileType = readS.readByte();
	compType = readS.readByte();
	compSize = readS.readUint32LE();
	decompSize = readS.readUint32LE();
	readS.read(name, NAME_LEN);
}

void ResHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeByte(fileType);
	writeS.writeByte(compType);
	writeS.writeUint32LE(compSize);
	writeS.writeUint32LE(decompSize);
	writeS.write(name, NAME_LEN);
}

void AnimHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	if (Sword2Engine::isPsx()) {
		noAnimFrames = readS.readUint16LE();
		feetStartX = readS.readUint16LE();
		feetStartY = readS.readUint16LE();
		feetEndX = readS.readUint16LE();
		feetEndY = readS.readUint16LE();
		blend = readS.readUint16LE();
		runTimeComp = readS.readByte();
		feetStartDir = readS.readByte();
		feetEndDir = readS.readByte();
	} else {
		runTimeComp = readS.readByte();
		noAnimFrames = readS.readUint16LE();
		feetStartX = readS.readUint16LE();
		feetStartY = readS.readUint16LE();
		feetStartDir = readS.readByte();
		feetEndX = readS.readUint16LE();
		feetEndY = readS.readUint16LE();
		feetEndDir = readS.readByte();
		blend = readS.readUint16LE();
	}
}

void AnimHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeByte(runTimeComp);
	writeS.writeUint16LE(noAnimFrames);
	writeS.writeUint16LE(feetStartX);
	writeS.writeUint16LE(feetStartY);
	writeS.writeByte(feetStartDir);
	writeS.writeUint16LE(feetEndX);
	writeS.writeUint16LE(feetEndY);
	writeS.writeByte(feetEndDir);
	writeS.writeUint16LE(blend);
}

int CdtEntry::size() {
	if (Sword2Engine::isPsx())
		return 12;
	else
		return 9;
}

void CdtEntry::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	if (Sword2Engine::isPsx()) {
		readS.readByte(); // Skip a byte in psx version
		x = readS.readUint16LE();
		y = readS.readUint16LE();
		frameOffset = readS.readUint32LE();
		frameType = readS.readByte();
	} else {
		x = readS.readUint16LE();
		y = readS.readUint16LE();
		frameOffset = readS.readUint32LE();
		frameType = readS.readByte();
	}
}

void CdtEntry::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(x);
	writeS.writeUint16LE(y);
	writeS.writeUint32LE(frameOffset);
	writeS.writeByte(frameType);
}

void FrameHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	compSize = readS.readUint32LE();
	width = readS.readUint16LE();
	height = readS.readUint16LE();

	if (Sword2Engine::isPsx()) { // In PSX version, frames are half height
		height *= 2;
		width = (width % 2) ? width + 1 : width;
	}
}

void FrameHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint32LE(compSize);
	writeS.writeUint16LE(width);
	writeS.writeUint16LE(height);
}

void MultiScreenHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	palette = readS.readUint32LE();
	bg_parallax[0] = readS.readUint32LE();
	bg_parallax[1] = readS.readUint32LE();
	screen = readS.readUint32LE();
	fg_parallax[0] = readS.readUint32LE();
	fg_parallax[1] = readS.readUint32LE();
	layers = readS.readUint32LE();
	paletteTable = readS.readUint32LE();
	maskOffset = readS.readUint32LE();
}

void MultiScreenHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint32LE(palette);
	writeS.writeUint32LE(bg_parallax[0]);
	writeS.writeUint32LE(bg_parallax[1]);
	writeS.writeUint32LE(screen);
	writeS.writeUint32LE(fg_parallax[0]);
	writeS.writeUint32LE(fg_parallax[1]);
	writeS.writeUint32LE(layers);
	writeS.writeUint32LE(paletteTable);
	writeS.writeUint32LE(maskOffset);
}

void ScreenHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	width = readS.readUint16LE();
	height = readS.readUint16LE();
	noLayers = readS.readUint16LE();
}

void ScreenHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(width);
	writeS.writeUint16LE(height);
	writeS.writeUint16LE(noLayers);
}

void LayerHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	x = readS.readUint16LE();
	y = readS.readUint16LE();
	width = readS.readUint16LE();
	height = readS.readUint16LE();
	maskSize = readS.readUint32LE();
	offset = readS.readUint32LE();
}

void LayerHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(x);
	writeS.writeUint16LE(y);
	writeS.writeUint16LE(width);
	writeS.writeUint16LE(height);
	writeS.writeUint32LE(maskSize);
	writeS.writeUint32LE(offset);
}

void TextHeader::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	noOfLines = readS.readUint32LE();
}

void TextHeader::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint32LE(noOfLines);
}

void PSXScreensEntry::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	bgPlxXres = readS.readUint16LE();
	bgPlxYres = readS.readUint16LE();
	bgPlxOffset = readS.readUint32LE();
	bgPlxSize = readS.readUint32LE();
	bgXres = readS.readUint16LE();
	bgYres = readS.readUint16LE();
	bgOffset = readS.readUint32LE();
	bgSize = readS.readUint32LE();
	fgPlxXres = readS.readUint16LE();
	fgPlxYres = readS.readUint16LE();
	fgPlxOffset = readS.readUint32LE();
	fgPlxSize = readS.readUint32LE();
}

void PSXScreensEntry::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(bgPlxXres);
	writeS.writeUint16LE(bgPlxYres);
	writeS.writeUint32LE(bgPlxOffset);
	writeS.writeUint32LE(bgPlxSize);
	writeS.writeUint16LE(bgXres);
	writeS.writeUint16LE(bgYres);
	writeS.writeUint32LE(bgOffset);
	writeS.writeUint32LE(bgSize);
	writeS.writeUint16LE(fgPlxXres);
	writeS.writeUint16LE(fgPlxYres);
	writeS.writeUint32LE(fgPlxOffset);
	writeS.writeUint32LE(fgPlxSize);
}

void PSXFontEntry::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	offset = readS.readUint16LE() / 2;
	skipLines = readS.readUint16LE();
	charWidth = readS.readUint16LE() / 2;
	charHeight = readS.readUint16LE();
}

void PSXFontEntry::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(offset);
	writeS.writeUint16LE(skipLines);
	writeS.writeUint16LE(charWidth);
	writeS.writeUint16LE(charHeight);
}

void Parallax::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	w = readS.readUint16LE();
	h = readS.readUint16LE();
}

void Parallax::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint16LE(w);
	writeS.writeUint16LE(h);
}

void ObjectMouse::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	x1 = readS.readSint32LE();
	y1 = readS.readSint32LE();
	x2 = readS.readSint32LE();
	y2 = readS.readSint32LE();
	priority = readS.readSint32LE();
	pointer = readS.readSint32LE();
}

void ObjectMouse::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeSint32LE(x1);
	writeS.writeSint32LE(y1);
	writeS.writeSint32LE(x2);
	writeS.writeSint32LE(y2);
	writeS.writeSint32LE(priority);
	writeS.writeSint32LE(pointer);
}

void ObjectWalkdata::read(byte *addr) {
	Common::MemoryReadStream readS(addr, size());

	nWalkFrames = readS.readUint32LE();
	usingStandingTurnFrames = readS.readUint32LE();
	usingWalkingTurnFrames = readS.readUint32LE();
	usingSlowInFrames = readS.readUint32LE();
	usingSlowOutFrames = readS.readUint32LE();

	int i;

	for (i = 0; i < ARRAYSIZE(nSlowInFrames); i++)
		nSlowInFrames[i] = readS.readUint32LE();

	for (i = 0; i < ARRAYSIZE(leadingLeg); i++)
		leadingLeg[i] = readS.readUint32LE();

	for (i = 0; i < ARRAYSIZE(dx); i++)
		dx[i] = readS.readUint32LE();

	for (i = 0; i < ARRAYSIZE(dy); i++)
		dy[i] = readS.readUint32LE();
}

void ObjectWalkdata::write(byte *addr) {
	Common::MemoryWriteStream writeS(addr, size());

	writeS.writeUint32LE(nWalkFrames);
	writeS.writeUint32LE(usingStandingTurnFrames);
	writeS.writeUint32LE(usingWalkingTurnFrames);
	writeS.writeUint32LE(usingSlowInFrames);
	writeS.writeUint32LE(usingSlowOutFrames);

	int i;

	for (i = 0; i < ARRAYSIZE(nSlowInFrames); i++)
		writeS.writeUint32LE(nSlowInFrames[i]);

	for (i = 0; i < ARRAYSIZE(leadingLeg); i++)
		writeS.writeUint32LE(leadingLeg[i]);

	for (i = 0; i < ARRAYSIZE(dx); i++)
		writeS.writeUint32LE(dx[i]);

	for (i = 0; i < ARRAYSIZE(dy); i++)
		writeS.writeUint32LE(dy[i]);
}

} // End of namespace Sword2