Debian Codegraph @

src/animlib.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "animlib.h"
20
21 #include "file.h"
22 #include "keyboard.h"
23 #include "network.h"
24 #include "nortsong.h"
25 #include "palette.h"
26 #include "sizebuf.h"
27 #include "video.h"
28
29 #include <assert.h>
30 #include <string.h>
31
32 /*** Structs ***/
33 /* The actual header has a lot of fields that are basically useless to us since
34 * we both set our own framerate and the format itself only allows for
35 * 320x200x8. Should a (nonexistent) ani be played that doesn't have the same
36 * assumed values we are going to use, TOO BAD. It'll just be treated as
37 * corrupt in playback.
38 */
39 #define PALETTE_OFFSET 0x100 // 128 + sizeof(header)
40 #define PAGEHEADER_OFFSET 0x500 // PALETTE_OFFSET + sizeof(palette)
41 #define ANIM_OFFSET 0x0B00 // PAGEHEADER_OFFSET + sizeof(largepageheader) * 256
42 #define ANI_PAGE_SIZE 0x10000 // 65536.
43
44 typedef struct anim_FileHeader_s
45 {
46 Uint16 nlps; /* Number of 'pages', max 256. */
47 Uint32 nRecords; /* Number of 'records', max 65535 */
48 } anim_FileHeader_t;
49
50 typedef struct anim_LargePageHeader_s
51 {
52 Uint16 baseRecord; /* The first record's number */
53 Uint16 nRecords; /* Number of records. Supposedly there are bit flags but I saw no such code */
54 Uint16 nBytes; /* Number of bytes used, excluding headers */
55 } anim_LargePageHeader_t;
56
57 /*** Globals ***/
58 Uint8 CurrentPageBuffer[65536];
59 anim_LargePageHeader_t PageHeader[256];
60 Uint16 CurrentPageRecordSizes[256];
61
62 anim_LargePageHeader_t CurrentPageHeader;
63 anim_FileHeader_t FileHeader;
64
65 unsigned int Curlpnum;
66
67 FILE * InFile;
68
69 /*** Function decs ***/
70 int JE_playRunSkipDump(Uint8 *, unsigned int);
71 void JE_closeAnim(void);
72 int JE_loadAnim(const char *);
73 int JE_renderFrame(unsigned int);
74 int JE_findPage (unsigned int);
75 int JE_loadPage(unsigned int);
76
77 /*** Implementation ***/
78
79 /* Loads the given page into memory.
80 *
81 * Returns 0 on success or nonzero on failure (bad data)
82 */
83 int JE_loadPage(unsigned int pagenumber)
84 {
85 unsigned int i, pageSize;
86
87 if (Curlpnum == pagenumber)
88 return 0; /* Already loaded */
89 Curlpnum = pagenumber;
90
91 /* We need to seek to the page and load it into our buffer.
92 * Pages have a fixed size of 0x10000; any left over space is padded
93 * unless it's the end of the file.
94 *
95 * Pages repeat their headers for some reason. They then have two bytes of
96 * padding followed by a word for every record. THEN the data starts.
97 */
98 fseek(InFile, ANIM_OFFSET + (pagenumber * ANI_PAGE_SIZE), SEEK_SET);
99 fread_u16_die(&CurrentPageHeader.baseRecord, 1, InFile);
100 fread_u16_die(&CurrentPageHeader.nRecords, 1, InFile);
101 fread_u16_die(&CurrentPageHeader.nBytes, 1, InFile);
102
103 fseek(InFile, 2, SEEK_CUR);
104 fread_u16_die(CurrentPageRecordSizes, CurrentPageHeader.nRecords, InFile);
105
106 /* What remains is the 'compressed' data */
107 fread_die(CurrentPageBuffer, 1, CurrentPageHeader.nBytes, InFile);
108
109 /* Okay, we've succeeded in all our IO checks. Now, make sure the
110 * headers aren't lying or damaged or something.
111 */
112 pageSize = 0;
113 for (i = 0; i < CurrentPageHeader.nRecords; i++)
114 pageSize += CurrentPageRecordSizes[i];
115
116 if (pageSize != CurrentPageHeader.nBytes)
117 return -1;
118
119 /* So far, so good */
120 return 0;
121 }
122
123 int JE_findPage(unsigned int framenumber)
124 {
125 unsigned int i;
126
127 for (i = 0; i < FileHeader.nlps; i++)
128 {
129 if (PageHeader[i].baseRecord <= framenumber &&
130 PageHeader[i].baseRecord + PageHeader[i].nRecords > framenumber)
131 {
132 return i;
133 }
134 }
135
136 return -1; /* Did not find */
137 }
138
139 int JE_renderFrame(unsigned int framenumber)
140 {
141 unsigned int i, offset, destframe;
142
143 destframe = framenumber - CurrentPageHeader.baseRecord;
144
145 offset = 0;
146 for (i = 0; i < destframe; i++)
147 offset += CurrentPageRecordSizes[i];
148
149 return (JE_playRunSkipDump(CurrentPageBuffer + offset + 4, CurrentPageRecordSizes[destframe] - 4));
150 }
151
152 void JE_playAnim(const char *animfile, JE_byte startingframe, JE_byte speed)
153 {
154 unsigned int i;
155 int pageNum;
156
157 if (JE_loadAnim(animfile) != 0)
158 return; /* Failed to open or process file */
159
160 /* Blank screen */
161 JE_clr256(VGAScreen);
162 JE_showVGA();
163
164 /* re FileHeader.nRecords-1: It's -1 in the pascal too.
165 * The final frame is a delta of the first, and we don't need that.
166 * We could also, if we ever ended up needing to loop anis, check
167 * the bools in the header to see if we should render the last
168 * frame. But that's never going to be necessary :)
169 */
170 for (i = startingframe; i < FileHeader.nRecords-1; i++)
171 {
172 /* Handle boring crap */
173 setDelay(speed);
174
175 /* Load required frame. The loading function is smart enough to not re-load an already loaded frame */
176 pageNum = JE_findPage(i);
177 if (pageNum == -1)
178 break;
179 if (JE_loadPage(pageNum) != 0)
180 break;
181
182 /* render frame. */
183 if (JE_renderFrame(i) != 0)
184 break;
185 JE_showVGA();
186
187 /* Return early if user presses a key */
188 service_SDL_events(true);
189 if (newkey)
190 break;
191
192 /* Wait until we need the next frame */
193 NETWORK_KEEP_ALIVE();
194 wait_delay();
195 }
196
197 JE_closeAnim();
198 }
199
200 /* loadAnim opens the file and loads data from it into the header structs.
201 * It should take care to clean up after itself should an error occur.
202 */
203 int JE_loadAnim(const char *filename)
204 {
205 unsigned int i;
206 long fileSize;
207 char temp[4];
208
209 Curlpnum = -1;
210 InFile = dir_fopen(data_dir(), filename, "rb");
211 if (InFile == NULL)
212 return -1;
213
214 fileSize = ftell_eof(InFile);
215 if (fileSize < ANIM_OFFSET)
216 {
217 /* We don't know the exact size our file should be yet,
218 * but we do know it should be way more than this */
219 fclose(InFile);
220 return -1;
221 }
222
223 /* Read in the header. The header is 256 bytes long or so,
224 * but that includes a lot of padding as well as several
225 * vars we really don't care about. We shall check the ID and extract
226 * the handful of vars we care about. Every value in the header that
227 * is constant will be ignored.
228 */
229
230 fread_die(&temp, 1, 4, InFile); /* The ID, should equal "LPF " */
231 fseek(InFile, 2, SEEK_CUR); /* skip over this word */
232 fread_u16_die(&FileHeader.nlps, 1, InFile); /* Number of pages */
233 fread_u32_die(&FileHeader.nRecords, 1, InFile); /* Number of records */
234
235 if (memcmp(temp, "LPF ", 4) != 0 ||
236 FileHeader.nlps == 0 || FileHeader.nRecords == 0 ||
237 FileHeader.nlps > 256 || FileHeader.nRecords > 65535)
238 {
239 fclose(InFile);
240 return -1;
241 }
242
243 /* Read in headers */
244 fseek(InFile, PAGEHEADER_OFFSET, SEEK_SET);
245 for (i = 0; i < FileHeader.nlps; i++)
246 {
247 fread_u16_die(&PageHeader[i].baseRecord, 1, InFile);
248 fread_u16_die(&PageHeader[i].nRecords, 1, InFile);
249 fread_u16_die(&PageHeader[i].nBytes, 1, InFile);
250 }
251
252 /* Now we have enough information to calculate the 'expected' file size.
253 * Our calculation SHOULD be equal to fileSize, but we won't begrudge
254 * padding */
255 if (fileSize < (FileHeader.nlps-1) * ANI_PAGE_SIZE + ANIM_OFFSET
256 + PageHeader[FileHeader.nlps-1].nBytes
257 + PageHeader[FileHeader.nlps-1].nRecords * 2 + 8)
258 {
259 fclose(InFile);
260 return -1;
261 }
262
263 /* Now read in the palette. */
264 fseek(InFile, PALETTE_OFFSET, SEEK_SET);
265 for (i = 0; i < 256; i++)
266 {
267 Uint8 bgru[4];
268 fread_u8_die(bgru, 4, InFile);
269 colors[i].b = bgru[0];
270 colors[i].g = bgru[1];
271 colors[i].r = bgru[2];
272 }
273 set_palette(colors, 0, 255);
274
275 /* Whew! That was hard. Let's go grab some beers! */
276 return 0;
277 }
278
279 void JE_closeAnim(void)
280 {
281 fclose(InFile);
282 }
283
284 /* RunSkipDump decompresses the video. There are three operations, run, skip,
285 * and dump. They can be used in either byte or word variations, making six
286 * possible actions, and there's a seventh 'stop' action, which looks
287 * like 0x80 0x00 0x00.
288 *
289 * Run is a memset.
290 * Dump is a memcpy.
291 * Skip leaves the old data intact and simply increments the pointers.
292 *
293 * returns 0 on success or 1 if decompressing failed. Failure to decompress
294 * indicates a broken or malicious file; playback should terminate.
295 */
296 int JE_playRunSkipDump(Uint8 *incomingBuffer, unsigned int IncomingBufferLength)
297 {
298 sizebuf_t Buffer_IN, Buffer_OUT;
299 sizebuf_t * pBuffer_IN = &Buffer_IN, * pBuffer_OUT = &Buffer_OUT;
300
301 #define ANI_SHORT_RLE 0x00
302 #define ANI_SHORT_SKIP 0x80
303 #define ANI_LONG_OP 0x80
304 #define ANI_LONG_COPY_OR_RLE 0x8000
305 #define ANI_LONG_RLE 0x4000
306 #define ANI_STOP 0x0000
307
308 SZ_Init(pBuffer_IN, incomingBuffer, IncomingBufferLength);
309 SZ_Init(pBuffer_OUT, VGAScreen->pixels, VGAScreen->h * VGAScreen->pitch);
310
311 /* 320x200 is the only supported format.
312 * Assert is here as a hint should our screen size ever changes.
313 * As for how to decompress to the wrong screen size... */
314 assert(VGAScreen->h * VGAScreen->pitch == 320 * 200);
315
316 while (true)
317 {
318 /* Get one byte. This byte may have flags that tell us more */
319 unsigned int opcode = MSG_ReadByte(pBuffer_IN);
320
321 /* Before we continue, check the error states/
322 * We should *probably* check these after every read and write, but
323 * I've rigged it so that the buffers will never go out of bounds.
324 * So we can afford to be lazy; if the buffer overflows below it will
325 * silently fail its writes and we'll catch the failure on our next
326 * run through the loop. A failure means we should be
327 * leaving ANYWAY. The contents of our buffers doesn't matter.
328 */
329 if (SZ_Error(pBuffer_IN) || SZ_Error(pBuffer_OUT))
330 return -1;
331
332 /* Divide into 'short' and 'long' */
333 if (opcode == ANI_LONG_OP) /* long ops */
334 {
335 opcode = MSG_ReadWord(pBuffer_IN);
336
337 if (opcode == ANI_STOP) /* We are done decompressing. Leave */
338 {
339 break;
340 }
341 else if (!(opcode & ANI_LONG_COPY_OR_RLE)) /* If it's not those two, it's a skip */
342 {
343 unsigned int count = opcode;
344 SZ_Seek(pBuffer_OUT, count, SEEK_CUR);
345 }
346 else /* Now things get a bit more interesting... */
347 {
348 opcode &= ~ANI_LONG_COPY_OR_RLE; /* Clear that flag */
349
350 if (opcode & ANI_LONG_RLE) /* RLE */
351 {
352 unsigned int count = opcode & ~ANI_LONG_RLE; /* Clear flag */
353
354 /* Extract another byte */
355 unsigned int value = MSG_ReadByte(pBuffer_IN);
356
357 /* The actual run */
358 SZ_Memset(pBuffer_OUT, value, count);
359 }
360 else
361 { /* Long copy */
362 unsigned int count = opcode;
363
364 /* Copy */
365 SZ_Memcpy2(pBuffer_OUT, pBuffer_IN, count);
366 }
367 }
368 } /* End of long ops */
369 else /* short ops */
370 {
371 if (opcode & ANI_SHORT_SKIP) /* Short skip, move pointer only */
372 {
373 unsigned int count = opcode & ~ANI_SHORT_SKIP; /* clear flag to get count */
374 SZ_Seek(pBuffer_OUT, count, SEEK_CUR);
375 }
376 else if (opcode == ANI_SHORT_RLE) /* Short RLE, memset the destination */
377 {
378 /* Extract a few more bytes */
379 unsigned int count = MSG_ReadByte(pBuffer_IN);
380 unsigned int value = MSG_ReadByte(pBuffer_IN);
381
382 /* Run */
383 SZ_Memset(pBuffer_OUT, value, count);
384 }
385 else /* Short copy, memcpy from src to dest. */
386 {
387 unsigned int count = opcode;
388
389 /* Dump */
390 SZ_Memcpy2(pBuffer_OUT, pBuffer_IN, count);
391 }
392 } /* End of short ops */
393 }
394
395 /* And that's that */
396 return 0;
397 }