Disabled some more code, game now starts up and plays music, no graphics yet, just...
[supertux.git] / src / supertux / menu / options_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/menu/options_menu.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "gui/menu_manager.hpp"
22 #include "supertux/gameconfig.hpp"
23 #include "supertux/menu/joystick_menu.hpp"
24 #include "supertux/menu/keyboard_menu.hpp"
25 #include "supertux/menu/language_menu.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/menu/profile_menu.hpp"
28 #include "util/string_util.hpp"
29 #include "video/renderer.hpp"
30
31 #include <algorithm>
32 #include <sstream>
33 #include <stdio.h>
34
35 enum OptionsMenuIDs {
36   MNID_FULLSCREEN,
37   MNID_FULLSCREEN_RESOLUTION,
38   MNID_MAGNIFICATION,
39   MNID_ASPECTRATIO,
40   MNID_PROFILES,
41   MNID_SOUND,
42   MNID_MUSIC
43 };
44
45 OptionsMenu::OptionsMenu() :
46   language_menu()
47 {
48   language_menu.reset(new LanguageMenu());
49
50   add_label(_("Options"));
51   add_hl();
52
53   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
54   // FIXME: Implement me: if (get_parent() == main_menu)
55   add_submenu(_("Select Language"), language_menu.get())
56     ->set_help(_("Select a different language to display text in"));
57
58   add_submenu(_("Select Profile"), MenuStorage::get_profile_menu())
59     ->set_help(_("Select a profile to play with"));
60
61   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
62     ->set_help(_("Select your profile immediately after start-up"));
63   
64   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
65     ->set_help(_("Fill the entire screen"));
66
67   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
68   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
69
70   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
71   magnification->set_help(_("Change the magnification of the game area"));
72
73   // These values go from screen:640/projection:1600 to
74   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
75   magnification->list.push_back(_("auto"));
76   magnification->list.push_back("40%");
77   magnification->list.push_back("50%");
78   magnification->list.push_back("62.5%");
79   magnification->list.push_back("80%");
80   magnification->list.push_back("100%");
81   magnification->list.push_back("125%");
82   magnification->list.push_back("160%");
83   magnification->list.push_back("200%");
84   magnification->list.push_back("250%");
85   if (g_config->magnification != 0.0f) //auto
86   {
87     std::ostringstream out;
88     out << (g_config->magnification*100) << "%";
89     std::string magn = out.str();
90     size_t count = 0;
91     for (std::vector<std::string>::iterator i = magnification->list.begin(); i != magnification->list.end(); ++i)
92     {
93       if (*i == magn)
94       {
95         magnification->selected = count;
96         magn.clear();
97         break;
98       }
99       
100       ++count;
101     }
102     if (!magn.empty()) //magnification not in our list but accept anyway
103     {
104       magnification->selected = magnification->list.size();
105       magnification->list.push_back(magn);
106     }
107   }
108   
109
110   for(int disp_mode_ctr = 0; disp_mode_ctr < SDL_GetNumDisplayModes(0); disp_mode_ctr++)
111   {
112 #ifdef OLD_SDL1
113     SDL_DisplayMode* current = NULL;
114     int disp_retval = SDL_GetDisplayMode(0, // Display Index
115                                          disp_mode_ctr, // Mode index (default to first)
116                                          current); // DisplayMode structure ptr
117     if (disp_retval == -1) 
118     { // No resolutions at all available, bad
119     }
120     else if(disp_retval == 0) 
121     { // All resolutions should work, so we fall back to hardcoded defaults
122       std::stringstream width;
123       std::stringstream height;
124       width << current->w;
125       height << current->h;
126       fullscreen_res->list.push_back(width.str() + "x" + height.str());
127     }
128     else
129     {
130     }
131 #endif
132   }
133   // On Ubuntu/Linux resolutions are returned from highest to
134   // lowest, so reverse them
135   std::sort(fullscreen_res->list.begin(), fullscreen_res->list.end(), StringUtil::numeric_less);      
136   
137   std::ostringstream out;
138   out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height;
139   std::string fllscrn_sz = out.str();
140   size_t cnt = 0;
141   for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i) 
142   {
143     if (*i == fllscrn_sz)
144     {
145       fllscrn_sz.clear();
146       fullscreen_res->selected = cnt;
147       break;
148     }
149     ++cnt;
150   }
151   if (!fllscrn_sz.empty())
152   {
153     fullscreen_res->selected = fullscreen_res->list.size();
154     fullscreen_res->list.push_back(fllscrn_sz);
155   }
156
157   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
158   aspect->set_help(_("Adjust the aspect ratio"));
159   
160   aspect->list.push_back(_("auto"));
161   aspect->list.push_back("5:4");
162   aspect->list.push_back("4:3");
163   aspect->list.push_back("16:10");
164   aspect->list.push_back("16:9");
165   aspect->list.push_back("1368:768");
166
167   if (g_config->aspect_size != Size(0, 0))
168   {
169     std::ostringstream out;
170     out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
171     std::string aspect_ratio = out.str();
172     size_t cnt = 0;
173     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
174     {
175       if(*i == aspect_ratio)
176       {
177         aspect_ratio.clear();
178         aspect->selected = cnt;
179         break;
180       }
181       ++cnt;
182     }
183
184     if (!aspect_ratio.empty())
185     {
186       aspect->selected = aspect->list.size();
187       aspect->list.push_back(aspect_ratio);
188     }
189   }
190   
191   if (sound_manager->is_audio_enabled()) {
192     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
193       ->set_help(_("Disable all sound effects"));
194     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
195       ->set_help(_("Disable all music"));
196   } else {
197     add_inactive(MNID_SOUND, _("Sound (disabled)"));
198     add_inactive(MNID_MUSIC, _("Music (disabled)"));
199   }
200   
201   add_submenu(_("Setup Keyboard"), MenuStorage::get_key_options_menu())
202     ->set_help(_("Configure key-action mappings"));
203
204   add_submenu(_("Setup Joystick"), MenuStorage::get_joystick_options_menu())
205     ->set_help(_("Configure joystick control-action mappings"));
206   add_hl();
207   add_back(_("Back"));
208 }
209
210 OptionsMenu::~OptionsMenu()
211 {
212 }
213
214 void
215 OptionsMenu::menu_action(MenuItem* item)
216 {
217   switch (item->id) {
218     case MNID_ASPECTRATIO:
219     { 
220       if (item->list[item->selected] == _("auto"))
221       {
222         g_config->aspect_size = Size(0, 0); // Magic values
223         Renderer::instance()->apply_config();
224         MenuManager::recalc_pos();
225       }
226       else if (sscanf(item->list[item->selected].c_str(), "%d:%d", 
227                       &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
228       {
229         Renderer::instance()->apply_config();
230         MenuManager::recalc_pos();
231       }
232       else
233       {
234         assert(!"This must not be reached");
235       }
236     }
237     break;
238
239     case MNID_MAGNIFICATION:
240       if (item->list[item->selected] == _("auto"))
241       {
242         g_config->magnification = 0.0f; // Magic value 
243       }
244       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
245       {
246         g_config->magnification /= 100.0f;
247       }
248       Renderer::instance()->apply_config();
249       MenuManager::recalc_pos();
250       break;
251
252     case MNID_FULLSCREEN_RESOLUTION:
253       if(sscanf(item->list[item->selected].c_str(), "%dx%d", 
254                 &g_config->fullscreen_size.width, &g_config->fullscreen_size.height) == 2)
255       {
256         // do nothing, changes are only applied when toggling fullscreen mode
257       }      
258       break;
259
260     case MNID_FULLSCREEN:
261       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
262         g_config->use_fullscreen = !g_config->use_fullscreen;
263         Renderer::instance()->apply_config();
264         MenuManager::recalc_pos();
265         g_config->save();
266       }
267       break;
268
269     case MNID_SOUND:
270       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
271         g_config->sound_enabled = !g_config->sound_enabled;
272         sound_manager->enable_sound(g_config->sound_enabled);
273         g_config->save();
274       }
275       break;
276
277     case MNID_MUSIC:
278       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
279         g_config->music_enabled = !g_config->music_enabled;
280         sound_manager->enable_music(g_config->music_enabled);
281         g_config->save();
282       }
283       break;
284
285     default:
286       break;
287   }
288 }
289
290 void
291 OptionsMenu::check_menu()
292 {
293 }
294
295 /* EOF */