QTfrontend/util/SDLInteraction.cpp
author Xeli
Tue, 24 Jan 2012 03:04:23 +0100
changeset 6593 6f978fca3711
parent 6585 6225b838a630
child 6616 f77bb02b669f
permissions -rw-r--r--
revert Android.mk back after copying SDL_mixer and SDL_ttf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 555
diff changeset
     2
 * Hedgewars, a free turn based strategy game
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4594
diff changeset
     3
 * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     4
 *
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     8
 *
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    13
 *
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    17
 */
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    18
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    19
/**
6170
2b1748161278 fix multiplayer page layout, doc/comment fixes
sheepluva
parents: 6168
diff changeset
    20
 * @file
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    21
 * @brief SDLInteraction class implementation
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    22
 */
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    23
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    24
#include "SDL.h"
2515
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
    25
#include "SDL_mixer.h"
6164
62aa418ed214 change SDL.h/SDL.cpp to use HWDataManager instead of poking around on the harddrive - also I added doc/comments to the class
sheepluva
parents: 5747
diff changeset
    26
5252
ded882439548 file association for mac!
koda
parents: 5238
diff changeset
    27
#include "HWApplication.h"
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    28
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    29
#include "SDLInteraction.h"
2515
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
    30
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    31
extern char sdlkeys[1024][2][128];
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    32
extern char xb360buttons[][128];
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    33
extern char xb360dpad[128];
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    34
extern char xbox360axes[][128];
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    35
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    36
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    37
SDLInteraction & SDLInteraction::instance()
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    38
{
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    39
    static SDLInteraction instance;
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    40
    return instance;
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    41
}
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    42
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    43
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    44
SDLInteraction::SDLInteraction()
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    45
{
5085
e8944ed92b61 Reverting..
Zorg <zorgiepoo@gmail.com>
parents: 5083
diff changeset
    46
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    47
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
    48
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    49
    m_audioInitialized = false;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    50
    m_music = NULL;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    51
    m_musicTrack = "";
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    52
    m_isPlayingMusic = false;
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    53
    if(SDL_NumJoysticks())
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    54
        addGameControllerKeys();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    55
    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    56
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
    57
    m_soundMap = new QMap<QString,Mix_Chunk*>();
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    58
}
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    59
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    60
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    61
SDLInteraction::~SDLInteraction()
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    62
{
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    63
    stopMusic();
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    64
    if (m_audioInitialized)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    65
    {
6524
416fdfb666da make sure to stop music before freeing it
koda
parents: 6170
diff changeset
    66
        if (m_music != NULL) {
416fdfb666da make sure to stop music before freeing it
koda
parents: 6170
diff changeset
    67
            Mix_HaltMusic();
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    68
            Mix_FreeMusic(m_music);
6524
416fdfb666da make sure to stop music before freeing it
koda
parents: 6170
diff changeset
    69
        }
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    70
        Mix_CloseAudio();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    71
    }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    72
    SDL_Quit();
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    73
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
    74
    delete m_soundMap;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    75
}
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    76
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    77
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    78
QStringList SDLInteraction::getResolutions() const
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    79
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    80
    QStringList result;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    81
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    82
    SDL_Rect **modes;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    83
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    84
    modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    85
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    86
    if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1))
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    87
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    88
        result << "640x480";
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    89
    } else
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    90
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    91
        for(int i = 0; modes[i]; ++i)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    92
            if ((modes[i]->w >= 640) && (modes[i]->h >= 480))
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    93
                result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    94
    }
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    95
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    96
    return result;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    97
}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents: 2050
diff changeset
    98
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    99
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   100
void SDLInteraction::addGameControllerKeys() const
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   101
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   102
    QStringList result;
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   103
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   104
    int i = 0;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   105
    while(i < 1024 && sdlkeys[i][1][0] != '\0')
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   106
        i++;
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   107
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   108
    // Iterate through all game controllers
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   109
    for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   110
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   111
        SDL_Joystick* joy = SDL_JoystickOpen(jid);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   112
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   113
        // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   114
        QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   115
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   116
        // Connected Xbox 360 controller? Use specific button names then
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   117
        // Might be interesting to add 'named' buttons for the most often used gamepads
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   118
        bool isxb = joyname.contains("Xbox 360");
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   119
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   120
        // This part of the string won't change for multiple keys/hats, so keep it
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   121
        QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1);
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   122
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   123
        // Register entries for missing axes not assigned to sticks of this joystick/gamepad
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   124
        for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   125
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   126
            // Again store the part of the string not changing for multiple uses
5252
ded882439548 file association for mac!
koda
parents: 5238
diff changeset
   127
            QString axis = prefix + HWApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   128
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   129
            // Entry for "Axis Up"
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   130
            sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   131
            sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2])) : axis + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   132
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   133
            // Entry for "Axis Down"
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   134
            sprintf(sdlkeys[i][0], "j%da%dd", jid, aid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   135
            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());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   136
        }
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   137
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   138
        // Register entries for all coolie hats of this joystick/gamepad
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   139
        for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   140
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   141
            // Again store the part of the string not changing for multiple uses
5252
ded882439548 file association for mac!
koda
parents: 5238
diff changeset
   142
            QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1));
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   143
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   144
            // Entry for "Hat Up"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   145
            sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   146
            sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   147
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   148
            // Entry for "Hat Down"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   149
            sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   150
            sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Down)")).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   151
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   152
            // Entry for "Hat Left"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   153
            sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   154
            sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Left)")).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   155
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   156
            // Entry for "Hat Right"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   157
            sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   158
            sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Right)")).toUtf8().constData());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   159
        }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   160
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   161
        // Register entries for all buttons of this joystick/gamepad
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   162
        for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   163
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   164
            // Buttons
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   165
            sprintf(sdlkeys[i][0], "j%db%d", jid, bid);
5747
65006a679352 Remove STL dependency
unc0rr
parents: 5252
diff changeset
   166
            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());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   167
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   168
        // Close the game controller as we no longer need it
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   169
        SDL_JoystickClose(joy);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   170
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   171
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   172
    // Terminate the list
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   173
    sdlkeys[i][0][0] = '\0';
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   174
    sdlkeys[i][1][0] = '\0';
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   175
}
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   176
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   177
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   178
void SDLInteraction::SDLAudioInit()
2515
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
   179
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   180
    // don't init again
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   181
    if (m_audioInitialized)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   182
        return;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   183
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   184
    SDL_Init(SDL_INIT_AUDIO);
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   185
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   186
    m_audioInitialized = true;
2515
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
   187
}
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
   188
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   189
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   190
void SDLInteraction::playSoundFile(const QString & soundFile)
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   191
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   192
    SDLAudioInit();
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
   193
    if (!m_soundMap->contains(soundFile))
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
   194
        m_soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   195
6585
6225b838a630 little hack for playing less sounds in frontend
koda
parents: 6524
diff changeset
   196
    //FIXME: this is a hack, but works as long as we have few concurrent playing sounds
6225b838a630 little hack for playing less sounds in frontend
koda
parents: 6524
diff changeset
   197
    if (Mix_Playing(lastchannel) == false)
6225b838a630 little hack for playing less sounds in frontend
koda
parents: 6524
diff changeset
   198
        lastchannel = Mix_PlayChannel(-1, m_soundMap->value(soundFile), 0);
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   199
}
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   200
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   201
void SDLInteraction::setMusicTrack(const QString & musicFile)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   202
{
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   203
    bool wasPlayingMusic = m_isPlayingMusic;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   204
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   205
    stopMusic();
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   206
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   207
    if (m_music != NULL)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   208
    {
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   209
        Mix_FreeMusic(m_music);
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   210
        m_music = NULL;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   211
    }
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   212
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   213
    m_musicTrack = musicFile;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   214
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   215
    if (wasPlayingMusic)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   216
        startMusic();
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   217
}
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   218
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   219
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   220
void SDLInteraction::startMusic()
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   221
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   222
    if (m_isPlayingMusic)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   223
        return;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   224
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   225
    m_isPlayingMusic = true;
2515
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
   226
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   227
    if (m_musicTrack.isEmpty())
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   228
        return;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   229
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   230
    SDLAudioInit();
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   231
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   232
    if (m_music == NULL)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   233
        m_music = Mix_LoadMUS(m_musicTrack.toLocal8Bit().constData());
6164
62aa418ed214 change SDL.h/SDL.cpp to use HWDataManager instead of poking around on the harddrive - also I added doc/comments to the class
sheepluva
parents: 5747
diff changeset
   234
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   235
    Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   236
    Mix_FadeInMusic(m_music, -1, 1750);
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   237
}
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   238
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   239
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   240
void SDLInteraction::stopMusic()
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   241
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   242
    if (m_isPlayingMusic && (m_music != NULL)) {
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   243
        // fade out music to finish 0,5 seconds from now
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   244
        while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   245
            SDL_Delay(100);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   246
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   247
    }
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   248
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   249
    m_isPlayingMusic = false;
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   250
}
2402
edd12b259e7c revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents: 2399
diff changeset
   251