QTfrontend/SDLs.cpp
changeset 2428 6800f8aa0184
parent 2418 538a777f90c4
child 2443 fececcbc2189
equal deleted inserted replaced
2427:241e3bb6a146 2428:6800f8aa0184
    19 #include "SDLs.h"
    19 #include "SDLs.h"
    20 
    20 
    21 #include "SDL.h"
    21 #include "SDL.h"
    22 #include "hwconsts.h"
    22 #include "hwconsts.h"
    23 
    23 
       
    24 #include <QApplication>
       
    25 
       
    26 extern char sdlkeys[1024][2][128];
       
    27 extern char xb360buttons[][128];
       
    28 extern char xb360dpad[128];
       
    29 extern char xbox360axes[][128];
       
    30 
    24 bool hardware;
    31 bool hardware;
    25 extern char *programname;
    32 extern char *programname;
    26 
    33 
    27 SDLInteraction::SDLInteraction(bool hardware_snd)
    34 SDLInteraction::SDLInteraction()
    28 {
    35 {
    29 	music = -1;
    36 	music = -1;
    30 	hardware = hardware_snd;
    37 	hardware = false;
    31 	SDL_Init(SDL_INIT_VIDEO);
    38 	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
       
    39 
       
    40 
       
    41 	if(SDL_NumJoysticks())
       
    42 		addGameControllerKeys();
       
    43 	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    32 }
    44 }
    33 
    45 
    34 SDLInteraction::~SDLInteraction()
    46 SDLInteraction::~SDLInteraction()
    35 {
    47 {
    36 	SDL_Quit();
    48 	SDL_Quit();
    37 	openal_close();
    49 	openal_close();
       
    50 }
       
    51 
       
    52 void SDLInteraction::setHardwareSound(bool hardware_snd)
       
    53 {
       
    54 	hardware = hardware_snd;
    38 }
    55 }
    39 
    56 
    40 QStringList SDLInteraction::getResolutions() const
    57 QStringList SDLInteraction::getResolutions() const
    41 {
    58 {
    42 	QStringList result;
    59 	QStringList result;
    54 			if ((modes[i]->w >= 640) && (modes[i]->h >= 480))
    71 			if ((modes[i]->w >= 640) && (modes[i]->h >= 480))
    55 				result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
    72 				result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
    56 	}
    73 	}
    57 
    74 
    58 	return result;
    75 	return result;
       
    76 }
       
    77 
       
    78 void SDLInteraction::addGameControllerKeys() const
       
    79 {
       
    80 	QStringList result;
       
    81 
       
    82 	int i = 0;
       
    83 	while(i < 1024 && sdlkeys[i][1][0] != '\0')
       
    84 		i++;
       
    85 
       
    86 	// Iterate through all game controllers
       
    87 	for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
       
    88 	{
       
    89 		SDL_Joystick* joy = SDL_JoystickOpen(jid);
       
    90 		
       
    91 		// Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only)
       
    92 		QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
       
    93 
       
    94 		// Connected Xbox 360 controller? Use specific button names then
       
    95 		// Might be interesting to add 'named' buttons for the most often used gamepads
       
    96 		bool isxb = joyname.contains("Xbox 360");
       
    97 
       
    98 		// This part of the string won't change for multiple keys/hats, so keep it
       
    99 		QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1);
       
   100 
       
   101 		// Register entries for missing axes not assigned to sticks of this joystick/gamepad
       
   102 		for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++)
       
   103 		{
       
   104 			// Axis 2 on xbox 360 is left/right trigger but those are used as buttons anyway so skip it
       
   105 			if(aid == 2 && isxb)
       
   106 				continue;
       
   107 
       
   108 			// Again store the part of the string not changing for multiple uses
       
   109 			QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1);
       
   110 			
       
   111 			// Entry for "Axis Up"
       
   112 			sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
       
   113 			sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + QApplication::translate("binds (keys)", xbox360axes[aid * 2])) : axis + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str());
       
   114 
       
   115 			// Entry for "Axis Down"
       
   116 			sprintf(sdlkeys[i][0], "j%da%dd", jid, aid);
       
   117 			sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + QApplication::translate("binds (keys)", xbox360axes[aid * 2 + 1])) : axis + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str());
       
   118 		}
       
   119 
       
   120 		// Register entries for all coolie hats of this joystick/gamepad
       
   121 		for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++)
       
   122 		{
       
   123 			// Again store the part of the string not changing for multiple uses
       
   124 			QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1));
       
   125 
       
   126 			// Entry for "Hat Up"
       
   127 			sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);			
       
   128 			sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str());
       
   129 
       
   130 			// Entry for "Hat Down"
       
   131 			sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);			
       
   132 			sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str());
       
   133 
       
   134 			// Entry for "Hat Left"
       
   135 			sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);			
       
   136 			sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str());
       
   137 
       
   138 			// Entry for "Hat Right"
       
   139 			sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);			
       
   140 			sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str());
       
   141 		}
       
   142 		
       
   143 		// Register entries for all buttons of this joystick/gamepad
       
   144 		for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++)
       
   145 		{
       
   146 			// Buttons
       
   147 			sprintf(sdlkeys[i][0], "j%db%d", jid, bid);			
       
   148 			sprintf(sdlkeys[i++][1], "%s", (prefix + ((isxb && bid < 10) ? (QApplication::translate("binds (keys)", xb360buttons[bid]) + QString(" ")) : QApplication::translate("binds (keys)", "Button") + QString(" %1").arg(bid + 1))).toStdString().c_str());
       
   149 		}
       
   150 		// Close the game controller as we no longer need it
       
   151 		SDL_JoystickClose(joy);
       
   152 	}
       
   153 	
       
   154 	// Terminate the list
       
   155 	sdlkeys[i][0][0] = '\0';
       
   156 	sdlkeys[i][1][0] = '\0';
    59 }
   157 }
    60 
   158 
    61 void SDLInteraction::StartMusic()
   159 void SDLInteraction::StartMusic()
    62 {
   160 {
    63 	OpenAL_Init();
   161 	OpenAL_Init();