From b856e8b47ab2dfd393e3c2720c5647eb66393931 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 24 Jan 2012 19:14:24 +0100 Subject: Stream music files directly from the archives Use Mix_LoadMUS_RW to stream music files directly from PhysFS. I kept around ResourceManager:copyFile for now, since it may have other uses. Also cleaned up some initialization of configuration defaults. Reviewed-by: Yohann Ferreira --- src/resources/music.cpp | 50 +++++++++++-------------------------------------- src/resources/music.h | 17 ++++++----------- 2 files changed, 17 insertions(+), 50 deletions(-) (limited to 'src/resources') diff --git a/src/resources/music.cpp b/src/resources/music.cpp index 2154fb5f..677838b6 100644 --- a/src/resources/music.cpp +++ b/src/resources/music.cpp @@ -23,61 +23,33 @@ #include "log.h" -Music::Music(Mix_Chunk *music): - mChunk(music), - mChannel(-1) +Music::Music(Mix_Music *music): + mMusic(music) { } Music::~Music() { - //Mix_FreeMusic(music); - Mix_FreeChunk(mChunk); + Mix_FreeMusic(mMusic); } Resource *Music::load(SDL_RWops *rw) { - // Use Mix_LoadMUS to load the raw music data - //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted - Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1); - - if (tmpMusic) + if (Mix_Music *music = Mix_LoadMUS_RW(rw)) { - return new Music(tmpMusic); + return new Music(music); } else { logger->log("Error, failed to load music: %s", Mix_GetError()); - return NULL; + return 0; } } -bool Music::play(int loops) -{ - /* - * Warning: loops should be always set to -1 (infinite) with current - * implementation to avoid halting the playback of other samples - */ - - /*if (Mix_PlayMusic(music, loops)) - return true;*/ - Mix_VolumeChunk(mChunk, 120); - mChannel = Mix_PlayChannel(-1, mChunk, loops); - - return mChannel != -1; -} - -void Music::stop() +bool Music::play(int loops, int fadeIn) { - /* - * Warning: very dungerous trick, it could try to stop channels occupied - * by samples rather than the current music file - */ - - //Mix_HaltMusic(); - - if (mChannel != -1) - { - Mix_HaltChannel(mChannel); - } + if (fadeIn > 0) + return Mix_FadeInMusic(mMusic, loops, fadeIn); + else + return Mix_PlayMusic(mMusic, loops); } diff --git a/src/resources/music.h b/src/resources/music.h index 6a4cabd5..f744fcd4 100644 --- a/src/resources/music.h +++ b/src/resources/music.h @@ -51,24 +51,19 @@ class Music : public Resource /** * Plays the music. * - * @param loops Number of times to repeat the playback. + * @param loops Number of times to repeat the playback (-1 means + * forever). + * @param fadeIn Duration in milliseconds to fade in the music. * * @return true if the playback started properly * false otherwise. */ - virtual bool play(int loops); - - /** - * Stops the music. - */ - virtual void stop(); + bool play(int loops = -1, int fadeIn = 0); protected: - Music(Mix_Chunk *music); + Music(Mix_Music *music); - //Mix_Music *music; - Mix_Chunk *mChunk; - int mChannel; + Mix_Music *mMusic; }; #endif -- cgit v1.2.3-70-g09d2