music_ogg.c
changeset 521 565549e046b0
parent 518 8bc9b5fd2aae
child 542 3de4970b36d4
     1.1 --- a/music_ogg.c	Sat Dec 31 14:23:44 2011 -0500
     1.2 +++ b/music_ogg.c	Sat Dec 31 18:32:49 2011 -0500
     1.3 @@ -61,7 +61,7 @@
     1.4  		SDL_SetError("Couldn't open %s", file);
     1.5  		return NULL;
     1.6  	}
     1.7 -	return OGG_new_RW(rw);
     1.8 +	return OGG_new_RW(rw, 1);
     1.9  }
    1.10  
    1.11  
    1.12 @@ -75,46 +75,52 @@
    1.13      return SDL_RWseek((SDL_RWops*)datasource, (int)offset, whence);
    1.14  }
    1.15  
    1.16 -static int sdl_close_func(void *datasource)
    1.17 -{
    1.18 -    return SDL_RWclose((SDL_RWops*)datasource);
    1.19 -}
    1.20 -
    1.21  static long sdl_tell_func(void *datasource)
    1.22  {
    1.23      return SDL_RWtell((SDL_RWops*)datasource);
    1.24  }
    1.25  
    1.26  /* Load an OGG stream from an SDL_RWops object */
    1.27 -OGG_music *OGG_new_RW(SDL_RWops *rw)
    1.28 +OGG_music *OGG_new_RW(SDL_RWops *rw, int freerw)
    1.29  {
    1.30  	OGG_music *music;
    1.31  	ov_callbacks callbacks;
    1.32  
    1.33 +	SDL_memset(&callbacks, 0, sizeof(callbacks));
    1.34  	callbacks.read_func = sdl_read_func;
    1.35  	callbacks.seek_func = sdl_seek_func;
    1.36 -	callbacks.close_func = sdl_close_func;
    1.37  	callbacks.tell_func = sdl_tell_func;
    1.38  
    1.39  	music = (OGG_music *)malloc(sizeof *music);
    1.40  	if ( music ) {
    1.41  		/* Initialize the music structure */
    1.42  		memset(music, 0, (sizeof *music));
    1.43 +		music->rw = rw;
    1.44 +		music->freerw = freerw;
    1.45  		OGG_stop(music);
    1.46  		OGG_setvolume(music, MIX_MAX_VOLUME);
    1.47  		music->section = -1;
    1.48  
    1.49  		if ( !Mix_Init(MIX_INIT_OGG) ) {
    1.50 +			if ( freerw ) {
    1.51 +				SDL_RWclose(rw);
    1.52 +			}
    1.53  			return(NULL);
    1.54  		}
    1.55  		if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
    1.56  			free(music);
    1.57 -			SDL_RWclose(rw);
    1.58 +			if ( freerw ) {
    1.59 +				SDL_RWclose(rw);
    1.60 +			}
    1.61  			SDL_SetError("Not an Ogg Vorbis audio stream");
    1.62  			return(NULL);
    1.63  		}
    1.64  	} else {
    1.65 +		if ( freerw ) {
    1.66 +			SDL_RWclose(rw);
    1.67 +		}
    1.68  		SDL_OutOfMemory();
    1.69 +		return(NULL);
    1.70  	}
    1.71  	return(music);
    1.72  }
    1.73 @@ -220,6 +226,9 @@
    1.74  		if ( music->cvt.buf ) {
    1.75  			free(music->cvt.buf);
    1.76  		}
    1.77 +		if ( music->freerw ) {
    1.78 +			SDL_RWclose(music->rw);
    1.79 +		}
    1.80  		vorbis.ov_clear(&music->vf);
    1.81  		free(music);
    1.82  	}