comparison playsound/playsound.c @ 302:414cfef0978e

Cleaned up archive support. Fixes segfault when playing non-archived sounds with latest PhysicsFS.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 08 Apr 2002 13:31:15 +0000
parents 259daddc2d6b
children 6a80b2f9c47c
comparison
equal deleted inserted replaced
301:ca43129df299 302:414cfef0978e
151 PLAYSOUND_VER_MAJOR, PLAYSOUND_VER_MINOR, PLAYSOUND_VER_PATCH); 151 PLAYSOUND_VER_MAJOR, PLAYSOUND_VER_MINOR, PLAYSOUND_VER_PATCH);
152 } /* output_credits */ 152 } /* output_credits */
153 153
154 154
155 155
156 /* archive stuff... */
157
158 static int init_archive(const char *argv0)
159 {
160 int retval = 1;
161
162 #if SUPPORT_PHYSFS
163 retval = PHYSFS_init(argv0);
164 if (!retval)
165 {
166 fprintf(stderr, "Couldn't init PhysicsFS: %s\n",
167 PHYSFS_getLastError());
168 } /* if */
169 #endif
170
171 return(retval);
172 } /* init_archive */
173
174
175 #if SUPPORT_PHYSFS
176 static SDL_RWops *rwops_from_physfs(const char *filename)
177 {
178 SDL_RWops *retval = NULL;
179
180 char *path = (char *) alloca(strlen(filename) + 1);
181 char *archive;
182
183 strcpy(path, filename);
184 archive = strchr(path, '@');
185 if (archive != NULL)
186 {
187 *(archive++) = '\0'; /* blank '@', point to archive name. */
188 if (!PHYSFS_addToSearchPath(archive, 0))
189 {
190 fprintf(stderr, "Couldn't open archive: %s\n",
191 PHYSFS_getLastError());
192 return(NULL);
193 } /* if */
194
195 retval = PHYSFSRWOPS_openRead(path);
196 } /* if */
197
198 return(retval);
199 } /* rwops_from_physfs */
200 #endif
201
202
203 static Sound_Sample *sample_from_archive(const char *fname,
204 Sound_AudioInfo *desired,
205 Uint32 decode_buffersize)
206 {
207 #if SUPPORT_PHYSFS
208 SDL_RWops *rw = rwops_from_physfs(fname);
209 if (rw != NULL)
210 {
211 char *path = (char *) alloca(strlen(fname) + 1);
212 char *ptr;
213 strcpy(path, fname);
214 ptr = strchr(path, '@');
215 *ptr = '\0';
216 ptr = strrchr(path, '.');
217 if (ptr != NULL)
218 ptr++;
219
220 return(Sound_NewSample(rw, ptr, desired, decode_buffersize));
221 } /* if */
222 #endif
223
224 return(NULL);
225 } /* sample_from_archive */
226
227
228 static void close_archive(const char *filename)
229 {
230 #if SUPPORT_PHYSFS
231 char *archive_name = strchr(filename, '@');
232 if (archive_name != NULL)
233 PHYSFS_removeFromSearchPath(archive_name + 1);
234 #endif
235 } /* close_archive */
236
237
238 static void deinit_archive(void)
239 {
240 #if SUPPORT_PHYSFS
241 PHYSFS_deinit();
242 #endif
243 } /* deinit_archive */
244
245
246
156 static volatile int done_flag = 0; 247 static volatile int done_flag = 0;
157 248
158 249
159 void sigint_catcher(int signum) 250 void sigint_catcher(int signum)
160 { 251 {
167 { 258 {
168 SDL_PauseAudio(1); 259 SDL_PauseAudio(1);
169 SDL_CloseAudio(); 260 SDL_CloseAudio();
170 Sound_Quit(); 261 Sound_Quit();
171 SDL_Quit(); 262 SDL_Quit();
263 deinit_archive();
172 exit(1); 264 exit(1);
173 } /* if */ 265 } /* if */
174 266
175 else 267 else
176 { 268 {
356 return AUDIO_S16MSB; 448 return AUDIO_S16MSB;
357 return 0; 449 return 0;
358 } /* str_to_fmt */ 450 } /* str_to_fmt */
359 451
360 452
361 #if SUPPORT_PHYSFS
362 static SDL_RWops *rwops_from_physfs(const char *argv0, const char *filename)
363 {
364 SDL_RWops *retval = NULL;
365
366 char *path = (char *) alloca(strlen(filename) + 1);
367 char *archive;
368
369 strcpy(path, filename);
370 archive = strchr(path, '@');
371 if (archive != NULL)
372 {
373 *(archive++) = '\0'; /* blank '@', point to archive name. */
374
375 if (!PHYSFS_init(argv0))
376 {
377 fprintf(stderr, "Couldn't init PhysicsFS: %s\n",
378 PHYSFS_getLastError());
379 return(NULL);
380 } /* if */
381
382 if (!PHYSFS_addToSearchPath(archive, 0))
383 {
384 fprintf(stderr, "Couldn't open archive: %s\n",
385 PHYSFS_getLastError());
386 return(NULL);
387 } /* if */
388
389 retval = PHYSFSRWOPS_openRead(path);
390 } /* if */
391
392 return(retval);
393 } /* rwops_from_physfs */
394 #endif
395
396
397 static Sound_Sample *sample_from_archive(const char *argv0, const char *fname,
398 Sound_AudioInfo *desired,
399 Uint32 decode_buffersize)
400 {
401 #if SUPPORT_PHYSFS
402 SDL_RWops *rw = rwops_from_physfs(argv0, fname);
403 if (rw != NULL)
404 {
405 char *path = (char *) alloca(strlen(fname) + 1);
406 char *ptr;
407 strcpy(path, fname);
408 ptr = strchr(path, '@');
409 *ptr = '\0';
410 ptr = strrchr(path, '.');
411 if (ptr != NULL)
412 ptr++;
413
414 return(Sound_NewSample(rw, ptr, desired, decode_buffersize));
415 } /* if */
416 #endif
417
418 return(NULL);
419 } /* sample_from_archive */
420
421
422 int main(int argc, char **argv) 453 int main(int argc, char **argv)
423 { 454 {
424 Sound_AudioInfo sound_desired; 455 Sound_AudioInfo sound_desired;
425 Uint32 audio_buffersize = DEFAULT_AUDIOBUF; 456 Uint32 audio_buffersize = DEFAULT_AUDIOBUF;
426 Uint32 decode_buffersize = DEFAULT_DECODEBUF; 457 Uint32 decode_buffersize = DEFAULT_DECODEBUF;
565 sound_desired.format = AUDIO_S16SYS; 596 sound_desired.format = AUDIO_S16SYS;
566 if (sound_desired.channels == 0) 597 if (sound_desired.channels == 0)
567 sound_desired.channels = 2; 598 sound_desired.channels = 2;
568 } /* if */ 599 } /* if */
569 600
601 if (!init_archive(argv[0]))
602 return(42);
603
570 if (SDL_Init(SDL_INIT_AUDIO) == -1) 604 if (SDL_Init(SDL_INIT_AUDIO) == -1)
571 { 605 {
572 fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed!\n" 606 fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed!\n"
573 " reason: [%s].\n", SDL_GetError()); 607 " reason: [%s].\n", SDL_GetError());
574 return(42); 608 return(42);
620 } /* else if */ 654 } /* else if */
621 655
622 else 656 else
623 { 657 {
624 filename = argv[i]; 658 filename = argv[i];
625 sample = sample_from_archive(argv[0], filename, 659 sample = sample_from_archive(filename,
626 use_specific_audiofmt ? &sound_desired : NULL, 660 use_specific_audiofmt ? &sound_desired : NULL,
627 decode_buffersize); 661 decode_buffersize);
628 662
629 if (sample == NULL) 663 if (sample == NULL)
630 { 664 {
631 sample = Sound_NewSampleFromFile(argv[i], 665 sample = Sound_NewSampleFromFile(filename,
632 use_specific_audiofmt ? &sound_desired : NULL, 666 use_specific_audiofmt ? &sound_desired : NULL,
633 decode_buffersize); 667 decode_buffersize);
634 } /* if */ 668 } /* if */
635 } /* else */ 669 } /* else */
636 670
709 SDL_Delay(delay); 743 SDL_Delay(delay);
710 744
711 SDL_CloseAudio(); /* reopen with next sample's format if possible */ 745 SDL_CloseAudio(); /* reopen with next sample's format if possible */
712 Sound_FreeSample(sample); 746 Sound_FreeSample(sample);
713 747
714 #if SUPPORT_PHYSFS 748 close_archive(filename);
715 PHYSFS_deinit(); /* !!! FIXME: move this somewhere? */
716 #endif
717 } /* for */ 749 } /* for */
718 750
719 Sound_Quit(); 751 Sound_Quit();
720 SDL_Quit(); 752 SDL_Quit();
753 deinit_archive();
721 return(0); 754 return(0);
722 } /* main */ 755 } /* main */
723 756
724 /* end of playsound.c ... */ 757 /* end of playsound.c ... */
725 758