comparison test/loopwave.c @ 1151:be9c9c8f6d53

Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe if SDL is built with a non-cdecl calling convention, and it's just generally bad practice anyhow. Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in a cdecl function where it can't be avoided, and rely on the parachute where a crash might have hit the atexit() before (these ARE test programs, after all!).
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 28 Sep 2005 11:36:20 +0000
parents 74212992fb08
children 0394f8ebc42d
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
16 SDL_AudioSpec spec; 16 SDL_AudioSpec spec;
17 Uint8 *sound; /* Pointer to wave data */ 17 Uint8 *sound; /* Pointer to wave data */
18 Uint32 soundlen; /* Length of wave data */ 18 Uint32 soundlen; /* Length of wave data */
19 int soundpos; /* Current play position */ 19 int soundpos; /* Current play position */
20 } wave; 20 } wave;
21
22
23 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
24 static void quit(int rc)
25 {
26 SDL_Quit();
27 exit(rc);
28 }
29
21 30
22 void fillerup(void *unused, Uint8 *stream, int len) 31 void fillerup(void *unused, Uint8 *stream, int len)
23 { 32 {
24 Uint8 *waveptr; 33 Uint8 *waveptr;
25 int waveleft; 34 int waveleft;
52 char name[32]; 61 char name[32];
53 62
54 /* Load the SDL library */ 63 /* Load the SDL library */
55 if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { 64 if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
56 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 65 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
57 exit(1); 66 return(1);
58 } 67 }
59 atexit(SDL_Quit);
60 68
61 if ( argv[1] == NULL ) { 69 if ( argv[1] == NULL ) {
62 fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]); 70 fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
63 exit(1); 71 quit(1);
64 } 72 }
65 73
66 /* Load the wave file into memory */ 74 /* Load the wave file into memory */
67 if ( SDL_LoadWAV(argv[1], 75 if ( SDL_LoadWAV(argv[1],
68 &wave.spec, &wave.sound, &wave.soundlen) == NULL ) { 76 &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
69 fprintf(stderr, "Couldn't load %s: %s\n", 77 fprintf(stderr, "Couldn't load %s: %s\n",
70 argv[1], SDL_GetError()); 78 argv[1], SDL_GetError());
71 exit(1); 79 quit(1);
72 } 80 }
73 wave.spec.callback = fillerup; 81 wave.spec.callback = fillerup;
74 82
75 /* Set the signals */ 83 /* Set the signals */
76 #ifdef SIGHUP 84 #ifdef SIGHUP
84 92
85 /* Initialize fillerup() variables */ 93 /* Initialize fillerup() variables */
86 if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) { 94 if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
87 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); 95 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
88 SDL_FreeWAV(wave.sound); 96 SDL_FreeWAV(wave.sound);
89 exit(2); 97 quit(2);
90 } 98 }
91 SDL_PauseAudio(0); 99 SDL_PauseAudio(0);
92 100
93 /* Let the audio run */ 101 /* Let the audio run */
94 printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32)); 102 printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
96 SDL_Delay(1000); 104 SDL_Delay(1000);
97 105
98 /* Clean up on signal */ 106 /* Clean up on signal */
99 SDL_CloseAudio(); 107 SDL_CloseAudio();
100 SDL_FreeWAV(wave.sound); 108 SDL_FreeWAV(wave.sound);
109 SDL_Quit();
101 return(0); 110 return(0);
102 } 111 }