author | unc0rr |
Mon, 12 Oct 2009 16:53:01 +0000 | |
changeset 2431 | 23242609c44b |
parent 2428 | 6800f8aa0184 |
child 2443 | fececcbc2189 |
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" |
|
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
22 |
#include "hwconsts.h" |
555 | 23 |
|
2428 | 24 |
#include <QApplication> |
25 |
||
26 |
extern char sdlkeys[1024][2][128]; |
|
27 |
extern char xb360buttons[][128]; |
|
28 |
extern char xb360dpad[128]; |
|
29 |
extern char xbox360axes[][128]; |
|
30 |
||
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
31 |
bool hardware; |
2418 | 32 |
extern char *programname; |
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 |
{ |
2191 | 36 |
music = -1; |
2428 | 37 |
hardware = false; |
38 |
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
|
39 |
||
40 |
||
41 |
if(SDL_NumJoysticks()) |
|
42 |
addGameControllerKeys(); |
|
43 |
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
|
555 | 44 |
} |
45 |
||
46 |
SDLInteraction::~SDLInteraction() |
|
47 |
{ |
|
1224 | 48 |
SDL_Quit(); |
2191 | 49 |
openal_close(); |
555 | 50 |
} |
51 |
||
2428 | 52 |
void SDLInteraction::setHardwareSound(bool hardware_snd) |
53 |
{ |
|
54 |
hardware = hardware_snd; |
|
55 |
} |
|
56 |
||
555 | 57 |
QStringList SDLInteraction::getResolutions() const |
58 |
{ |
|
59 |
QStringList result; |
|
60 |
||
61 |
SDL_Rect **modes; |
|
62 |
||
1191
5539aa59f703
With this change, SDL will probably report more screen resolutions on dual-monitor configurations
unc0rr
parents:
1066
diff
changeset
|
63 |
modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
555 | 64 |
|
65 |
if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
|
66 |
{ |
|
67 |
result << "640x480"; |
|
68 |
} else |
|
69 |
{ |
|
70 |
for(int i = 0; modes[i]; ++i) |
|
71 |
if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
|
72 |
result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); |
|
73 |
} |
|
74 |
||
75 |
return result; |
|
76 |
} |
|
2191 | 77 |
|
2428 | 78 |
void SDLInteraction::addGameControllerKeys() const |
79 |
{ |
|
80 |
QStringList result; |
|
81 |
||
82 |
int i = 0; |
|
83 |
while(i < 1024 && sdlkeys[i][1][0] != '\0') |
|
84 |
i++; |
|
85 |
||
86 |
// Iterate through all game controllers |
|
87 |
for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
|
88 |
{ |
|
89 |
SDL_Joystick* joy = SDL_JoystickOpen(jid); |
|
90 |
||
91 |
// Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only) |
|
92 |
QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
|
93 |
||
94 |
// Connected Xbox 360 controller? Use specific button names then |
|
95 |
// Might be interesting to add 'named' buttons for the most often used gamepads |
|
96 |
bool isxb = joyname.contains("Xbox 360"); |
|
97 |
||
98 |
// This part of the string won't change for multiple keys/hats, so keep it |
|
99 |
QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
|
100 |
||
101 |
// Register entries for missing axes not assigned to sticks of this joystick/gamepad |
|
102 |
for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++) |
|
103 |
{ |
|
104 |
// Axis 2 on xbox 360 is left/right trigger but those are used as buttons anyway so skip it |
|
105 |
if(aid == 2 && isxb) |
|
106 |
continue; |
|
107 |
||
108 |
// Again store the part of the string not changing for multiple uses |
|
109 |
QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1); |
|
110 |
||
111 |
// Entry for "Axis Up" |
|
112 |
sprintf(sdlkeys[i][0], "j%da%du", jid, aid); |
|
113 |
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()); |
|
114 |
||
115 |
// Entry for "Axis Down" |
|
116 |
sprintf(sdlkeys[i][0], "j%da%dd", jid, aid); |
|
117 |
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()); |
|
118 |
} |
|
119 |
||
120 |
// Register entries for all coolie hats of this joystick/gamepad |
|
121 |
for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
|
122 |
{ |
|
123 |
// Again store the part of the string not changing for multiple uses |
|
124 |
QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1)); |
|
125 |
||
126 |
// Entry for "Hat Up" |
|
127 |
sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
|
128 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str()); |
|
129 |
||
130 |
// Entry for "Hat Down" |
|
131 |
sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
|
132 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str()); |
|
133 |
||
134 |
// Entry for "Hat Left" |
|
135 |
sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
|
136 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str()); |
|
137 |
||
138 |
// Entry for "Hat Right" |
|
139 |
sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
|
140 |
sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str()); |
|
141 |
} |
|
142 |
||
143 |
// Register entries for all buttons of this joystick/gamepad |
|
144 |
for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
|
145 |
{ |
|
146 |
// Buttons |
|
147 |
sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
|
148 |
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()); |
|
149 |
} |
|
150 |
// Close the game controller as we no longer need it |
|
151 |
SDL_JoystickClose(joy); |
|
152 |
} |
|
153 |
||
154 |
// Terminate the list |
|
155 |
sdlkeys[i][0][0] = '\0'; |
|
156 |
sdlkeys[i][1][0] = '\0'; |
|
157 |
} |
|
158 |
||
2399
ddde0ac1472b
fix a bug that prevented from hearing team voicepacks if frontend started with music disabled
koda
parents:
2392
diff
changeset
|
159 |
void SDLInteraction::StartMusic() |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
160 |
{ |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
161 |
OpenAL_Init(); |
2191 | 162 |
if (music < 0) { |
163 |
music = openal_loadfile(QString(datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); |
|
164 |
openal_toggleloop(music); |
|
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2294
diff
changeset
|
165 |
|
2210 | 166 |
} |
167 |
openal_setvolume(music, 60); |
|
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2294
diff
changeset
|
168 |
openal_fadein(music, 30); |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
169 |
} |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
170 |
|
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
171 |
void SDLInteraction::StopMusic() |
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
172 |
{ |
2213 | 173 |
if (music >= 0) openal_fadeout(music, 40); |
1223
41d7283934c1
Hackish way to play music in frontend... to be fixed
unc0rr
parents:
1191
diff
changeset
|
174 |
} |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
175 |
|
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
176 |
//we need thjs wrapper because of some issues with windows drivers |
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
177 |
//beware that this cause a slight delay when playing the first sound |
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
178 |
void OpenAL_Init() |
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
179 |
{ |
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
180 |
if (!openal_ready()) |
2418 | 181 |
openal_init(programname, hardware ? 1 : 0, 5); |
2402
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
182 |
} |
edd12b259e7c
revert to manual startup of frontend's OpenAL session (keeping voices' bug fixed)
koda
parents:
2399
diff
changeset
|
183 |