QTfrontend/SDLs.cpp
changeset 2515 51d3f4b6293a
parent 2494 1e10a47cabea
child 2520 b785c90b93fe
equal deleted inserted replaced
2514:df9d0728c5bb 2515:51d3f4b6293a
    17  */
    17  */
    18 
    18 
    19 #include "SDLs.h"
    19 #include "SDLs.h"
    20 
    20 
    21 #include "SDL.h"
    21 #include "SDL.h"
       
    22 #include "SDL_mixer.h"
    22 #include "hwconsts.h"
    23 #include "hwconsts.h"
    23 
    24 
    24 #include <QApplication>
    25 #include <QApplication>
       
    26 
    25 
    27 
    26 extern char sdlkeys[1024][2][128];
    28 extern char sdlkeys[1024][2][128];
    27 extern char xb360buttons[][128];
    29 extern char xb360buttons[][128];
    28 extern char xb360dpad[128];
    30 extern char xb360dpad[128];
    29 extern char xbox360axes[][128];
    31 extern char xbox360axes[][128];
    30 
    32 
    31 #ifdef _WIN32
       
    32 bool hardware;
       
    33 #endif
       
    34 extern char *programname;
       
    35 
    33 
    36 SDLInteraction::SDLInteraction()
    34 SDLInteraction::SDLInteraction()
    37 {
    35 {
    38 	music = -1;
    36 
    39 #ifdef _WIN32
       
    40 	hardware = false;
       
    41 #endif
       
    42 	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    37 	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    43 
    38 	
    44 
    39 	musicInitialized = 0;
       
    40 	music = NULL;
    45 	if(SDL_NumJoysticks())
    41 	if(SDL_NumJoysticks())
    46 		addGameControllerKeys();
    42 		addGameControllerKeys();
    47 	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    43 	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    48 }
    44 }
    49 
    45 
    50 SDLInteraction::~SDLInteraction()
    46 SDLInteraction::~SDLInteraction()
    51 {
    47 {
       
    48 	if (musicInitialized == 1) {
       
    49 		if (music != NULL)
       
    50 			Mix_FreeMusic(music);
       
    51 		Mix_CloseAudio();
       
    52 	}
    52 	SDL_Quit();
    53 	SDL_Quit();
    53 	oalb_close();
       
    54 }
    54 }
    55 
       
    56 #ifdef _WIN32
       
    57 void SDLInteraction::setHardwareSound(bool hardware_snd)
       
    58 {
       
    59 	hardware = hardware_snd;
       
    60 }
       
    61 #endif
       
    62 
    55 
    63 QStringList SDLInteraction::getResolutions() const
    56 QStringList SDLInteraction::getResolutions() const
    64 {
    57 {
    65 	QStringList result;
    58 	QStringList result;
    66 
    59 
   160 	// Terminate the list
   153 	// Terminate the list
   161 	sdlkeys[i][0][0] = '\0';
   154 	sdlkeys[i][0][0] = '\0';
   162 	sdlkeys[i][1][0] = '\0';
   155 	sdlkeys[i][1][0] = '\0';
   163 }
   156 }
   164 
   157 
       
   158 void SDLInteraction::SDLMusicInit()
       
   159 {
       
   160 	if (musicInitialized == 0) {
       
   161 		SDL_Init(SDL_INIT_AUDIO);
       
   162 		Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
       
   163 		musicInitialized = 1;
       
   164 	}
       
   165 }
       
   166 
       
   167 
   165 void SDLInteraction::StartMusic()
   168 void SDLInteraction::StartMusic()
   166 {
   169 {
   167 	OpenAL_Init();
   170 	SDLMusicInit();
   168 	if (music < 0) {
   171 
   169 		music = oalb_loadfile(QString(datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData());
   172 	if (music == NULL) {
       
   173 		music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData());
   170 	
   174 	
   171 	}
   175 	}
   172 	oalb_playsound(music, 1);
   176 	Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
   173 	oalb_setvolume(music, 60);
   177 	Mix_FadeInMusic(music, -1, 1750);
   174 	oalb_fadein(music, 50);	
       
   175 }
   178 }
   176 
   179 
   177 void SDLInteraction::StopMusic()
   180 void SDLInteraction::StopMusic()
   178 {
   181 {
   179 	if (music >= 0) oalb_fadeout(music, 20);
   182 	if (music != NULL) {
   180 	oalb_stopsound(music);
   183 		// fade out music to finish 0,5 seconds from now
       
   184 		while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
       
   185 			SDL_Delay(100);
       
   186 		}
       
   187 	}
   181 }
   188 }
   182 
   189 
   183 //we need thjs wrapper because of some issues with windows drivers
       
   184 //beware that this cause a slight delay when playing the first sound
       
   185 void OpenAL_Init()
       
   186 {
       
   187 	if (!oalb_ready())
       
   188 #ifdef _WIN32
       
   189         	oalb_init(programname, hardware ? 1 : 0);
       
   190 #else
       
   191 		oalb_init(programname, 0);
       
   192 #endif
       
   193 }
       
   194