author | nemo |
Sat, 17 Oct 2009 23:03:31 +0000 | |
changeset 2532 | 43d700d8dad0 |
parent 2531 | c7f841eb91fb |
child 2948 | 3f21a9dc93d0 |
permissions | -rw-r--r-- |
555 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
555 | 3 |
* Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include "SDLs.h" |
|
20 |
||
21 |
#include "SDL.h" |
|
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
22 |
#include "SDL_mixer.h" |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
23 |
#include "hwconsts.h" |
555 | 24 |
|
2428 | 25 |
#include <QApplication> |
26 |
||
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
27 |
|
2428 | 28 |
extern char sdlkeys[1024][2][128]; |
29 |
extern char xb360buttons[][128]; |
|
30 |
extern char xb360dpad[128]; |
|
31 |
extern char xbox360axes[][128]; |
|
32 |
||
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
33 |
|
2428 | 34 |
SDLInteraction::SDLInteraction() |
555 | 35 |
{ |
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
36 |
|
2428 | 37 |
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
38 |
|
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
39 |
musicInitialized = 0; |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
40 |
music = NULL; |
2428 | 41 |
if(SDL_NumJoysticks()) |
42 |
addGameControllerKeys(); |
|
43 |
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
|
555 | 44 |
} |
45 |
||
46 |
SDLInteraction::~SDLInteraction() |
|
47 |
{ |
|
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
48 |
if (musicInitialized == 1) { |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
49 |
if (music != NULL) |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
50 |
Mix_FreeMusic(music); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
51 |
Mix_CloseAudio(); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
52 |
} |
1224 | 53 |
SDL_Quit(); |
555 | 54 |
} |
55 |
||
56 |
QStringList SDLInteraction::getResolutions() const |
|
57 |
{ |
|
58 |
QStringList result; |
|
59 |
||
60 |
SDL_Rect **modes; |
|
61 |
||
1191
5539aa59f703
With this change, SDL will probably report more screen resolutions on dual-monitor configurations
unc0rr
parents:
1066
diff
changeset
|
62 |
modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
555 | 63 |
|
64 |
if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
|
65 |
{ |
|
66 |
result << "640x480"; |
|
67 |
} else |
|
68 |
{ |
|
69 |
for(int i = 0; modes[i]; ++i) |
|
2531
c7f841eb91fb
Remove option windows for hardware sound, unneeded w/o openal, update german locale, remove a restriction on modelist
nemo
parents:
2520
diff
changeset
|
70 |
if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
555 | 71 |
result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); |
72 |
} |
|
73 |
||
74 |
return result; |
|
75 |
} |
|
2191 | 76 |
|
2428 | 77 |
void SDLInteraction::addGameControllerKeys() const |
78 |
{ |
|
79 |
QStringList result; |
|
80 |
||
81 |
int i = 0; |
|
82 |
while(i < 1024 && sdlkeys[i][1][0] != '\0') |
|
83 |
i++; |
|
84 |
||
85 |
// Iterate through all game controllers |
|
86 |
for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
|
87 |
{ |
|
88 |
SDL_Joystick* joy = SDL_JoystickOpen(jid); |
|
89 |
||
90 |
// Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only) |
|
91 |
QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
|
92 |
||
93 |
// Connected Xbox 360 controller? Use specific button names then |
|
94 |
// Might be interesting to add 'named' buttons for the most often used gamepads |
|
95 |
bool isxb = joyname.contains("Xbox 360"); |
|
96 |
||
97 |
// This part of the string won't change for multiple keys/hats, so keep it |
|
98 |
QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
|
99 |
||
100 |
// Register entries for missing axes not assigned to sticks of this joystick/gamepad |
|
101 |
for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++) |
|
102 |
{ |
|
103 |
// Again store the part of the string not changing for multiple uses |
|
104 |
QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1); |
|
105 |
||
106 |
// Entry for "Axis Up" |
|
107 |
sprintf(sdlkeys[i][0], "j%da%du", jid, aid); |
|
108 |
sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + QApplication::translate("binds (keys)", xbox360axes[aid * 2])) : axis + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str()); |
|
109 |
||
110 |
// Entry for "Axis Down" |
|
111 |
sprintf(sdlkeys[i][0], "j%da%dd", jid, aid); |
|
112 |
sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + QApplication::translate("binds (keys)", xbox360axes[aid * 2 + 1])) : axis + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str()); |
|
113 |
} |
|
114 |
||
115 |
// Register entries for all coolie hats of this joystick/gamepad |
|
116 |
for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
|
117 |
{ |
|
118 |
// Again store the part of the string not changing for multiple uses |
|
119 |
QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1)); |
|
120 |
||
121 |
// Entry for "Hat Up" |
|
122 |
sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
|
123 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str()); |
|
124 |
||
125 |
// Entry for "Hat Down" |
|
126 |
sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
|
127 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str()); |
|
128 |
||
129 |
// Entry for "Hat Left" |
|
130 |
sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
|
131 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str()); |
|
132 |
||
133 |
// Entry for "Hat Right" |
|
134 |
sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
|
135 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str()); |
|
136 |
} |
|
137 |
||
138 |
// Register entries for all buttons of this joystick/gamepad |
|
139 |
for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
|
140 |
{ |
|
141 |
// Buttons |
|
142 |
sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
|
143 |
sprintf(sdlkeys[i++][1], "%s", (prefix + ((isxb && bid < 10) ? (QApplication::translate("binds (keys)", xb360buttons[bid]) + QString(" ")) : QApplication::translate("binds (keys)", "Button") + QString(" %1").arg(bid + 1))).toStdString().c_str()); |
|
144 |
} |
|
145 |
// Close the game controller as we no longer need it |
|
146 |
SDL_JoystickClose(joy); |
|
147 |
} |
|
148 |
||
149 |
// Terminate the list |
|
150 |
sdlkeys[i][0][0] = '\0'; |
|
151 |
sdlkeys[i][1][0] = '\0'; |
|
152 |
} |
|
153 |
||
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
154 |
void SDLInteraction::SDLMusicInit() |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
155 |
{ |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
156 |
if (musicInitialized == 0) { |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
157 |
SDL_Init(SDL_INIT_AUDIO); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
158 |
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
159 |
musicInitialized = 1; |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
160 |
} |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
161 |
} |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
162 |
|
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
163 |
|
2399
ddde0ac1472b
fix a bug that prevented from hearing team voicepacks if frontend started with music disabled
koda
parents:
2392
diff
changeset
|
164 |
void SDLInteraction::StartMusic() |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
165 |
{ |
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
166 |
SDLMusicInit(); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
167 |
|
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
168 |
if (music == NULL) { |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
169 |
music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); |
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2294
diff
changeset
|
170 |
|
2210 | 171 |
} |
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
172 |
Mix_VolumeMusic(MIX_MAX_VOLUME - 28); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
173 |
Mix_FadeInMusic(music, -1, 1750); |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
174 |
} |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
175 |
|
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
176 |
void SDLInteraction::StopMusic() |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
177 |
{ |
2515
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
178 |
if (music != NULL) { |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
179 |
// fade out music to finish 0,5 seconds from now |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
180 |
while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) { |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
181 |
SDL_Delay(100); |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
182 |
} |
51d3f4b6293a
revert audio to use SDL_mixer -- also frontend uses it, so it needs sdlmixer sources
koda
parents:
2494
diff
changeset
|
183 |
} |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
184 |
} |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
185 |