QTfrontend/SDLs.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include "SDLs.h"
       
    20 
       
    21 #include "SDL.h"
       
    22 #include "SDL_mixer.h"
       
    23 #include "hwconsts.h"
       
    24 #include "HWApplication.h"
       
    25 
       
    26 
       
    27 extern char sdlkeys[1024][2][128];
       
    28 extern char xb360buttons[][128];
       
    29 extern char xb360dpad[128];
       
    30 extern char xbox360axes[][128];
       
    31 
       
    32 
       
    33 SDLInteraction::SDLInteraction()
       
    34 {
       
    35 
       
    36     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
       
    37 
       
    38     musicInitialized = 0;
       
    39     music = NULL;
       
    40     if(SDL_NumJoysticks())
       
    41         addGameControllerKeys();
       
    42     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
       
    43 }
       
    44 
       
    45 SDLInteraction::~SDLInteraction()
       
    46 {
       
    47     if (musicInitialized == 1) {
       
    48         if (music != NULL)
       
    49             Mix_FreeMusic(music);
       
    50         Mix_CloseAudio();
       
    51     }
       
    52     SDL_Quit();
       
    53 }
       
    54 
       
    55 QStringList SDLInteraction::getResolutions() const
       
    56 {
       
    57     QStringList result;
       
    58 
       
    59     SDL_Rect **modes;
       
    60 
       
    61     modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
       
    62 
       
    63     if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1))
       
    64     {
       
    65         result << "640x480";
       
    66     } else
       
    67     {
       
    68         for(int i = 0; modes[i]; ++i)
       
    69             if ((modes[i]->w >= 640) && (modes[i]->h >= 480))
       
    70                 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
       
    71     }
       
    72 
       
    73     return result;
       
    74 }
       
    75 
       
    76 void SDLInteraction::addGameControllerKeys() const
       
    77 {
       
    78     QStringList result;
       
    79 
       
    80     int i = 0;
       
    81     while(i < 1024 && sdlkeys[i][1][0] != '\0')
       
    82         i++;
       
    83 
       
    84     // Iterate through all game controllers
       
    85     for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
       
    86     {
       
    87         SDL_Joystick* joy = SDL_JoystickOpen(jid);
       
    88 
       
    89         // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only)
       
    90         QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
       
    91 
       
    92         // Connected Xbox 360 controller? Use specific button names then
       
    93         // Might be interesting to add 'named' buttons for the most often used gamepads
       
    94         bool isxb = joyname.contains("Xbox 360");
       
    95 
       
    96         // This part of the string won't change for multiple keys/hats, so keep it
       
    97         QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1);
       
    98 
       
    99         // Register entries for missing axes not assigned to sticks of this joystick/gamepad
       
   100         for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++)
       
   101         {
       
   102             // Again store the part of the string not changing for multiple uses
       
   103             QString axis = prefix + HWApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1);
       
   104 
       
   105             // Entry for "Axis Up"
       
   106             sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
       
   107             sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2])) : axis + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData());
       
   108 
       
   109             // Entry for "Axis Down"
       
   110             sprintf(sdlkeys[i][0], "j%da%dd", jid, aid);
       
   111             sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2 + 1])) : axis + HWApplication::translate("binds (keys)", "(Down)")).toUtf8().constData());
       
   112         }
       
   113 
       
   114         // Register entries for all coolie hats of this joystick/gamepad
       
   115         for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++)
       
   116         {
       
   117             // Again store the part of the string not changing for multiple uses
       
   118             QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1));
       
   119 
       
   120             // Entry for "Hat Up"
       
   121             sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);
       
   122             sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData());
       
   123 
       
   124             // Entry for "Hat Down"
       
   125             sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);
       
   126             sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Down)")).toUtf8().constData());
       
   127 
       
   128             // Entry for "Hat Left"
       
   129             sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);
       
   130             sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Left)")).toUtf8().constData());
       
   131 
       
   132             // Entry for "Hat Right"
       
   133             sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);
       
   134             sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Right)")).toUtf8().constData());
       
   135         }
       
   136 
       
   137         // Register entries for all buttons of this joystick/gamepad
       
   138         for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++)
       
   139         {
       
   140             // Buttons
       
   141             sprintf(sdlkeys[i][0], "j%db%d", jid, bid);
       
   142             sprintf(sdlkeys[i++][1], "%s", (prefix + ((isxb && bid < 10) ? (HWApplication::translate("binds (keys)", xb360buttons[bid]) + QString(" ")) : HWApplication::translate("binds (keys)", "Button") + QString(" %1").arg(bid + 1))).toUtf8().constData());
       
   143         }
       
   144         // Close the game controller as we no longer need it
       
   145         SDL_JoystickClose(joy);
       
   146     }
       
   147 
       
   148     // Terminate the list
       
   149     sdlkeys[i][0][0] = '\0';
       
   150     sdlkeys[i][1][0] = '\0';
       
   151 }
       
   152 
       
   153 void SDLInteraction::SDLMusicInit()
       
   154 {
       
   155     if (musicInitialized == 0) {
       
   156         SDL_Init(SDL_INIT_AUDIO);
       
   157         Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
       
   158         musicInitialized = 1;
       
   159     }
       
   160 }
       
   161 
       
   162 
       
   163 void SDLInteraction::StartMusic()
       
   164 {
       
   165     SDLMusicInit();
       
   166     QFile tmpfile;
       
   167 
       
   168     tmpfile.setFileName(cfgdir->absolutePath() + "/Data/Music/main_theme.ogg");
       
   169     if (!tmpfile.exists()) tmpfile.setFileName(datadir->absolutePath() + "/Music/main_theme.ogg");
       
   170     if (music == NULL) {
       
   171         music = Mix_LoadMUS(QFileInfo(tmpfile).absoluteFilePath().toLocal8Bit().constData());
       
   172 
       
   173     }
       
   174     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
       
   175     Mix_FadeInMusic(music, -1, 1750);
       
   176 }
       
   177 
       
   178 void SDLInteraction::StopMusic()
       
   179 {
       
   180     if (music != NULL) {
       
   181         // fade out music to finish 0,5 seconds from now
       
   182         while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
       
   183             SDL_Delay(100);
       
   184         }
       
   185     }
       
   186 }
       
   187