Debian Codegraph @

terps/hugo/source/heres.c

raw symbols SCIP
1 /*
2 HERES.C
3
4 contains resource-file management routines:
5
6 DisplayPicture
7 PlayMusic
8 PlaySample
9 PlayVideo
10
11 FindResource
12 GetResourceParameters
13
14 for the Hugo Engine
15
16 Copyright (c) 1995-2009 by Kent Tessman
17
18 NOTE: The normal course of action for loading any resource is
19 first to try and find it in the specified resourcefile. If that
20 fails, an attempt is made to load the resource as an independent
21 file (see FindResource()).
22 */
23
24
25 #include "heheader.h"
26
27 #ifdef HUGO_GLKUNIX
28 #include "glkstart.h"
29 #endif
30
31 /* Function prototypes: */
32 long FindResource(char *filename, char *resname);
33 static int GetResourceParameters(char *filename, char *resname);
34
35 /* from hejpeg.c */
36 int hugo_displaypicture(HUGO_FILE infile, long len);
37
38 /* from hesound.c */
39 int hugo_playmusic(HUGO_FILE infile, long reslength, char loop_flag);
40 int hugo_playsample(HUGO_FILE infile, long reslength, char loop_flag);
41 void hugo_musicvolume(int vol);
42 void hugo_stopmusic(void);
43 void hugo_samplevolume(int vol);
44 void hugo_stopsample(void);
45
46 /* from hevideo.c */
47 #if !defined (COMPILE_V25)
48 int hugo_hasvideo(void);
49 int hugo_playvideo(HUGO_FILE infile, long reslength,
50 char loop_flag, char background, int volume);
51 void hugo_stopvideo(void);
52 #endif
53
54
55 #ifndef MAX_RES_PATH
56 #define MAX_RES_PATH 255
57 #endif
58
59 HUGO_FILE resource_file;
60 int extra_param;
61 char loaded_filename[MAX_RES_PATH];
62 char loaded_resname[MAX_RES_PATH];
63 char resource_type = 0;
64
65
66 /* For system_status: */
67 #define STAT_UNAVAILABLE ((short)-1)
68 #define STAT_NOFILE 101
69 #define STAT_NORESOURCE 102
70 #define STAT_LOADERROR 103
71
72
73 /* DISPLAYPICTURE */
74
75 void DisplayPicture(void)
76 {
77 char filename[MAX_RES_PATH], resname[MAX_RES_PATH];
78 long reslength;
79
80 GetResourceParameters(filename, resname);
81
82 if (!hugo_hasgraphics())
83 {
84 var[system_status] = STAT_UNAVAILABLE;
85 return;
86 }
87
88 /* If filename is empty, no resource file was specified and
89 the line[] array simply holds the path of the file to be loaded
90 as a resource
91 */
92 if (!(reslength = FindResource(filename, resname)))
93 return;
94
95 /* Find out what type of image resource this is */
96 resource_type = (char)((fgetc(resource_file)==0xff)?JPEG_R:UNKNOWN_R);
97 fseek(resource_file, -1, SEEK_CUR);
98
99 /* If FindResource() is successful, the resource file is already
100 open and positioned; hugo_displaypicture() is responsible for
101 closing resource_file before returning regardless of success
102 or failure
103 */
104 if (!hugo_displaypicture(resource_file, reslength))
105 var[system_status] = STAT_LOADERROR;
106 }
107
108
109 /* PLAYMUSIC */
110
111 void PlayMusic(void)
112 {
113 char filename[MAX_RES_PATH], resname[MAX_RES_PATH];
114 char loop_flag = 0;
115 long resstart, reslength;
116
117 if (MEM(codeptr+1)==REPEAT_T) loop_flag = true, codeptr++;
118
119 hugo_stopmusic();
120
121 /* If a 0 parameter is passed, i.e. "music 0" */
122 if (!GetResourceParameters(filename, resname))
123 {
124 return;
125 }
126
127 if (extra_param>=0)
128 {
129 if (extra_param > 100) extra_param = 100;
130 hugo_musicvolume(extra_param);
131 }
132
133 if (!(reslength = FindResource(filename, resname)))
134 return;
135
136 /* Find out what type of music resource this is */
137 resstart = ftell(resource_file);
138
139 /* Check for MIDI */
140 if (fseek(resource_file, resstart, SEEK_SET) == 0
141 && fread(resname, 4, 1, resource_file) == 1
142 && !memcmp(resname, "MThd", 4))
143 {
144 resource_type = MIDI_R;
145 goto Identified;
146 }
147
148 /* Check for XM */
149 if (fseek(resource_file, resstart, SEEK_SET) == 0
150 && fread(resname, 17, 1, resource_file) == 1
151 && !memcmp(resname, "Extended Module: ", 17))
152 {
153 resource_type = XM_R;
154 goto Identified;
155 }
156
157 /* Check for S3M */
158 if (fseek(resource_file, resstart+0x2c, SEEK_SET) == 0
159 && fread(resname, 4, 1, resource_file) == 1
160 && !memcmp(resname, "SCRM", 4))
161 {
162 resource_type = S3M_R;
163 goto Identified;
164 }
165
166 /* Check for MOD */
167 resname[4] = '\0';
168 /* There are a whole bunch of different MOD identifiers: */
169 if (fseek(resource_file, resstart+1080, SEEK_SET) == 0
170 && fread(resname, 4, 1, resource_file) == 1
171 && (!strcmp(resname+1, "CHN") /* 4CHN, 6CHN, 8CHN */
172 || !strcmp(resname+2, "CN") /* 16CN, 32CN */
173 || !strcmp(resname, "M.K.")
174 || !strcmp(resname, "M!K!")
175 || !strcmp(resname, "FLT4")
176 || !strcmp(resname, "CD81")
177 || !strcmp(resname, "OKTA")
178 || !strcmp(resname, " ")))
179 {
180 resource_type = MOD_R;
181 goto Identified;
182 }
183
184 /* Check for MP3 */
185 /* Assume for now that otherwise unidentified is MP3 */
186 else
187 {
188 resource_type = MP3_R;
189 goto Identified;
190 }
191
192 /* No file type identified */
193 resource_type = UNKNOWN_R;
194
195 Identified:
196 fseek(resource_file, resstart, SEEK_SET);
197
198 if (!hugo_playmusic(resource_file, reslength, loop_flag))
199 var[system_status] = STAT_LOADERROR;
200 }
201
202
203 /* PLAYSAMPLE */
204
205 void PlaySample(void)
206 {
207 char filename[MAX_RES_PATH], resname[MAX_RES_PATH];
208 char loop_flag = 0;
209 long reslength;
210
211 if (MEM(codeptr+1)==REPEAT_T) loop_flag = true, codeptr++;
212
213 hugo_stopsample();
214
215 /* If a 0 parameter is passed, i.e. "sound 0" */
216 if (!GetResourceParameters(filename, resname))
217 {
218 return;
219 }
220
221 if (extra_param>=0)
222 {
223 if (extra_param > 100) extra_param = 100;
224 hugo_samplevolume(extra_param);
225 }
226
227 if (!(reslength = FindResource(filename, resname)))
228 return;
229
230 /* Find out what kind of audio sample this is */
231 if (fread(resname, 4, 1, resource_file) == 1 && !memcmp(resname, "WAVE", 4))
232 resource_type = WAVE_R;
233 else
234 resource_type = UNKNOWN_R;
235
236 fseek(resource_file, -4, SEEK_CUR);
237
238 if (!hugo_playsample(resource_file, reslength, loop_flag))
239 var[system_status] = STAT_LOADERROR;
240 }
241
242
243 /* PLAYVIDEO */
244
245 void PlayVideo(void)
246 {
247 char filename[MAX_RES_PATH], resname[MAX_RES_PATH];
248 char loop_flag = 0, background = 0;
249 int volume = 100;
250 long resstart, reslength;
251
252 #if defined (COMPILE_V25)
253 var[system_status] = STAT_UNAVAILABLE;
254 #endif
255 if (MEM(codeptr+1)==REPEAT_T) loop_flag = true, codeptr++;
256
257 #if !defined (COMPILE_V25)
258 hugo_stopvideo();
259 #endif
260
261 /* If a 0 parameter is passed, i.e. "video 0" */
262 if (!GetResourceParameters(filename, resname))
263 {
264 return;
265 }
266
267 if (MEM(codeptr-1)==COMMA_T)
268 {
269 background = (char)GetValue();
270 codeptr++; /* eol */
271 }
272
273 if (extra_param>=0)
274 {
275 if (extra_param > 100) extra_param = 100;
276 volume = extra_param;
277 }
278
279 if (!(reslength = FindResource(filename, resname)))
280 return;
281
282 /* Find out what type of video resource this is */
283 resstart = ftell(resource_file);
284
285 /* Check for MPEG */
286 if (fseek(resource_file, resstart, SEEK_SET) == 0
287 && fread(resname, 4, 1, resource_file) == 1
288 && resname[2]==0x01 && (unsigned char)resname[3]==0xba)
289 {
290 resource_type = MPEG_R;
291 goto Identified;
292 }
293
294 /* Check for AVI */
295 if (fseek(resource_file, resstart+8, SEEK_SET) == 0
296 && fread(resname, 4, 1, resource_file) == 1
297 && !memcmp(resname, "AVI ", 4))
298 {
299 resource_type = AVI_R;
300 goto Identified;
301 }
302
303 /* No file type identified */
304 resource_type = UNKNOWN_R;
305
306 Identified:
307 fseek(resource_file, resstart, SEEK_SET);
308
309 #if !defined (COMPILE_V25)
310 if (!hugo_playvideo(resource_file, reslength, loop_flag, background, volume))
311 var[system_status] = STAT_LOADERROR;
312 #else
313 fclose(resource_file);
314 resource_file = NULL;
315 #endif
316 }
317
318
319 /* FINDRESOURCE
320
321 Assumes that filename/resname contain a resourcefile name and
322 a resource name. If resname is "", filename contains the path
323 of the resource on disk. Returns the length of the resource if
324 if the named resource is found.
325
326 If FindResource() returns non-zero, the file is hot, i.e., it is
327 open and positioned to the start of the resource.
328
329 Note that resourcefiles are expected to be in (if not the current
330 directory) "object" or "games", and on-disk resources in (if not the
331 given directory) "source" or "resource" (where these are the
332 environment variables "HUGO_...", not actual on-disk directories).
333 */
334
335 long FindResource(char *filename, char *resname)
336 {
337 char resource_in_file[MAX_RES_PATH];
338 int i, len;
339 int rescount;
340 unsigned int startofdata;
341 long resposition, reslength;
342 #if defined (GLK)
343 frefid_t fref;
344 #endif
345 /* Previously, resource positions were written as 24 bits, which meant that
346 a given resource couldn't start after 16,777,216 bytes or be more than
347 that length. The new resource file format (designated by 'r') corrects this. */
348 int res_32bits = true;
349
350 resource_file = NULL;
351
352 strcpy(loaded_filename, filename);
353 strcpy(loaded_resname, resname);
354 if (!strcmp(filename, "")) strcpy(loaded_filename, resname);
355
356 /* See if the file is supposed to be in a resourcefile to
357 begin with
358 */
359 if (!strcmp(filename, ""))
360 goto NotinResourceFile;
361
362
363 /* Open the resourcefile */
364 strupr(filename);
365
366 #if !defined (GLK)
367 /* stdio implementation */
368 if (!(resource_file = TrytoOpen(filename, "rb", "games")))
369 if (!(resource_file = TrytoOpen(filename, "rb", "object")))
370 {
371 var[system_status] = STAT_NOFILE;
372 return 0;
373 }
374 #else
375 /* Glk implementation */
376 #ifdef HUGO_GLKUNIX
377 // Under Unix Glk, use glkunix_stream_open_pathname to directly open a file.
378 char temp[1024];
379 snprintf(temp, sizeof temp, "%s/%s", hugo_path_to_game, filename);
380 resource_file = glkunix_stream_open_pathname(temp, 0, 0);
381 #else
382 fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode,
383 filename, 0);
384 if (glk_fileref_does_file_exist(fref))
385 resource_file = glk_stream_open_file(fref, filemode_Read, 0);
386 else
387 resource_file = NULL;
388 glk_fileref_destroy(fref);
389 #endif
390 if (!resource_file)
391 {
392 var[system_status] = STAT_NOFILE;
393 return 0;
394 }
395 #endif
396
397 /* Read the resourcefile header */
398 /* if (fgetc(resource_file)!='R') goto ResfileError; */
399 i = fgetc(resource_file);
400 if (i=='r')
401 res_32bits = true;
402 else if (i=='R')
403 res_32bits = false;
404 else
405 goto ResfileError;
406 /* Read and ignore the resource file version. */
407 fgetc(resource_file);
408 rescount = fgetc(resource_file);
409 rescount += fgetc(resource_file)*256;
410 startofdata = fgetc(resource_file);
411 startofdata += (unsigned int)fgetc(resource_file)*256;
412 if (ferror(resource_file))
413 goto ResfileError;
414
415
416 /* Now skim through the list of resources in the resourcefile to
417 see if we have a match
418 */
419 for (i=1; i<=rescount; i++)
420 {
421 len = fgetc(resource_file);
422 if (ferror(resource_file))
423 goto ResfileError;
424
425 if (!(fgets(resource_in_file, len+1, resource_file)))
426 goto ResfileError;
427
428 resposition = (long)fgetc(resource_file);
429 resposition += (long)fgetc(resource_file)*256L;
430 resposition += (long)fgetc(resource_file)*65536L;
431 if (res_32bits)
432 {
433 resposition += (long)fgetc(resource_file)*16777216L;
434 }
435
436 reslength = (long)fgetc(resource_file);
437 reslength += (long)fgetc(resource_file)*256L;
438 reslength += (long)fgetc(resource_file)*65536L;
439 if (res_32bits)
440 {
441 reslength += (long)fgetc(resource_file)*16777216L;
442 }
443 if (ferror(resource_file)) goto ResfileError;
444
445 if (!strcmp(resname, resource_in_file))
446 {
447 if (fseek(resource_file, (long)startofdata+resposition, SEEK_SET))
448 goto ResfileError;
449 return reslength;
450 }
451 }
452
453 ResfileError:
454
455 var[system_status] = STAT_NORESOURCE;
456
457 #if defined (DEBUGGER)
458 SwitchtoDebugger();
459 sprintf(debug_line, "Unable to find \"%s\" in \"%s\"", resname, filename);
460 DebugMessageBox("Resource Error", debug_line);
461 SwitchtoGame();
462 #endif
463 fclose(resource_file);
464 resource_file = NULL;
465
466
467 /* If we get here, we've either been unable to find the named
468 resource in the given resourcefile, or no resourcefile was
469 given
470 */
471 NotinResourceFile:
472
473 #if !defined (GLK)
474 /* stdio implementation */
475 if (!(resource_file = TrytoOpen(resname, "rb", "resource")))
476 if (!(resource_file = TrytoOpen(resname, "rb", "source")))
477 {
478 if (!strcmp(filename, ""))
479 var[system_status] = STAT_NOFILE;
480 else
481 var[system_status] = STAT_NORESOURCE;
482 return 0;
483 }
484 #else
485 /* Glk implementation */
486 #ifdef HUGO_GLKUNIX
487 snprintf(temp, sizeof temp, "%s/%s", hugo_path_to_game, resname);
488 resource_file = glkunix_stream_open_pathname(temp, 0, 0);
489 #else
490 fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode,
491 resname, 0);
492 if (glk_fileref_does_file_exist(fref))
493 resource_file = glk_stream_open_file(fref, filemode_Read, 0);
494 else
495 resource_file = NULL;
496 glk_fileref_destroy(fref);
497 #endif
498 if (!resource_file)
499 {
500 if (!strcmp(filename, ""))
501 var[system_status] = STAT_NOFILE;
502 else
503 var[system_status] = STAT_NORESOURCE;
504 return 0;
505 }
506 #endif
507
508 /* resource_file here refers to a resource in an individual
509 on-disk file, not a consolidated resource file
510 */
511 fseek(resource_file, 0, SEEK_END);
512 reslength = ftell(resource_file);
513 fseek(resource_file, 0, SEEK_SET);
514 if (ferror(resource_file))
515 {
516 fclose(resource_file);
517 resource_file = NULL;
518 return false;
519 }
520
521 return reslength;
522 }
523
524
525 /* GETRESOURCEPARAMETERS
526
527 Processes resourcefile/filename (and resource, if applicable).
528 Returns 0 if a valid 0 parameter is passed as in "music 0" or
529 "sound 0".
530 */
531
532 static int GetResourceParameters(char *filename, char *resname)
533 {
534 int f;
535
536 var[system_status] = 0;
537
538 extra_param = -1;
539
540 codeptr++; /* token--i.e., 'picture', etc. */
541
542 f = GetValue();
543
544 /* If a 0 parameter is passed for "music 0", etc. */
545 if (!f && MEM(codeptr)!=COMMA_T)
546 {
547 ++codeptr;
548 return 0;
549 }
550
551 strcpy(filename, GetWord((unsigned int)f));
552
553 if (MEM(codeptr++)!=EOL_T) /* two or more parameters */
554 {
555 strupr(filename);
556 strcpy(resname, GetWord(GetValue()));
557 if (MEM(codeptr++)==COMMA_T)
558 {
559 extra_param = GetValue();
560 codeptr++;
561 }
562 }
563 else /* only one parameter */
564 {
565 strcpy(resname, filename);
566 strcpy(filename, "");
567 }
568
569 return true;
570 }