author | koda |
Fri, 06 Dec 2013 00:28:55 +0100 | |
branch | sdl2transition |
changeset 9705 | 322fe1736347 |
parent 9701 | 7f6786625667 |
child 11338 | 7a7611adf715 |
permissions | -rw-r--r-- |
555 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
9080 | 3 |
* Copyright (c) 2004-2013 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 |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
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" |
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
|
28 |
#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
|
29 |
#include "gameuiconfig.h" |
2428 | 30 |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
31 |
#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
|
32 |
|
8117 | 33 |
#include "physfsrwops.h" |
34 |
||
2428 | 35 |
extern char sdlkeys[1024][2][128]; |
36 |
extern char xb360buttons[][128]; |
|
37 |
extern char xb360dpad[128]; |
|
38 |
extern char xbox360axes[][128]; |
|
39 |
||
6165
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 |
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
|
42 |
{ |
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
43 |
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
|
44 |
return instance; |
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
45 |
} |
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
46 |
|
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
47 |
|
2428 | 48 |
SDLInteraction::SDLInteraction() |
555 | 49 |
{ |
5085 | 50 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
51 |
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
3697 | 52 |
|
6166 | 53 |
m_audioInitialized = false; |
54 |
m_music = NULL; |
|
55 |
m_musicTrack = ""; |
|
56 |
m_isPlayingMusic = false; |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
57 |
if(SDL_NumJoysticks()) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
58 |
addGameControllerKeys(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
59 |
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
|
60 |
|
6167 | 61 |
m_soundMap = new QMap<QString,Mix_Chunk*>(); |
555 | 62 |
} |
63 |
||
6166 | 64 |
|
555 | 65 |
SDLInteraction::~SDLInteraction() |
66 |
{ |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
67 |
stopMusic(); |
6166 | 68 |
if (m_audioInitialized) |
69 |
{ |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
70 |
if (m_music != NULL) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
71 |
{ |
6524 | 72 |
Mix_HaltMusic(); |
6166 | 73 |
Mix_FreeMusic(m_music); |
6524 | 74 |
} |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
75 |
Mix_CloseAudio(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
76 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
77 |
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
|
78 |
|
6167 | 79 |
delete m_soundMap; |
555 | 80 |
} |
81 |
||
6166 | 82 |
|
555 | 83 |
QStringList SDLInteraction::getResolutions() const |
84 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
85 |
QStringList result; |
555 | 86 |
|
9701 | 87 |
#if SDL_VERSION_ATLEAST(2, 0, 0) |
88 |
int modesNumber = SDL_GetNumDisplayModes(0); |
|
89 |
SDL_DisplayMode mode; |
|
90 |
||
91 |
for(int i = 0; i < modesNumber; ++i) |
|
92 |
{ |
|
93 |
SDL_GetDisplayMode(0, i, &mode); |
|
94 |
||
95 |
if ((mode.w >= 640) && (mode.h >= 480)) |
|
96 |
result << QString("%1x%2").arg(mode.w).arg(mode.h); |
|
97 |
} |
|
98 |
#else |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
99 |
SDL_Rect **modes; |
555 | 100 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
101 |
modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
555 | 102 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
103 |
if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
104 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
105 |
result << "640x480"; |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
106 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
107 |
else |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
108 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
109 |
for(int i = 0; modes[i]; ++i) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
110 |
if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
111 |
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
|
112 |
} |
9701 | 113 |
#endif |
555 | 114 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
115 |
return result; |
555 | 116 |
} |
2191 | 117 |
|
6166 | 118 |
|
2428 | 119 |
void SDLInteraction::addGameControllerKeys() const |
120 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
121 |
QStringList result; |
2428 | 122 |
|
9701 | 123 |
#if SDL_VERSION_ATLEAST(2, 0, 0) |
124 |
||
125 |
#else |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
126 |
int i = 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
127 |
while(i < 1024 && sdlkeys[i][1][0] != '\0') |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
128 |
i++; |
2428 | 129 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
130 |
// Iterate through all game controllers |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
131 |
for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
132 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
133 |
SDL_Joystick* joy = SDL_JoystickOpen(jid); |
3697 | 134 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
135 |
// 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
|
136 |
QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
2428 | 137 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
138 |
// Connected Xbox 360 controller? Use specific button names then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
139 |
// 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
|
140 |
bool isxb = joyname.contains("Xbox 360"); |
2428 | 141 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
142 |
// 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
|
143 |
QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
2428 | 144 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
145 |
// 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
|
146 |
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
|
147 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
148 |
// Again store the part of the string not changing for multiple uses |
5252 | 149 |
QString axis = prefix + HWApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1); |
3697 | 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); |
5747 | 153 |
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 | 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); |
5747 | 157 |
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
|
158 |
} |
2428 | 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 |
5252 | 164 |
QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1)); |
2428 | 165 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
166 |
// Entry for "Hat Up" |
3697 | 167 |
sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
5747 | 168 |
sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData()); |
2428 | 169 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
170 |
// Entry for "Hat Down" |
3697 | 171 |
sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
5747 | 172 |
sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Down)")).toUtf8().constData()); |
2428 | 173 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
174 |
// Entry for "Hat Left" |
3697 | 175 |
sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
5747 | 176 |
sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Left)")).toUtf8().constData()); |
2428 | 177 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
178 |
// Entry for "Hat Right" |
3697 | 179 |
sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
5747 | 180 |
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
|
181 |
} |
3697 | 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 | 187 |
sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
5747 | 188 |
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
|
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 | 193 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
194 |
// Terminate the list |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
195 |
sdlkeys[i][0][0] = '\0'; |
9701 | 196 |
sdlkeys[i][1][0] = '\0'; |
197 |
#endif |
|
2428 | 198 |
} |
199 |
||
6166 | 200 |
|
201 |
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
|
202 |
{ |
6166 | 203 |
// don't init again |
204 |
if (m_audioInitialized) |
|
205 |
return; |
|
206 |
||
207 |
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
|
208 |
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
|
209 |
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
|
210 |
} |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
211 |
|
6166 | 212 |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
213 |
void SDLInteraction::playSoundFile(const QString & soundFile) |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
214 |
{ |
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
|
215 |
if (!HWForm::config || !HWForm::config->isFrontendSoundEnabled()) return; |
6166 | 216 |
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
|
217 |
if (!m_audioInitialized) return; |
6167 | 218 |
if (!m_soundMap->contains(soundFile)) |
8117 | 219 |
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
|
220 |
|
6585 | 221 |
//FIXME: this is a hack, but works as long as we have few concurrent playing sounds |
222 |
if (Mix_Playing(lastchannel) == false) |
|
223 |
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
|
224 |
} |
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
225 |
|
6166 | 226 |
void SDLInteraction::setMusicTrack(const QString & musicFile) |
227 |
{ |
|
228 |
bool wasPlayingMusic = m_isPlayingMusic; |
|
229 |
||
230 |
stopMusic(); |
|
231 |
||
232 |
if (m_music != NULL) |
|
233 |
{ |
|
234 |
Mix_FreeMusic(m_music); |
|
235 |
m_music = NULL; |
|
236 |
} |
|
237 |
||
238 |
m_musicTrack = musicFile; |
|
239 |
||
240 |
if (wasPlayingMusic) |
|
241 |
startMusic(); |
|
242 |
} |
|
243 |
||
244 |
||
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
245 |
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
|
246 |
{ |
6166 | 247 |
if (m_isPlayingMusic) |
248 |
return; |
|
249 |
||
250 |
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
|
251 |
|
6166 | 252 |
if (m_musicTrack.isEmpty()) |
253 |
return; |
|
3697 | 254 |
|
6166 | 255 |
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
|
256 |
if (!m_audioInitialized) return; |
6166 | 257 |
|
258 |
if (m_music == NULL) |
|
9701 | 259 |
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
|
260 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
261 |
Mix_VolumeMusic(MIX_MAX_VOLUME - 28); |
6166 | 262 |
Mix_FadeInMusic(m_music, -1, 1750); |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
263 |
} |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
264 |
|
6166 | 265 |
|
6165
6fe3e922246e
moving and renaming SDLs.*, making it a singleton; cleaning up class responsibilties wrt SDLInteraction; some comments
sheepluva
parents:
6164
diff
changeset
|
266 |
void SDLInteraction::stopMusic() |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
267 |
{ |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
268 |
if (m_isPlayingMusic && (m_music != NULL)) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6585
diff
changeset
|
269 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
270 |
// 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
|
271 |
while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) |
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 |
SDL_Delay(100); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
274 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2531
diff
changeset
|
275 |
} |
6166 | 276 |
|
277 |
m_isPlayingMusic = false; |
|
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
278 |
} |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
279 |
|
9701 | 280 |
|
281 |
QSize SDLInteraction::getCurrentResolution() |
|
282 |
{ |
|
283 |
#if SDL_VERSION_ATLEAST(2, 0, 0) |
|
284 |
SDL_DisplayMode mode; |
|
285 |
||
286 |
SDL_GetDesktopDisplayMode(0, &mode); |
|
287 |
||
288 |
return QSize(mode.w, mode.h); |
|
289 |
#else |
|
290 |
SDL_VideoInfo * vi = SDL_GetVideoInfo(); |
|
291 |
return QSize(vi->current_w, vi->current_h); |
|
292 |
#endif |
|
293 |
} |