author | nemo |
Sun, 29 Jul 2018 11:37:14 -0400 | |
changeset 13570 | 5190d41a2440 |
parent 13515 | c5365f05d4de |
child 14532 | e1ae6d8e84b0 |
permissions | -rw-r--r-- |
555 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
555 | 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 |
|
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 | 17 |
*/ |
18 |
||
6168 | 19 |
/** |
6170 | 20 |
* @file |
6168 | 21 |
* @brief SDLInteraction class implementation |
22 |
*/ |
|
555 | 23 |
|
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 | 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 | 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 | 34 |
#include "physfsrwops.h" |
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 | 43 |
SDLInteraction::SDLInteraction() |
555 | 44 |
{ |
5085 | 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 | 47 |
|
6166 | 48 |
m_audioInitialized = false; |
49 |
m_music = NULL; |
|
50 |
m_musicTrack = ""; |
|
51 |
m_isPlayingMusic = false; |
|
10248 | 52 |
lastchannel = 0; |
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 | 57 |
m_soundMap = new QMap<QString,Mix_Chunk*>(); |
555 | 58 |
} |
59 |
||
6166 | 60 |
|
555 | 61 |
SDLInteraction::~SDLInteraction() |
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 | 64 |
if (m_audioInitialized) |
65 |
{ |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
66 |
if (m_music != NULL) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
67 |
{ |
6524 | 68 |
Mix_HaltMusic(); |
6166 | 69 |
Mix_FreeMusic(m_music); |
6524 | 70 |
} |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
71 |
Mix_CloseAudio(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
72 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
73 |
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
|
74 |
|
6167 | 75 |
delete m_soundMap; |
555 | 76 |
} |
77 |
||
6166 | 78 |
|
555 | 79 |
QStringList SDLInteraction::getResolutions() const |
80 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
81 |
QStringList result; |
555 | 82 |
|
9701 | 83 |
int modesNumber = SDL_GetNumDisplayModes(0); |
84 |
SDL_DisplayMode mode; |
|
85 |
||
86 |
for(int i = 0; i < modesNumber; ++i) |
|
87 |
{ |
|
88 |
SDL_GetDisplayMode(0, i, &mode); |
|
89 |
||
90 |
if ((mode.w >= 640) && (mode.h >= 480)) |
|
91 |
result << QString("%1x%2").arg(mode.w).arg(mode.h); |
|
92 |
} |
|
555 | 93 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
94 |
return result; |
555 | 95 |
} |
2191 | 96 |
|
6166 | 97 |
|
2428 | 98 |
void SDLInteraction::addGameControllerKeys() const |
99 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
100 |
QStringList result; |
2428 | 101 |
|
9701 | 102 |
#if SDL_VERSION_ATLEAST(2, 0, 0) |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
103 |
int i = 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
104 |
while(i < 1024 && sdlkeys[i][1][0] != '\0') |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
105 |
i++; |
2428 | 106 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
107 |
// Iterate through all game controllers |
13514
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
108 |
qDebug("Detecting controllers ..."); |
2948
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 | 112 |
|
13514
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
113 |
// Retrieve the game controller's name |
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
114 |
QString joyname = QString(SDL_JoystickNameForIndex(jid)); |
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
115 |
|
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
116 |
// Strip "Controller (...)" that's added by some drivers (English only) |
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
117 |
joyname.replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
118 |
|
31d0b0911cba
Make frontend detect controllers again
Wuzzy <Wuzzy2@mail.ru>
parents:
11362
diff
changeset
|
119 |
qDebug("- Controller no. %d: %s", jid, qPrintable(joyname)); |
2428 | 120 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
121 |
// Connected Xbox 360 controller? Use specific button names then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
122 |
// 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
|
123 |
bool isxb = joyname.contains("Xbox 360"); |
2428 | 124 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
125 |
// 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
|
126 |
QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
2428 | 127 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
128 |
// 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
|
129 |
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
|
130 |
{ |
13515
c5365f05d4de
Refactor storage of joystick SDL key names
Wuzzy <Wuzzy2@mail.ru>
parents:
13514
diff
changeset
|
131 |
QString axis = prefix + HWApplication::translate("binds (keys)", controlleraxis).arg(aid + 1); |
3697 | 132 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
133 |
// Entry for "Axis Up" |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
134 |
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
|
135 |
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 | 136 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
137 |
// Entry for "Axis Down" |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
} |
2428 | 141 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
142 |
// 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
|
143 |
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
|
144 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
145 |
// 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
|
146 |
QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", controllerhat).arg(hid + 1)); |
2428 | 147 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
148 |
// Entry for "Hat Up" |
3697 | 149 |
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
|
150 |
sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerup)).toUtf8().constData()); |
2428 | 151 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
152 |
// Entry for "Hat Down" |
3697 | 153 |
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
|
154 |
sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerdown)).toUtf8().constData()); |
2428 | 155 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
156 |
// Entry for "Hat Left" |
3697 | 157 |
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
|
158 |
sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerleft)).toUtf8().constData()); |
2428 | 159 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
160 |
// Entry for "Hat Right" |
3697 | 161 |
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
|
162 |
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
|
163 |
} |
3697 | 164 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
165 |
// Register entries for all buttons of this joystick/gamepad |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
166 |
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
|
167 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
168 |
// Buttons |
3697 | 169 |
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
|
170 |
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
|
171 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
172 |
// 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
|
173 |
SDL_JoystickClose(joy); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
174 |
} |
3697 | 175 |
|
10248 | 176 |
if(i >= 1024) |
177 |
i = 1023; |
|
178 |
||
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
179 |
// Terminate the list |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
180 |
sdlkeys[i][0][0] = '\0'; |
9701 | 181 |
sdlkeys[i][1][0] = '\0'; |
182 |
#endif |
|
2428 | 183 |
} |
184 |
||
6166 | 185 |
|
186 |
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
|
187 |
{ |
6166 | 188 |
// don't init again |
189 |
if (m_audioInitialized) |
|
190 |
return; |
|
191 |
||
192 |
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
|
193 |
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
|
194 |
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
|
195 |
} |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
196 |
|
6166 | 197 |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
198 |
void SDLInteraction::playSoundFile(const QString & soundFile) |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
199 |
{ |
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
|
200 |
if (!HWForm::config || !HWForm::config->isFrontendSoundEnabled()) return; |
6166 | 201 |
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
|
202 |
if (!m_audioInitialized) return; |
6167 | 203 |
if (!m_soundMap->contains(soundFile)) |
8117 | 204 |
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
|
205 |
|
6585 | 206 |
//FIXME: this is a hack, but works as long as we have few concurrent playing sounds |
207 |
if (Mix_Playing(lastchannel) == false) |
|
208 |
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
|
209 |
} |
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
210 |
|
6166 | 211 |
void SDLInteraction::setMusicTrack(const QString & musicFile) |
212 |
{ |
|
213 |
bool wasPlayingMusic = m_isPlayingMusic; |
|
214 |
||
215 |
stopMusic(); |
|
216 |
||
217 |
if (m_music != NULL) |
|
218 |
{ |
|
219 |
Mix_FreeMusic(m_music); |
|
220 |
m_music = NULL; |
|
221 |
} |
|
222 |
||
223 |
m_musicTrack = musicFile; |
|
224 |
||
225 |
if (wasPlayingMusic) |
|
226 |
startMusic(); |
|
227 |
} |
|
228 |
||
229 |
||
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
230 |
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
|
231 |
{ |
6166 | 232 |
if (m_isPlayingMusic) |
233 |
return; |
|
234 |
||
235 |
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
|
236 |
|
6166 | 237 |
if (m_musicTrack.isEmpty()) |
238 |
return; |
|
3697 | 239 |
|
6166 | 240 |
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
|
241 |
if (!m_audioInitialized) return; |
6166 | 242 |
|
243 |
if (m_music == NULL) |
|
9701 | 244 |
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
|
245 |
|
10939 | 246 |
Mix_VolumeMusic(MIX_MAX_VOLUME/4); |
6166 | 247 |
Mix_FadeInMusic(m_music, -1, 1750); |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
248 |
} |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
249 |
|
6166 | 250 |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
251 |
void SDLInteraction::stopMusic() |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
252 |
{ |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
253 |
if (m_isPlayingMusic && (m_music != NULL)) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
254 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
255 |
// 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
|
256 |
while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
257 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
258 |
SDL_Delay(100); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
259 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
260 |
} |
6166 | 261 |
|
262 |
m_isPlayingMusic = false; |
|
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
263 |
} |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
264 |
|
9701 | 265 |
|
266 |
QSize SDLInteraction::getCurrentResolution() |
|
267 |
{ |
|
268 |
SDL_DisplayMode mode; |
|
269 |
||
270 |
SDL_GetDesktopDisplayMode(0, &mode); |
|
271 |
||
272 |
return QSize(mode.w, mode.h); |
|
273 |
} |