1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1994-1998 Revolution Software Ltd.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 #ifndef	SWORD2_HEADER_H
26 #define	SWORD2_HEADER_H
27 
28 #include "common/endian.h"
29 
30 namespace Sword2 {
31 
32 //----------------------------------------------------------
33 // SYSTEM FILE & FRAME HEADERS
34 //----------------------------------------------------------
35 
36 //----------------------------------------------------------
37 // ALL FILES
38 //----------------------------------------------------------
39 
40 // Standard File Header
41 
42 #define	NAME_LEN 34
43 
44 struct ResHeader {
45 	uint8 fileType;			// Byte to define file type (see below)
46 	uint8 compType;			// Type of file compression used ie.
47 					// on whole file (see below)
48 	uint32 compSize;		// Length of compressed file (ie.
49 					// length on disk)
50 	uint32 decompSize;		// Length of decompressed file held in
51 					// memory (NB. frames still held
52 					// compressed)
53 	byte name[NAME_LEN];		// Name of object
54 
sizeResHeader55 	static int size() {
56 		return 44;
57 	}
58 
59 	void read(byte *addr);
60 	void write(byte *addr);
61 };
62 
63 // fileType
64 
65 enum {
66 	// 0 something's wrong!
67 	ANIMATION_FILE		= 1,	// All normal animations & sprites
68 					// including mega-sets & font files
69 					// which are the same format (but all
70 					// frames always uncompressed)
71 	SCREEN_FILE		= 2,	// Each contains background, palette,
72 					// layer sprites, parallax layers &
73 					// shading mask
74 	GAME_OBJECT		= 3,	// Each contains object hub +
75 					// structures + script data
76 	WALK_GRID_FILE		= 4,	// Walk-grid data
77 	GLOBAL_VAR_FILE		= 5,	// All the global script variables in
78 					// one file; "there can be only one"
79 	PARALLAX_FILE_null	= 6,	// NOT USED
80 	RUN_LIST		= 7,	// Each contains a list of object
81 					// resource id's
82 	TEXT_FILE		= 8,	// Each contains all the lines of text
83 					// for a location or a character's
84 					// conversation script
85 	SCREEN_MANAGER		= 9,	// One for each location; this contains
86 					// special startup scripts
87 	MOUSE_FILE		= 10,	// Mouse pointers and luggage icons
88 					// (sprites in General / Mouse pointers
89 					// & Luggage icons)
90 	WAV_FILE		= 11,	// WAV file
91 	ICON_FILE		= 12,	// Menu icon (sprites in General / Menu
92 					// icons)
93 	PALETTE_FILE		= 13	// separate palette file (see also
94 					// _paletteHeader)
95 };
96 
97 // compType
98 
99 enum {
100 	NO_COMPRESSION		= 0,
101 	FILE_COMPRESSION	= 1	// standard whole-file compression
102 					// (not yet devised!)
103 };
104 
105 //----------------------------------------------------------
106 // (1) ANIMATION FILES
107 //----------------------------------------------------------
108 
109 // an animation file consists of:
110 //
111 // standard file header
112 // animation header
113 // a string of CDT entries (one per frame of the anim)
114 // a 16-byte color table ONLY if (runTimeComp==RLE16)
115 // a string of groups of (frame header + frame data)
116 
117 // Animation Header
118 
119 struct AnimHeader {
120 	uint8 runTimeComp;	// Type of runtime compression used for the
121 				// frame data (see below)
122 	uint16 noAnimFrames;	// Number of frames in the anim (ie. no. of
123 				// CDT entries)
124 	uint16 feetStartX;	// Start coords for mega to walk to, before
125 	uint16 feetStartY;	// running anim
126 	uint8 feetStartDir;	// Direction to start in before running anim
127 	uint16 feetEndX;	// End coords for mega to stand at after
128 	uint16 feetEndY;	// running anim (vital if anim starts from an
129 				// off-screen position, or ends in a different
130 				// place from the start)
131 	uint8 feetEndDir;	// Direction to start in after running anim
132 	uint16 blend;
133 
sizeAnimHeader134 	static int size() {
135 		return 15;
136 	}
137 
138 	void read(byte *addr);
139 	void write(byte *addr);
140 
141 };
142 
143 // runtimeComp - compression used on each frame of the anim
144 
145 enum {
146 	NONE	= 0,		// No frame compression
147 	RLE256	= 1,		// James's RLE for 256-color sprites
148 	RLE16	= 2		// James's RLE for 16- or 17-color sprites
149 				// (raw blocks have max 16 colors for 2 pixels
150 				// per byte, so '0's are encoded only as FLAT
151 				// for 17-color sprites eg. George's mega-set)
152 };
153 
154 // CDT Entry
155 
156 struct CdtEntry {
157 	int16 x;		// sprite x-coord OR offset to add to mega's
158 				// feet x-coord to calc sprite y-coord
159 	int16 y;		// sprite y-coord OR offset to add to mega's
160 				// feet y-coord to calc sprite y-coord
161 	uint32 frameOffset;	// points to start of frame header (from start
162 				// of file header)
163 	uint8 frameType;	// 0 = print sprite normally with top-left
164 				// corner at (x,y), otherwise see below...
165 
166 	static int size();
167 
168 	void read(byte *addr);
169 	void write(byte *addr);
170 };
171 
172 // 'frameType' bit values
173 
174 enum {
175 	FRAME_OFFSET	= 1,	// Print at (feetX + x, feetY + y), with
176 				// scaling according to feetY
177 	FRAME_FLIPPED	= 2,	// Print the frame flipped Left->Right
178 	FRAME_256_FAST	= 4	// Frame has been compressed using Pauls fast
179 				// RLE 256 compression.
180 };
181 
182 // Frame Header
183 
184 struct FrameHeader {
185 	uint32 compSize;	// Compressed size of frame - NB. compression
186 				// type is now in Anim Header
187 	uint16 width;		// Dimensions of frame
188 	uint16 height;
189 
sizeFrameHeader190 	static int size() {
191 		return 8;
192 	}
193 
194 	void read(byte *addr);
195 	void write(byte *addr);
196 };
197 
198 //----------------------------------------------------------
199 // (2) SCREEN FILES
200 //----------------------------------------------------------
201 // a screen file consists of:
202 //
203 // standard file header
204 // multi screen header
205 // 4 * 256 bytes of palette data
206 // 256k palette match table
207 // 2 background parallax layers
208 // 1 background layer with screen header
209 // 2 foreground parallax layers
210 // a string of layer headers
211 // a string of layer masks
212 
213 // Multi screen header
214 // Goes at the beginning of a screen file after the standard header.
215 // Gives offsets from start of table of each of the components
216 
217 struct MultiScreenHeader {
218 	uint32 palette;
219 	uint32 bg_parallax[2];
220 	uint32 screen;
221 	uint32 fg_parallax[2];
222 	uint32 layers;
223 	uint32 paletteTable;
224 	uint32 maskOffset;
225 
sizeMultiScreenHeader226 	static int size() {
227 		return 36;
228 	}
229 
230 	void read(byte *addr);
231 	void write(byte *addr);
232 };
233 
234 // Screen Header
235 
236 struct ScreenHeader {
237 	uint16 width;		// dimensions of the background screen
238 	uint16 height;
239 	uint16 noLayers;	// number of layer areas
240 
sizeScreenHeader241 	static int size() {
242 		return 6;
243 	}
244 
245 	void read(byte *addr);
246 	void write(byte *addr);
247 };
248 
249 // Layer Header
250 
251 // Note that all the layer headers are kept together, rather than being placed
252 // before each layer mask, in order to simplify the sort routine.
253 
254 struct LayerHeader {
255 	uint16 x;		// coordinates of top-left pixel of area
256 	uint16 y;
257 	uint16 width;
258 	uint16 height;
259 	uint32 maskSize;
260 	uint32 offset;		// where to find mask data (from start of
261 				// standard file header)
262 
sizeLayerHeader263 	static int size() {
264 		return 16;
265 	}
266 
267 	void read(byte *addr);
268 	void write(byte *addr);
269 };
270 
271 //----------------------------------------------------------
272 // (3) SCRIPT OBJECT FILES
273 //----------------------------------------------------------
274 // a script object file consists of:
275 //
276 // standard file header
277 // script object header
278 // script object data
279 
280 //----------------------------------------------------------
281 // (5) PALETTE FILES
282 //----------------------------------------------------------
283 // a palette file consists of:
284 //
285 // standard file header
286 // 4 * 256 bytes of palette data
287 // 256k palette match table
288 
289 // an object hub - which represents all that remains of the compact concept
290 
291 class ObjectHub {
292 	// int32 type;		// type of object
293 	// uint32 logic_level;	// what level?
294 	// uint32 logic[3]	// NOT USED
295 	// uint32 script_id[3]	// need this if script
296 	// uint32 script_pc[3]	// need this also
297 
298 private:
299 	byte *_addr;
300 
301 public:
ObjectHub()302 	ObjectHub() {
303 		_addr = NULL;
304 	}
305 
size()306 	static int size() {
307 		return 44;
308 	}
309 
data()310 	byte *data() {
311 		return _addr;
312 	}
313 
setAddress(byte * addr)314 	void setAddress(byte *addr) {
315 		_addr = addr;
316 	}
317 
getScriptPcPtr(int level)318 	byte *getScriptPcPtr(int level) {
319 		return _addr + 32 + 4 * level;
320 	}
321 
getLogicLevel()322 	uint32 getLogicLevel() {
323 		return READ_LE_UINT32(_addr + 4);
324 	}
getScriptId(int level)325 	uint32 getScriptId(int level) {
326 		return READ_LE_UINT32(_addr + 20 + 4 * level);
327 	}
getScriptPc(int level)328 	uint32 getScriptPc(int level) {
329 		return READ_LE_UINT32(_addr + 32 + 4 * level);
330 	}
331 
setLogicLevel(uint32 x)332 	void setLogicLevel(uint32 x) {
333 		WRITE_LE_UINT32(_addr + 4, x);
334 	}
setScriptId(int level,uint32 x)335 	void setScriptId(int level, uint32 x) {
336 		WRITE_LE_UINT32(_addr + 20 + 4 * level, x);
337 	}
setScriptPc(int level,uint32 x)338 	void setScriptPc(int level, uint32 x) {
339 		WRITE_LE_UINT32(_addr + 32 + 4 * level, x);
340 	}
341 };
342 
343 // (6) text module header
344 
345 struct TextHeader {
346 	uint32 noOfLines;	// how many lines of text are there in this
347 				// module
348 
sizeTextHeader349 	static int size() {
350 		return 4;
351 	}
352 
353 	void read(byte *addr);
354 	void write(byte *addr);
355 };
356 
357 // a text file has:
358 //
359 //	ResHeader
360 //	TextHeader
361 //	look up table, to
362 //	line of text,0
363 //	line of text,0
364 
365 //----------------------------------------------------------
366 // SCREENS.CLU file
367 //----------------------------------------------------------
368 // This file is present in PSX version of the game only.
369 // It keeps parallax and background images, aligned at 1024 bytes
370 // for faster access by the psx cd drive.
371 //
372 // SCREENS.CLU structure:
373 // In first 2048 Bytes there's an offset table. Each entry is an
374 // 32bit offset for a background/parallax group. If entry is 0, screen
375 // does not exist.
376 // To find matching screen for the location, you must count LOCATION_NO
377 // words and then go to the corresponding offset indicated by last 32bit
378 // word.
379 // Each screen then begins with a PSXScreensEntry entry:
380 
381 struct PSXScreensEntry {
382 	uint16 fgPlxXres; // If these values are 0, subsequent fgPlx* values must be
383 	uint16 fgPlxYres; // ignored, as this means foreground parallax is not present.
384 	uint32 fgPlxOffset; // This offset is relative, counting from the beginning of Resource Header
385 	uint32 fgPlxSize; // Size of parallax, the size is aligned at 1024 bytes.
386 					  // fgPlxSize/1024 gives number of sector the parallax is divided into.
387 	uint16 bgXres;
388 	uint16 bgYres;
389 	uint32 bgOffset; // relative
390 	uint32 bgSize;
391 	uint16 bgPlxXres; // same considerations for fg parallaxes apply
392 	uint16 bgPlxYres;
393 	uint32 bgPlxOffset; // relative
394 	uint32 bgPlxSize;
395 
sizePSXScreensEntry396 	static int size() {
397 		return 36;
398 	}
399 
400 	void read(byte *addr);
401 	void write(byte *addr);
402 };
403 
404 // PSXFontEntry is present in font resource file, it is used
405 // to address a single char in the character atlas image.
406 
407 struct PSXFontEntry {
408 	uint16 offset;
409 	uint16 skipLines;
410 	uint16 charWidth;
411 	uint16 charHeight;
412 
sizePSXFontEntry413 	static int size() {
414 		return 8;
415 	}
416 
417 	void read(byte *addr);
418 	void write(byte *addr);
419 };
420 
421 } // End of namespace Sword2
422 
423 #endif
424