finishing SDLInterface cleanup (wrt audio)
authorsheepluva
Fri, 21 Oct 2011 08:03:42 +0200
changeset 6166 701c5b8fac56
parent 6165 6fe3e922246e
child 6167 728cabee2c9f
finishing SDLInterface cleanup (wrt audio)
QTfrontend/hwform.cpp
QTfrontend/util/SDLInteraction.cpp
QTfrontend/util/SDLInteraction.h
--- a/QTfrontend/hwform.cpp	Fri Oct 21 07:00:49 2011 +0200
+++ b/QTfrontend/hwform.cpp	Fri Oct 21 08:03:42 2011 +0200
@@ -98,6 +98,15 @@
 HWForm::HWForm(QWidget *parent, QString styleSheet)
   : QMainWindow(parent), pnetserver(0), pRegisterServer(0), editedTeam(0), hwnet(0)
 {
+    // set music track
+    QFile * tmpFile =
+            HWDataManager::instance().findFileForRead("Music/main_theme.ogg");
+
+    SDLInteraction::instance().setMusicTrack(tmpFile->fileName());
+
+    // this QFile isn't needed any further
+    delete tmpFile;
+
 #ifdef USE_XFIRE
     xfire_init();
 #endif
--- a/QTfrontend/util/SDLInteraction.cpp	Fri Oct 21 07:00:49 2011 +0200
+++ b/QTfrontend/util/SDLInteraction.cpp	Fri Oct 21 08:03:42 2011 +0200
@@ -20,8 +20,6 @@
 #include "SDL.h"
 #include "SDL_mixer.h"
 
-#include "HWDataManager.h"
-
 #include "HWApplication.h"
 
 #include "SDLInteraction.h"
@@ -44,8 +42,10 @@
 
     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
 
-    musicInitialized = 0;
-    music = NULL;
+    m_audioInitialized = false;
+    m_music = NULL;
+    m_musicTrack = "";
+    m_isPlayingMusic = false;
     if(SDL_NumJoysticks())
         addGameControllerKeys();
     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
@@ -53,12 +53,14 @@
     soundMap = new QMap<QString,Mix_Chunk*>();
 }
 
+
 SDLInteraction::~SDLInteraction()
 {
     stopMusic();
-    if (musicInitialized == 1) {
-        if (music != NULL)
-            Mix_FreeMusic(music);
+    if (m_audioInitialized)
+    {
+        if (m_music != NULL)
+            Mix_FreeMusic(m_music);
         Mix_CloseAudio();
     }
     SDL_Quit();
@@ -66,6 +68,7 @@
     delete soundMap;
 }
 
+
 QStringList SDLInteraction::getResolutions() const
 {
     QStringList result;
@@ -87,6 +90,7 @@
     return result;
 }
 
+
 void SDLInteraction::addGameControllerKeys() const
 {
     QStringList result;
@@ -164,46 +168,77 @@
     sdlkeys[i][1][0] = '\0';
 }
 
-void SDLInteraction::SDLSoundInit()
+
+void SDLInteraction::SDLAudioInit()
 {
-    if (musicInitialized == 0) {
-        SDL_Init(SDL_INIT_AUDIO);
-        Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
-        musicInitialized = 1;
-    }
+    // don't init again
+    if (m_audioInitialized)
+        return;
+
+    SDL_Init(SDL_INIT_AUDIO);
+    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
+    m_audioInitialized = true;
 }
 
+
 void SDLInteraction::playSoundFile(const QString & soundFile)
 {
-    SDLSoundInit();
+    SDLAudioInit();
     if (!soundMap->contains(soundFile))
         soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
 
     Mix_PlayChannel(-1, soundMap->value(soundFile), 0);
 }
 
+
+void SDLInteraction::setMusicTrack(const QString & musicFile)
+{
+    bool wasPlayingMusic = m_isPlayingMusic;
+
+    stopMusic();
+
+    if (m_music != NULL)
+    {
+        Mix_FreeMusic(m_music);
+        m_music = NULL;
+    }
+
+    m_musicTrack = musicFile;
+
+    if (wasPlayingMusic)
+        startMusic();
+}
+
+
 void SDLInteraction::startMusic()
 {
-    SDLSoundInit();
-    QFile * tmpFile = HWDataManager::instance().findFileForRead("Music/main_theme.ogg");
+    if (m_isPlayingMusic)
+        return;
+
+    m_isPlayingMusic = true;
 
-    if (music == NULL)
-        music = Mix_LoadMUS(tmpFile->fileName().toLocal8Bit().constData());
+    if (m_musicTrack.isEmpty())
+        return;
 
-    // this QFile isn't needed any further
-    delete tmpFile;
+    SDLAudioInit();
+
+    if (m_music == NULL)
+        m_music = Mix_LoadMUS(m_musicTrack.toLocal8Bit().constData());
 
     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
-    Mix_FadeInMusic(music, -1, 1750);
+    Mix_FadeInMusic(m_music, -1, 1750);
 }
 
+
 void SDLInteraction::stopMusic()
 {
-    if (music != NULL) {
+    if (m_isPlayingMusic && (m_music != NULL)) {
         // fade out music to finish 0,5 seconds from now
         while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
             SDL_Delay(100);
         }
     }
+
+    m_isPlayingMusic = false;
 }
 
--- a/QTfrontend/util/SDLInteraction.h	Fri Oct 21 07:00:49 2011 +0200
+++ b/QTfrontend/util/SDLInteraction.h	Fri Oct 21 08:03:42 2011 +0200
@@ -45,19 +45,22 @@
     SDLInteraction();
 
     /// Initializes SDL for sound output if needed.
-    void SDLSoundInit();
+    void SDLAudioInit();
 
-    Mix_Music *music;
-    int musicInitialized;
+    bool m_audioInitialized; ///< true if audio is initialized already
+    Mix_Music * m_music; ///< pointer to the music channel of the mixer
+    QString m_musicTrack; ///< path to the music track;
+    bool m_isPlayingMusic; ///< true if music was started but not stopped again.
 
-    QMap<QString,Mix_Chunk*> * soundMap; ///< maps sound file paths to channel
+
+    QMap<QString,Mix_Chunk*> * soundMap; ///< maps sound file paths to channels
 
 public:
     /**
      * @brief Returns reference to the <i>singleton</i> instance of this class.
-     * 
+     *
      * @see <a href="http://en.wikipedia.org/wiki/Singleton_pattern">singleton pattern</a>
-     * 
+     *
      * @return reference to the instance.
      */
     static SDLInteraction & instance();
@@ -77,15 +80,22 @@
 
     /**
      * @brief Plays a sound file.
-     * 
+     *
      * @param soundFile path of the sound file.
      */
     void playSoundFile(const QString & soundFile);
 
-    /// Starts the background music.
+    /**
+     * @brief Sets the music track to be played (or not).
+     *
+     * @param soundFile path of the music file.
+     */
+    void setMusicTrack(const QString & musicFile);
+
+    /// Starts the background music if not already playing.
     void startMusic();
 
-    /// Fades out and stops the background music.
+    /// Fades out and stops the background music (if playing).
     void stopMusic();
 };