32 |
32 |
33 |
33 |
34 SDLInteraction::SDLInteraction() |
34 SDLInteraction::SDLInteraction() |
35 { |
35 { |
36 |
36 |
37 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
37 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
38 |
38 |
39 musicInitialized = 0; |
39 musicInitialized = 0; |
40 music = NULL; |
40 music = NULL; |
41 if(SDL_NumJoysticks()) |
41 if(SDL_NumJoysticks()) |
42 addGameControllerKeys(); |
42 addGameControllerKeys(); |
43 SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
43 SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
44 } |
44 } |
45 |
45 |
46 SDLInteraction::~SDLInteraction() |
46 SDLInteraction::~SDLInteraction() |
47 { |
47 { |
48 if (musicInitialized == 1) { |
48 if (musicInitialized == 1) { |
49 if (music != NULL) |
49 if (music != NULL) |
50 Mix_FreeMusic(music); |
50 Mix_FreeMusic(music); |
51 Mix_CloseAudio(); |
51 Mix_CloseAudio(); |
52 } |
52 } |
53 SDL_Quit(); |
53 SDL_Quit(); |
54 } |
54 } |
55 |
55 |
56 QStringList SDLInteraction::getResolutions() const |
56 QStringList SDLInteraction::getResolutions() const |
57 { |
57 { |
58 QStringList result; |
58 QStringList result; |
59 |
59 |
60 SDL_Rect **modes; |
60 SDL_Rect **modes; |
61 |
61 |
62 modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
62 modes = SDL_ListModes(NULL, SDL_FULLSCREEN); |
63 |
63 |
64 if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
64 if((modes == (SDL_Rect **)0) || (modes == (SDL_Rect **)-1)) |
65 { |
65 { |
66 result << "640x480"; |
66 result << "640x480"; |
67 } else |
67 } else |
68 { |
68 { |
69 for(int i = 0; modes[i]; ++i) |
69 for(int i = 0; modes[i]; ++i) |
70 if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
70 if ((modes[i]->w >= 640) && (modes[i]->h >= 480)) |
71 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); |
71 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h); |
72 } |
72 } |
73 |
73 |
74 return result; |
74 return result; |
75 } |
75 } |
76 |
76 |
77 void SDLInteraction::addGameControllerKeys() const |
77 void SDLInteraction::addGameControllerKeys() const |
78 { |
78 { |
79 QStringList result; |
79 QStringList result; |
80 |
80 |
81 int i = 0; |
81 int i = 0; |
82 while(i < 1024 && sdlkeys[i][1][0] != '\0') |
82 while(i < 1024 && sdlkeys[i][1][0] != '\0') |
83 i++; |
83 i++; |
84 |
84 |
85 // Iterate through all game controllers |
85 // Iterate through all game controllers |
86 for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
86 for(int jid = 0; jid < SDL_NumJoysticks(); jid++) |
87 { |
87 { |
88 SDL_Joystick* joy = SDL_JoystickOpen(jid); |
88 SDL_Joystick* joy = SDL_JoystickOpen(jid); |
89 |
89 |
90 // Retrieve the game controller's name and strip "Controller (...)" that's added by some drivers (English only) |
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"); |
91 QString joyname = QString(SDL_JoystickName(jid)).replace(QRegExp("^Controller \\((.*)\\)$"), "\\1"); |
92 |
92 |
93 // Connected Xbox 360 controller? Use specific button names then |
93 // Connected Xbox 360 controller? Use specific button names then |
94 // Might be interesting to add 'named' buttons for the most often used gamepads |
94 // Might be interesting to add 'named' buttons for the most often used gamepads |
95 bool isxb = joyname.contains("Xbox 360"); |
95 bool isxb = joyname.contains("Xbox 360"); |
96 |
96 |
97 // This part of the string won't change for multiple keys/hats, so keep it |
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); |
98 QString prefix = QString("%1 (%2): ").arg(joyname).arg(jid + 1); |
99 |
99 |
100 // Register entries for missing axes not assigned to sticks of this joystick/gamepad |
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++) |
101 for(int aid = 0; aid < SDL_JoystickNumAxes(joy) && i < 1021; aid++) |
102 { |
102 { |
103 // Again store the part of the string not changing for multiple uses |
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); |
104 QString axis = prefix + QApplication::translate("binds (keys)", "Axis") + QString(" %1 ").arg(aid + 1); |
105 |
105 |
106 // Entry for "Axis Up" |
106 // Entry for "Axis Up" |
107 sprintf(sdlkeys[i][0], "j%da%du", jid, aid); |
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()); |
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 |
109 |
110 // Entry for "Axis Down" |
110 // Entry for "Axis Down" |
111 sprintf(sdlkeys[i][0], "j%da%dd", jid, aid); |
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()); |
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 } |
113 } |
114 |
114 |
115 // Register entries for all coolie hats of this joystick/gamepad |
115 // Register entries for all coolie hats of this joystick/gamepad |
116 for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
116 for(int hid = 0; hid < SDL_JoystickNumHats(joy) && i < 1019; hid++) |
117 { |
117 { |
118 // Again store the part of the string not changing for multiple uses |
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)); |
119 QString hat = prefix + (isxb ? (QApplication::translate("binds (keys)", xb360dpad) + QString(" ")) : QApplication::translate("binds (keys)", "Hat") + QString(" %1 ").arg(hid + 1)); |
120 |
120 |
121 // Entry for "Hat Up" |
121 // Entry for "Hat Up" |
122 sprintf(sdlkeys[i][0], "j%dh%du", jid, hid); |
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()); |
123 sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Up)")).toStdString().c_str()); |
124 |
124 |
125 // Entry for "Hat Down" |
125 // Entry for "Hat Down" |
126 sprintf(sdlkeys[i][0], "j%dh%dd", jid, hid); |
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()); |
127 sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Down)")).toStdString().c_str()); |
128 |
128 |
129 // Entry for "Hat Left" |
129 // Entry for "Hat Left" |
130 sprintf(sdlkeys[i][0], "j%dh%dl", jid, hid); |
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()); |
131 sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Left)")).toStdString().c_str()); |
132 |
132 |
133 // Entry for "Hat Right" |
133 // Entry for "Hat Right" |
134 sprintf(sdlkeys[i][0], "j%dh%dr", jid, hid); |
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()); |
135 sprintf(sdlkeys[i++][1], "%s", (hat + QApplication::translate("binds (keys)", "(Right)")).toStdString().c_str()); |
136 } |
136 } |
137 |
137 |
138 // Register entries for all buttons of this joystick/gamepad |
138 // Register entries for all buttons of this joystick/gamepad |
139 for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
139 for(int bid = 0; bid < SDL_JoystickNumButtons(joy) && i < 1022; bid++) |
140 { |
140 { |
141 // Buttons |
141 // Buttons |
142 sprintf(sdlkeys[i][0], "j%db%d", jid, bid); |
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()); |
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 } |
144 } |
145 // Close the game controller as we no longer need it |
145 // Close the game controller as we no longer need it |
146 SDL_JoystickClose(joy); |
146 SDL_JoystickClose(joy); |
147 } |
147 } |
148 |
148 |
149 // Terminate the list |
149 // Terminate the list |
150 sdlkeys[i][0][0] = '\0'; |
150 sdlkeys[i][0][0] = '\0'; |
151 sdlkeys[i][1][0] = '\0'; |
151 sdlkeys[i][1][0] = '\0'; |
152 } |
152 } |
153 |
153 |
154 void SDLInteraction::SDLMusicInit() |
154 void SDLInteraction::SDLMusicInit() |
155 { |
155 { |
156 if (musicInitialized == 0) { |
156 if (musicInitialized == 0) { |
157 SDL_Init(SDL_INIT_AUDIO); |
157 SDL_Init(SDL_INIT_AUDIO); |
158 Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); |
158 Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); |
159 musicInitialized = 1; |
159 musicInitialized = 1; |
160 } |
160 } |
161 } |
161 } |
162 |
162 |
163 |
163 |
164 void SDLInteraction::StartMusic() |
164 void SDLInteraction::StartMusic() |
165 { |
165 { |
166 SDLMusicInit(); |
166 SDLMusicInit(); |
167 |
167 |
168 if (music == NULL) { |
168 if (music == NULL) { |
169 music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); |
169 music = Mix_LoadMUS((datadir->absolutePath() + "/Music/main theme.ogg").toLocal8Bit().constData()); |
170 |
170 |
171 } |
171 } |
172 Mix_VolumeMusic(MIX_MAX_VOLUME - 28); |
172 Mix_VolumeMusic(MIX_MAX_VOLUME - 28); |
173 Mix_FadeInMusic(music, -1, 1750); |
173 Mix_FadeInMusic(music, -1, 1750); |
174 } |
174 } |
175 |
175 |
176 void SDLInteraction::StopMusic() |
176 void SDLInteraction::StopMusic() |
177 { |
177 { |
178 if (music != NULL) { |
178 if (music != NULL) { |
179 // fade out music to finish 0,5 seconds from now |
179 // fade out music to finish 0,5 seconds from now |
180 while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) { |
180 while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) { |
181 SDL_Delay(100); |
181 SDL_Delay(100); |
182 } |
182 } |
183 } |
183 } |
184 } |
184 } |
185 |
185 |