78 |
96 |
79 QStringList SDLInteraction::getResolutions() const |
97 QStringList SDLInteraction::getResolutions() const |
80 { |
98 { |
81 QStringList result; |
99 QStringList result; |
82 |
100 |
83 SDL_Rect **modes; |
101 int modesNumber = SDL_GetNumDisplayModes(0); |
84 |
102 SDL_DisplayMode mode; |
85 modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
103 |
86 |
104 for(int i = 0; i < modesNumber; ++i) |
87 if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
105 { |
88 { |
106 SDL_GetDisplayMode(0, i, &mode); |
89 result << "640x480"; |
107 |
90 } |
108 if ((mode.w >= 640) && (mode.h >= 480)) |
91 else |
109 result << QString("%1x%2").arg(mode.w).arg(mode.h); |
92 { |
|
93 for(int i = 0; modes[i]; ++i) |
|
94 if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
|
95 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); |
|
96 } |
110 } |
97 |
111 |
98 return result; |
112 return result; |
99 } |
113 } |
100 |
114 |
101 |
115 |
102 void SDLInteraction::addGameControllerKeys() const |
116 void SDLInteraction::addGameControllerKeys() const |
103 { |
117 { |
104 QStringList result; |
118 QStringList result; |
105 |
119 |
|
120 #if SDL_VERSION_ATLEAST(2, 0, 0) |
106 int i = 0; |
121 int i = 0; |
107 while(i < 1024 && sdlkeys[i][1][0] != '\0') |
122 while(i < 1024 && sdlkeys[i][1][0] != '\0') |
108 i++; |
123 i++; |
109 |
124 |
110 // Iterate through all game controllers |
125 // Iterate through all game controllers |
|
126 qDebug("Detecting controllers ..."); |
111 for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
127 for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
112 { |
128 { |
113 SDL_Joystick* joy = SDL_JoystickOpen(jid); |
129 SDL_Joystick* joy = SDL_JoystickOpen(jid); |
114 |
130 |
115 // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only) |
131 // Retrieve the game controller's name |
116 QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
132 QString joyname = QString(SDL_JoystickNameForIndex(jid)); |
|
133 |
|
134 // Strip "Controller (...)" that's added by some drivers (English only) |
|
135 joyname.replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
|
136 |
|
137 qDebug("- Controller no. %d: %s", jid, qPrintable(joyname)); |
117 |
138 |
118 // Connected Xbox 360 controller? Use specific button names then |
139 // Connected Xbox 360 controller? Use specific button names then |
119 // Might be interesting to add 'named' buttons for the most often used gamepads |
140 // Might be interesting to add 'named' buttons for the most often used gamepads |
120 bool isxb = joyname.contains("Xbox 360"); |
141 bool isxb = joyname.contains("Xbox 360"); |
121 |
142 |
123 QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
144 QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
124 |
145 |
125 // Register entries for missing axes not assigned to sticks of this joystick/gamepad |
146 // Register entries for missing axes not assigned to sticks of this joystick/gamepad |
126 for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++) |
147 for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++) |
127 { |
148 { |
128 // Again store the part of the string not changing for multiple uses |
149 QString axis = prefix + HWApplication::translate("binds (keys)", controlleraxis).arg(aid + 1); |
129 QString axis = prefix + HWApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1); |
|
130 |
150 |
131 // Entry for "Axis Up" |
151 // Entry for "Axis Up" |
132 sprintf(sdlkeys[i][0], "j%da%du", jid, aid); |
152 sprintf(sdlkeys[i][0], "j%da%du", jid, aid); |
133 sprintf(sdlkeys[i++][1], "%s", ((isxb && aid < 5) ? (prefix + HWApplication::translate("binds (keys)", xbox360axes[aid * 2])) : axis + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData()); |
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()); |
134 |
154 |
135 // Entry for "Axis Down" |
155 // Entry for "Axis Down" |
136 sprintf(sdlkeys[i][0], "j%da%dd", jid, aid); |
156 sprintf(sdlkeys[i][0], "j%da%dd", jid, aid); |
137 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()); |
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()); |
138 } |
158 } |
139 |
159 |
140 // Register entries for all coolie hats of this joystick/gamepad |
160 // Register entries for all coolie hats of this joystick/gamepad |
141 for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
161 for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
142 { |
162 { |
143 // Again store the part of the string not changing for multiple uses |
163 // Again store the part of the string not changing for multiple uses |
144 QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1)); |
164 QString hat = prefix + (isxb ? (HWApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : HWApplication::translate("binds (keys)", controllerhat).arg(hid + 1)); |
145 |
165 |
146 // Entry for "Hat Up" |
166 // Entry for "Hat Up" |
147 sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
167 sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
148 sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Up)")).toUtf8().constData()); |
168 sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerup)).toUtf8().constData()); |
149 |
169 |
150 // Entry for "Hat Down" |
170 // Entry for "Hat Down" |
151 sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
171 sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
152 sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Down)")).toUtf8().constData()); |
172 sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerdown)).toUtf8().constData()); |
153 |
173 |
154 // Entry for "Hat Left" |
174 // Entry for "Hat Left" |
155 sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
175 sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
156 sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Left)")).toUtf8().constData()); |
176 sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerleft)).toUtf8().constData()); |
157 |
177 |
158 // Entry for "Hat Right" |
178 // Entry for "Hat Right" |
159 sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
179 sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
160 sprintf(sdlkeys[i++][1], "%s", (hat + HWApplication::translate("binds (keys)", "(Right)")).toUtf8().constData()); |
180 sprintf(sdlkeys[i++][1], "%s", hat.arg(HWApplication::translate("binds (keys)", controllerright)).toUtf8().constData()); |
161 } |
181 } |
162 |
182 |
163 // Register entries for all buttons of this joystick/gamepad |
183 // Register entries for all buttons of this joystick/gamepad |
164 for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
184 for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
165 { |
185 { |
166 // Buttons |
186 // Buttons |
167 sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
187 sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
168 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()); |
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()); |
169 } |
189 } |
170 // Close the game controller as we no longer need it |
190 // Close the game controller as we no longer need it |
171 SDL_JoystickClose(joy); |
191 SDL_JoystickClose(joy); |
172 } |
192 } |
173 |
193 |
|
194 if(i >= 1024) |
|
195 i = 1023; |
|
196 |
174 // Terminate the list |
197 // Terminate the list |
175 sdlkeys[i][0][0] = '\0'; |
198 sdlkeys[i][0][0] = '\0'; |
176 sdlkeys[i][1][0] = '\0'; |
199 sdlkeys[i][1][0] = '\0'; |
|
200 #endif |
177 } |
201 } |
178 |
202 |
179 |
203 |
180 void SDLInteraction::SDLAudioInit() |
204 void SDLInteraction::SDLAudioInit() |
181 { |
205 { |
182 // don't init again |
206 // don't init again |
183 if (m_audioInitialized) |
207 if (m_audioInitialized) |
184 return; |
208 return; |
185 |
209 |
186 SDL_Init(SDL_INIT_AUDIO); |
210 SDL_Init(SDL_INIT_AUDIO); |
187 Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); |
211 if(!Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)) /* should we keep trying, or just turn off permanently? */ |
188 m_audioInitialized = true; |
212 m_audioInitialized = true; |
189 } |
213 } |
190 |
214 |
191 |
215 |
192 void SDLInteraction::playSoundFile(const QString & soundFile) |
216 void SDLInteraction::playSoundFile(const QString & soundFile) |
193 { |
217 { |
|
218 if (!HWForm::config || !HWForm::config->isFrontendSoundEnabled()) return; |
194 SDLAudioInit(); |
219 SDLAudioInit(); |
|
220 if (!m_audioInitialized) return; |
195 if (!m_soundMap->contains(soundFile)) |
221 if (!m_soundMap->contains(soundFile)) |
196 m_soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData())); |
222 m_soundMap->insert(soundFile, Mix_LoadWAV_RW(PHYSFSRWOPS_openRead(soundFile.toLocal8Bit().constData()), 1)); |
197 |
223 |
198 //FIXME: this is a hack, but works as long as we have few concurrent playing sounds |
224 //FIXME: this is a hack, but works as long as we have few concurrent playing sounds |
199 if (Mix_Playing(lastchannel) == false) |
225 if (Mix_Playing(lastchannel) == false) |
200 lastchannel = Mix_PlayChannel(-1, m_soundMap->value(soundFile), 0); |
226 lastchannel = Mix_PlayChannel(-1, m_soundMap->value(soundFile), 0); |
201 } |
227 } |