QTfrontend/SDLs.cpp
changeset 3697 d5b30d6373fc
parent 3236 4ab3917d7d44
child 4594 5645462cc78f
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    33 
    33 
    34 SDLInteraction::SDLInteraction()
    34 SDLInteraction::SDLInteraction()
    35 {
    35 {
    36 
    36 
    37     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    37     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    38     
    38 
    39     musicInitialized = 0;
    39     musicInitialized = 0;
    40     music = NULL;
    40     music = NULL;
    41     if(SDL_NumJoysticks())
    41     if(SDL_NumJoysticks())
    42         addGameControllerKeys();
    42         addGameControllerKeys();
    43     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    43     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    84 
    84 
    85     // Iterate through all game controllers
    85     // Iterate through all game controllers
    86     for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
    86     for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
    87     {
    87     {
    88         SDL_Joystick* joy = SDL_JoystickOpen(jid);
    88         SDL_Joystick* joy = SDL_JoystickOpen(jid);
    89         
    89 
    90         // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only)
    90         // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only)
    91         QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
    91         QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
    92 
    92 
    93         // Connected Xbox 360 controller? Use specific button names then
    93         // Connected Xbox 360 controller? Use specific button names then
    94         // Might be interesting to add 'named' buttons for the most often used gamepads
    94         // Might be interesting to add 'named' buttons for the most often used gamepads
   100         // Register entries for missing axes not assigned to sticks of this joystick/gamepad
   100         // Register entries for missing axes not assigned to sticks of this joystick/gamepad
   101         for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++)
   101         for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++)
   102         {
   102         {
   103             // Again store the part of the string not changing for multiple uses
   103             // Again store the part of the string not changing for multiple uses
   104             QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1);
   104             QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1);
   105             
   105 
   106             // Entry for "Axis Up"
   106             // Entry for "Axis Up"
   107             sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
   107             sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
   108             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());
   108             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());
   109 
   109 
   110             // Entry for "Axis Down"
   110             // Entry for "Axis Down"
   117         {
   117         {
   118             // Again store the part of the string not changing for multiple uses
   118             // Again store the part of the string not changing for multiple uses
   119             QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1));
   119             QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1));
   120 
   120 
   121             // Entry for "Hat Up"
   121             // Entry for "Hat Up"
   122             sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);            
   122             sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);
   123             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str());
   123             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str());
   124 
   124 
   125             // Entry for "Hat Down"
   125             // Entry for "Hat Down"
   126             sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);            
   126             sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);
   127             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str());
   127             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str());
   128 
   128 
   129             // Entry for "Hat Left"
   129             // Entry for "Hat Left"
   130             sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);            
   130             sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);
   131             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str());
   131             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str());
   132 
   132 
   133             // Entry for "Hat Right"
   133             // Entry for "Hat Right"
   134             sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);            
   134             sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);
   135             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str());
   135             sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str());
   136         }
   136         }
   137         
   137 
   138         // Register entries for all buttons of this joystick/gamepad
   138         // Register entries for all buttons of this joystick/gamepad
   139         for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++)
   139         for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++)
   140         {
   140         {
   141             // Buttons
   141             // Buttons
   142             sprintf(sdlkeys[i][0], "j%db%d", jid, bid);         
   142             sprintf(sdlkeys[i][0], "j%db%d", jid, bid);
   143             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());
   143             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());
   144         }
   144         }
   145         // Close the game controller as we no longer need it
   145         // Close the game controller as we no longer need it
   146         SDL_JoystickClose(joy);
   146         SDL_JoystickClose(joy);
   147     }
   147     }
   148     
   148 
   149     // Terminate the list
   149     // Terminate the list
   150     sdlkeys[i][0][0] = '\0';
   150     sdlkeys[i][0][0] = '\0';
   151     sdlkeys[i][1][0] = '\0';
   151     sdlkeys[i][1][0] = '\0';
   152 }
   152 }
   153 
   153 
   165 {
   165 {
   166     SDLMusicInit();
   166     SDLMusicInit();
   167 
   167 
   168     if (music == NULL) {
   168     if (music == NULL) {
   169         music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData());
   169         music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData());
   170     
   170 
   171     }
   171     }
   172     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
   172     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
   173     Mix_FadeInMusic(music, -1, 1750);
   173     Mix_FadeInMusic(music, -1, 1750);
   174 }
   174 }
   175 
   175