QTfrontend/util/SDLInteraction.cpp
author unC0Rr
Thu, 10 Sep 2020 09:30:41 +0200
changeset 15739 94f4d0a96143
parent 14867 8bba7492558d
permissions -rw-r--r--
Prevent AI from getting stuck in attempt to look in another direction after choosing bee weapon
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
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10939
diff changeset
     3
 * Copyright (c) 2004-2015 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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
555
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"
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
    28
#include "sdlkeys.h"
8326
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
    29
#include "hwform.h" /* you know, we could just put a config singleton lookup function in gameuiconfig or something... */
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
    30
#include "gameuiconfig.h"
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    31
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    32
#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
    33
8117
329d9756b57c Load sounds with the help of physfsrwops library
unc0rr
parents: 6952
diff changeset
    34
#include "physfsrwops.h"
329d9756b57c Load sounds with the help of physfsrwops library
unc0rr
parents: 6952
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
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
    37
{
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    38
    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
    39
    return instance;
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    40
}
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
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
    43
SDLInteraction::SDLInteraction()
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    44
{
5085
e8944ed92b61 Reverting..
Zorg <zorgiepoo@gmail.com>
parents: 5083
diff changeset
    45
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    46
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
    47
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    48
    m_audioInitialized = false;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    49
    m_music = NULL;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    50
    m_musicTrack = "";
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    51
    m_isPlayingMusic = false;
10248
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10108
diff changeset
    52
    lastchannel = 0;
14867
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    53
    int i;
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    54
    // Initialize sdlkeys_iskeyboard
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    55
    for (i=0; i<1024; i++) {
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    56
        // First 7 entries are mouse buttons (see sdlkeys.cpp)
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    57
        if ((i > 6) && (sdlkeys[i][0][0] != '\0'))
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    58
            sdlkeys_iskeyboard[i] = true;
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    59
        else
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    60
            sdlkeys_iskeyboard[i] = false;
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    61
    }
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    62
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    63
    if(SDL_NumJoysticks())
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    64
        addGameControllerKeys();
14532
e1ae6d8e84b0 Allow to leave a control unused (no key binding)
Wuzzy <Wuzzy2@mail.ru>
parents: 13515
diff changeset
    65
14867
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    66
    // Add special "none" key at the end of list
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    67
    i = 0;
14532
e1ae6d8e84b0 Allow to leave a control unused (no key binding)
Wuzzy <Wuzzy2@mail.ru>
parents: 13515
diff changeset
    68
    while(i < 1024 && sdlkeys[i][1][0] != '\0')
e1ae6d8e84b0 Allow to leave a control unused (no key binding)
Wuzzy <Wuzzy2@mail.ru>
parents: 13515
diff changeset
    69
        i++;
e1ae6d8e84b0 Allow to leave a control unused (no key binding)
Wuzzy <Wuzzy2@mail.ru>
parents: 13515
diff changeset
    70
    sprintf(sdlkeys[i][0], "none");
14867
8bba7492558d Frontend: Refactor detection of keyboard keys in sdlkeys[]
Wuzzy <Wuzzy2@mail.ru>
parents: 14532
diff changeset
    71
    sprintf(sdlkeys[i][1], "%s", HWApplication::translate("binds (keys)", unboundcontrol).toUtf8().constData());
14532
e1ae6d8e84b0 Allow to leave a control unused (no key binding)
Wuzzy <Wuzzy2@mail.ru>
parents: 13515
diff changeset
    72
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    73
    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
    74
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
    75
    m_soundMap = new QMap<QString,Mix_Chunk*>();
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    76
}
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    77
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    78
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    79
SDLInteraction::~SDLInteraction()
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    80
{
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
    81
    stopMusic();
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    82
    if (m_audioInitialized)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    83
    {
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
    84
        if (m_music != NULL)
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
    85
        {
6524
416fdfb666da make sure to stop music before freeing it
koda
parents: 6170
diff changeset
    86
            Mix_HaltMusic();
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    87
            Mix_FreeMusic(m_music);
6524
416fdfb666da make sure to stop music before freeing it
koda
parents: 6170
diff changeset
    88
        }
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    89
        Mix_CloseAudio();
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
    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
    92
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
    93
    delete m_soundMap;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    94
}
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    95
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
    96
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    97
QStringList SDLInteraction::getResolutions() const
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
    98
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
    99
    QStringList result;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
   100
9701
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   101
    int modesNumber = SDL_GetNumDisplayModes(0);
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   102
    SDL_DisplayMode mode;
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   103
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   104
    for(int i = 0; i < modesNumber; ++i)
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   105
    {
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   106
        SDL_GetDisplayMode(0, i, &mode);
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   107
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   108
        if ((mode.w >= 640) && (mode.h >= 480))
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   109
            result << QString("%1x%2").arg(mode.w).arg(mode.h);
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   110
    }
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
   111
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   112
    return result;
555
0376fdcc6322 Get possible screen resolutions from SDL library
unc0rr
parents:
diff changeset
   113
}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents: 2050
diff changeset
   114
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   115
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   116
void SDLInteraction::addGameControllerKeys() const
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   117
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   118
    QStringList result;
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   119
9701
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   120
#if SDL_VERSION_ATLEAST(2, 0, 0)
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   121
    int i = 0;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   122
    while(i < 1024 && sdlkeys[i][1][0] != '\0')
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   123
        i++;
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   124
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   125
    // Iterate through all game controllers
13514
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   126
    qDebug("Detecting controllers ...");
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   127
    for(int jid = 0; jid < SDL_NumJoysticks(); jid++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   128
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   129
        SDL_Joystick* joy = SDL_JoystickOpen(jid);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   130
13514
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   131
        // Retrieve the game controller's name
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   132
        QString joyname = QString(SDL_JoystickNameForIndex(jid));
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   133
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   134
        // Strip "Controller (...)" that's added by some drivers (English only)
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   135
        joyname.replace(QRegExp("^Controller \\((.*)\\)$"), "\\1");
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   136
31d0b0911cba Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents: 11362
diff changeset
   137
        qDebug("- Controller no. %d: %s", jid, qPrintable(joyname));
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   138
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   139
        // Connected Xbox 360 controller? Use specific button names then
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   140
        // 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
   141
        bool isxb = joyname.contains("Xbox 360");
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   142
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   143
        // 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
   144
        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
   145
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   146
        // 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
   147
        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
   148
        {
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   149
            QString axis = prefix + HWApplication::translate("binds (keys)", controlleraxis).arg(aid + 1);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   150
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   151
            // Entry for "Axis Up"
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   152
            sprintf(sdlkeys[i][0], "j%da%du", jid, aid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   153
            sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2])) : (axis.arg(HWApplication::translate("binds (keys)", controllerup)))).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   154
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   155
            // Entry for "Axis Down"
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   156
            sprintf(sdlkeys[i][0], "j%da%dd", jid, aid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   157
            sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2 + 1])) : (axis.arg(HWApplication::translate("binds (keys)", controllerdown)))).toUtf8().constData());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   158
        }
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   159
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   160
        // 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
   161
        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
   162
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   163
            // Again store the part of the string not changing for multiple uses
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   164
            QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", controllerhat).arg(hid + 1));
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   165
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   166
            // Entry for "Hat Up"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   167
            sprintf(sdlkeys[i][0], "j%dh%du", jid, hid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   168
            sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerup)).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   169
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   170
            // Entry for "Hat Down"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   171
            sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   172
            sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerdown)).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   173
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   174
            // Entry for "Hat Left"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   175
            sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   176
            sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerleft)).toUtf8().constData());
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   177
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   178
            // Entry for "Hat Right"
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   179
            sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   180
            sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerright)).toUtf8().constData());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   181
        }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   182
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   183
        // Register entries for all buttons of this joystick/gamepad
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   184
        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
   185
        {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   186
            // Buttons
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   187
            sprintf(sdlkeys[i][0], "j%db%d", jid, bid);
13515
c5365f05d4de Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents: 13514
diff changeset
   188
            sprintf(sdlkeys[i++][1], "%s", (prefix + ((isxb && bid < 10) ? (HWApplication::translate("binds (keys)", xb360buttons[bid]) + QString(" ")) : HWApplication::translate("binds (keys)", controllerbutton).arg(bid + 1))).toUtf8().constData());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   189
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   190
        // 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
   191
        SDL_JoystickClose(joy);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   192
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   193
10248
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10108
diff changeset
   194
    if(i >= 1024)
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10108
diff changeset
   195
        i = 1023;
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10108
diff changeset
   196
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   197
    // Terminate the list
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   198
    sdlkeys[i][0][0] = '\0';
9701
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   199
    sdlkeys[i][1][0] = '\0';   
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   200
#endif
2428
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   201
}
6800f8aa0184 Huge Smaxx patch with some fixes by me:
unc0rr
parents: 2418
diff changeset
   202
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   203
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   204
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
   205
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   206
    // don't init again
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   207
    if (m_audioInitialized)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   208
        return;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   209
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   210
    SDL_Init(SDL_INIT_AUDIO);
8326
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
   211
    if(!Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)) /* should we keep trying, or just turn off permanently? */
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
   212
        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
   213
}
51d3f4b6293a revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents: 2494
diff changeset
   214
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   215
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   216
void SDLInteraction::playSoundFile(const QString & soundFile)
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   217
{
8326
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
   218
    if (!HWForm::config || !HWForm::config->isFrontendSoundEnabled()) return;
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   219
    SDLAudioInit();
8326
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
   220
    if (!m_audioInitialized) return;
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6166
diff changeset
   221
    if (!m_soundMap->contains(soundFile))
8117
329d9756b57c Load sounds with the help of physfsrwops library
unc0rr
parents: 6952
diff changeset
   222
        m_soundMap->insert(soundFile, Mix_LoadWAV_RW(PHYSFSRWOPS_openRead(soundFile.toLocal8Bit().constData()), 1));
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   223
6585
6225b838a630 little hack for playing less sounds in frontend
koda
parents: 6524
diff changeset
   224
    //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
   225
    if (Mix_Playing(lastchannel) == false)
6225b838a630 little hack for playing less sounds in frontend
koda
parents: 6524
diff changeset
   226
        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
   227
}
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   228
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   229
void SDLInteraction::setMusicTrack(const QString & musicFile)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   230
{
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   231
    bool wasPlayingMusic = m_isPlayingMusic;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   232
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   233
    stopMusic();
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   234
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   235
    if (m_music != NULL)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   236
    {
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   237
        Mix_FreeMusic(m_music);
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   238
        m_music = NULL;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   239
    }
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   240
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   241
    m_musicTrack = musicFile;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   242
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   243
    if (wasPlayingMusic)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   244
        startMusic();
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   245
}
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   246
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   247
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   248
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
   249
{
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   250
    if (m_isPlayingMusic)
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   251
        return;
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   252
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   253
    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
   254
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   255
    if (m_musicTrack.isEmpty())
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   256
        return;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3236
diff changeset
   257
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   258
    SDLAudioInit();
8326
023a71940f26 Don't play sounds in chat if sound is disabled, try to prevent sound crashes by checking for audio init failure.
nemo
parents: 8117
diff changeset
   259
    if (!m_audioInitialized) return;
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   260
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   261
    if (m_music == NULL)
9701
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   262
        m_music = Mix_LoadMUS_RW(PHYSFSRWOPS_openRead(m_musicTrack.toLocal8Bit().constData()), 0);
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
   263
10939
55ed72ad3a57 reduce volume a bit more. also fix engine hint
sheepluva
parents: 10938
diff changeset
   264
    Mix_VolumeMusic(MIX_MAX_VOLUME/4);
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   265
    Mix_FadeInMusic(m_music, -1, 1750);
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   266
}
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   267
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   268
6165
6fe3e922246e moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents: 6164
diff changeset
   269
void SDLInteraction::stopMusic()
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   270
{
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
   271
    if (m_isPlayingMusic && (m_music != NULL))
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
   272
    {
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   273
        // fade out music to finish 0,5 seconds from now
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
   274
        while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic())
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6585
diff changeset
   275
        {
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   276
            SDL_Delay(100);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   277
        }
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2531
diff changeset
   278
    }
6166
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   279
701c5b8fac56 finishing SDLInterface cleanup (wrt audio)
sheepluva
parents: 6165
diff changeset
   280
    m_isPlayingMusic = false;
1223
41d7283934c1 Hackish way to play music in frontend... to be fixed
unc0rr
parents: 1191
diff changeset
   281
}
2402
edd12b259e7c revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents: 2399
diff changeset
   282
9701
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   283
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   284
QSize SDLInteraction::getCurrentResolution()
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   285
{
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   286
    SDL_DisplayMode mode;
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   287
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   288
    SDL_GetDesktopDisplayMode(0, &mode);
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   289
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   290
    return QSize(mode.w, mode.h);
7f6786625667 Frontend builds
unc0rr
parents: 9080
diff changeset
   291
}