comparison playsound/playsound.c @ 283:6c059c4d2cea

Added PhysicsFS support.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 16 Mar 2002 03:47:41 +0000
parents 06fbb106a554
children 95082495d437
comparison
equal deleted inserted replaced
282:464089180e49 283:6c059c4d2cea
30 #include <string.h> 30 #include <string.h>
31 #include <assert.h> 31 #include <assert.h>
32 #include <signal.h> 32 #include <signal.h>
33 #include "SDL.h" 33 #include "SDL.h"
34 #include "SDL_sound.h" 34 #include "SDL_sound.h"
35
36 #define SUPPORT_PHYSFS 1
37 #if SUPPORT_PHYSFS
38 #include <alloca.h>
39 #include "physfs.h"
40 #include "physfsrwops.h"
41 #endif
35 42
36 #define DEFAULT_DECODEBUF 16384 43 #define DEFAULT_DECODEBUF 16384
37 #define DEFAULT_AUDIOBUF 4096 44 #define DEFAULT_AUDIOBUF 4096
38 45
39 #define PLAYSOUND_VER_MAJOR 0 46 #define PLAYSOUND_VER_MAJOR 0
350 return AUDIO_S16MSB; 357 return AUDIO_S16MSB;
351 return 0; 358 return 0;
352 } /* str_to_fmt */ 359 } /* str_to_fmt */
353 360
354 361
362 #if SUPPORT_PHYSFS
363 static SDL_RWops *rwops_from_physfs(const char *argv0, const char *filename)
364 {
365 SDL_RWops *retval = NULL;
366
367 char *path = (char *) alloca(strlen(filename) + 1);
368 char *archive;
369
370 strcpy(path, filename);
371 archive = strchr(path, '@');
372 if (archive != NULL)
373 {
374 *(archive++) = '\0'; /* blank '@', point to archive name. */
375
376 if (!PHYSFS_init(argv0))
377 {
378 fprintf(stderr, "Couldn't init PhysicsFS: %s\n",
379 PHYSFS_getLastError());
380 return(NULL);
381 } /* if */
382
383 if (!PHYSFS_addToSearchPath(archive, 0))
384 {
385 fprintf(stderr, "Couldn't open archive: %s\n",
386 PHYSFS_getLastError());
387 return(NULL);
388 } /* if */
389
390 retval = PHYSFSRWOPS_openRead(path);
391 } /* if */
392
393 return(retval);
394 } /* rwops_from_physfs */
395 #endif
396
397
398 static Sound_Sample *sample_from_archive(const char *argv0, const char *fname,
399 Sound_AudioInfo *desired,
400 Uint32 decode_buffersize)
401 {
402 #if SUPPORT_PHYSFS
403 SDL_RWops *rw = rwops_from_physfs(argv0, fname);
404 if (rw != NULL)
405 {
406 char *path = (char *) alloca(strlen(fname) + 1);
407 char *ptr;
408 strcpy(path, fname);
409 ptr = strchr(path, '@');
410 *ptr = '\0';
411 ptr = strrchr(path, '.');
412 if (ptr != NULL)
413 ptr++;
414
415 return(Sound_NewSample(rw, ptr, desired, decode_buffersize));
416 } /* if */
417 #endif
418
419 return(NULL);
420 } /* sample_from_archive */
421
422
355 int main(int argc, char **argv) 423 int main(int argc, char **argv)
356 { 424 {
357 Sound_AudioInfo sound_desired; 425 Sound_AudioInfo sound_desired;
358 Uint32 audio_buffersize = DEFAULT_AUDIOBUF; 426 Uint32 audio_buffersize = DEFAULT_AUDIOBUF;
359 Uint32 decode_buffersize = DEFAULT_DECODEBUF; 427 Uint32 decode_buffersize = DEFAULT_DECODEBUF;
543 * thing on the command line. This is correct behaviour. 611 * thing on the command line. This is correct behaviour.
544 */ 612 */
545 sample = Sound_NewSample(rw, argv[++i], 613 sample = Sound_NewSample(rw, argv[++i],
546 use_specific_audiofmt ? &sound_desired : NULL, 614 use_specific_audiofmt ? &sound_desired : NULL,
547 decode_buffersize); 615 decode_buffersize);
548 } 616 } /* if */
549 617
550 else if (strncmp(argv[i], "--", 2) == 0) 618 else if (strncmp(argv[i], "--", 2) == 0)
551 { 619 {
552 continue; 620 continue;
553 } 621 } /* else if */
554 622
555 else 623 else
556 { 624 {
557 filename = argv[i]; 625 filename = argv[i];
558 sample = Sound_NewSampleFromFile(argv[i], 626 sample = sample_from_archive(argv[0], filename,
559 use_specific_audiofmt ? &sound_desired : NULL, 627 use_specific_audiofmt ? &sound_desired : NULL,
560 decode_buffersize); 628 decode_buffersize);
561 } 629
630 if (sample == NULL)
631 {
632 sample = Sound_NewSampleFromFile(argv[i],
633 use_specific_audiofmt ? &sound_desired : NULL,
634 decode_buffersize);
635 } /* if */
636 } /* else */
562 637
563 if (!sample) 638 if (!sample)
564 { 639 {
565 fprintf(stderr, "Couldn't load \"%s\"!\n" 640 fprintf(stderr, "Couldn't load \"%s\"!\n"
566 " reason: [%s].\n", filename, Sound_GetError()); 641 " reason: [%s].\n", filename, Sound_GetError());
634 delay = 2 * 1000 * sdl_desired.samples / sdl_desired.freq; 709 delay = 2 * 1000 * sdl_desired.samples / sdl_desired.freq;
635 SDL_Delay(delay); 710 SDL_Delay(delay);
636 711
637 SDL_CloseAudio(); /* reopen with next sample's format if possible */ 712 SDL_CloseAudio(); /* reopen with next sample's format if possible */
638 Sound_FreeSample(sample); 713 Sound_FreeSample(sample);
714
715 #if SUPPORT_PHYSFS
716 PHYSFS_deinit(); /* !!! FIXME: move this somewhere? */
717 #endif
639 } /* for */ 718 } /* for */
640 719
641 Sound_Quit(); 720 Sound_Quit();
642 SDL_Quit(); 721 SDL_Quit();
643 return(0); 722 return(0);