QTfrontend/util/SDLInteraction.cpp
changeset 6166 701c5b8fac56
parent 6165 6fe3e922246e
child 6167 728cabee2c9f
equal deleted inserted replaced
6165:6fe3e922246e 6166:701c5b8fac56
    18 
    18 
    19 
    19 
    20 #include "SDL.h"
    20 #include "SDL.h"
    21 #include "SDL_mixer.h"
    21 #include "SDL_mixer.h"
    22 
    22 
    23 #include "HWDataManager.h"
       
    24 
       
    25 #include "HWApplication.h"
    23 #include "HWApplication.h"
    26 
    24 
    27 #include "SDLInteraction.h"
    25 #include "SDLInteraction.h"
    28 
    26 
    29 extern char sdlkeys[1024][2][128];
    27 extern char sdlkeys[1024][2][128];
    42 SDLInteraction::SDLInteraction()
    40 SDLInteraction::SDLInteraction()
    43 {
    41 {
    44 
    42 
    45     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    43     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    46 
    44 
    47     musicInitialized = 0;
    45     m_audioInitialized = false;
    48     music = NULL;
    46     m_music = NULL;
       
    47     m_musicTrack = "";
       
    48     m_isPlayingMusic = false;
    49     if(SDL_NumJoysticks())
    49     if(SDL_NumJoysticks())
    50         addGameControllerKeys();
    50         addGameControllerKeys();
    51     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    51     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    52 
    52 
    53     soundMap = new QMap<QString,Mix_Chunk*>();
    53     soundMap = new QMap<QString,Mix_Chunk*>();
    54 }
    54 }
    55 
    55 
       
    56 
    56 SDLInteraction::~SDLInteraction()
    57 SDLInteraction::~SDLInteraction()
    57 {
    58 {
    58     stopMusic();
    59     stopMusic();
    59     if (musicInitialized == 1) {
    60     if (m_audioInitialized)
    60         if (music != NULL)
    61     {
    61             Mix_FreeMusic(music);
    62         if (m_music != NULL)
       
    63             Mix_FreeMusic(m_music);
    62         Mix_CloseAudio();
    64         Mix_CloseAudio();
    63     }
    65     }
    64     SDL_Quit();
    66     SDL_Quit();
    65 
    67 
    66     delete soundMap;
    68     delete soundMap;
    67 }
    69 }
       
    70 
    68 
    71 
    69 QStringList SDLInteraction::getResolutions() const
    72 QStringList SDLInteraction::getResolutions() const
    70 {
    73 {
    71     QStringList result;
    74     QStringList result;
    72 
    75 
    84                 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
    87                 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
    85     }
    88     }
    86 
    89 
    87     return result;
    90     return result;
    88 }
    91 }
       
    92 
    89 
    93 
    90 void SDLInteraction::addGameControllerKeys() const
    94 void SDLInteraction::addGameControllerKeys() const
    91 {
    95 {
    92     QStringList result;
    96     QStringList result;
    93 
    97 
   162     // Terminate the list
   166     // Terminate the list
   163     sdlkeys[i][0][0] = '\0';
   167     sdlkeys[i][0][0] = '\0';
   164     sdlkeys[i][1][0] = '\0';
   168     sdlkeys[i][1][0] = '\0';
   165 }
   169 }
   166 
   170 
   167 void SDLInteraction::SDLSoundInit()
   171 
   168 {
   172 void SDLInteraction::SDLAudioInit()
   169     if (musicInitialized == 0) {
   173 {
   170         SDL_Init(SDL_INIT_AUDIO);
   174     // don't init again
   171         Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
   175     if (m_audioInitialized)
   172         musicInitialized = 1;
   176         return;
   173     }
   177 
   174 }
   178     SDL_Init(SDL_INIT_AUDIO);
       
   179     Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
       
   180     m_audioInitialized = true;
       
   181 }
       
   182 
   175 
   183 
   176 void SDLInteraction::playSoundFile(const QString & soundFile)
   184 void SDLInteraction::playSoundFile(const QString & soundFile)
   177 {
   185 {
   178     SDLSoundInit();
   186     SDLAudioInit();
   179     if (!soundMap->contains(soundFile))
   187     if (!soundMap->contains(soundFile))
   180         soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
   188         soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
   181 
   189 
   182     Mix_PlayChannel(-1, soundMap->value(soundFile), 0);
   190     Mix_PlayChannel(-1, soundMap->value(soundFile), 0);
   183 }
   191 }
   184 
   192 
       
   193 
       
   194 void SDLInteraction::setMusicTrack(const QString & musicFile)
       
   195 {
       
   196     bool wasPlayingMusic = m_isPlayingMusic;
       
   197 
       
   198     stopMusic();
       
   199 
       
   200     if (m_music != NULL)
       
   201     {
       
   202         Mix_FreeMusic(m_music);
       
   203         m_music = NULL;
       
   204     }
       
   205 
       
   206     m_musicTrack = musicFile;
       
   207 
       
   208     if (wasPlayingMusic)
       
   209         startMusic();
       
   210 }
       
   211 
       
   212 
   185 void SDLInteraction::startMusic()
   213 void SDLInteraction::startMusic()
   186 {
   214 {
   187     SDLSoundInit();
   215     if (m_isPlayingMusic)
   188     QFile * tmpFile = HWDataManager::instance().findFileForRead("Music/main_theme.ogg");
   216         return;
   189 
   217 
   190     if (music == NULL)
   218     m_isPlayingMusic = true;
   191         music = Mix_LoadMUS(tmpFile->fileName().toLocal8Bit().constData());
   219 
   192 
   220     if (m_musicTrack.isEmpty())
   193     // this QFile isn't needed any further
   221         return;
   194     delete tmpFile;
   222 
       
   223     SDLAudioInit();
       
   224 
       
   225     if (m_music == NULL)
       
   226         m_music = Mix_LoadMUS(m_musicTrack.toLocal8Bit().constData());
   195 
   227 
   196     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
   228     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
   197     Mix_FadeInMusic(music, -1, 1750);
   229     Mix_FadeInMusic(m_music, -1, 1750);
   198 }
   230 }
       
   231 
   199 
   232 
   200 void SDLInteraction::stopMusic()
   233 void SDLInteraction::stopMusic()
   201 {
   234 {
   202     if (music != NULL) {
   235     if (m_isPlayingMusic && (m_music != NULL)) {
   203         // fade out music to finish 0,5 seconds from now
   236         // fade out music to finish 0,5 seconds from now
   204         while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
   237         while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
   205             SDL_Delay(100);
   238             SDL_Delay(100);
   206         }
   239         }
   207     }
   240     }
   208 }
   241 
   209 
   242     m_isPlayingMusic = false;
       
   243 }
       
   244