e218a1037a7b1531bb88a7a0e6c3fee7d8c4be87
[supertux.git] / src / supertux / 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/options_menu.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "control/joystickkeyboardcontroller.hpp"
22 #include "gui/menu.hpp"
23 #include "gui/menu_item.hpp"
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/main.hpp"
26 #include "supertux/profile_menu.hpp"
27 #include "supertux/language_menu.hpp"
28 #include "util/gettext.hpp"
29 #include "video/renderer.hpp"
30
31 Menu* options_menu   = 0;
32
33 enum OptionsMenuIDs {
34   MNID_FULLSCREEN,
35   MNID_FULLSCREEN_RESOLUTION,
36   MNID_MAGNIFICATION,
37   MNID_ASPECTRATIO,
38   MNID_PROFILES,
39   MNID_SOUND,
40   MNID_MUSIC
41 };
42
43 OptionsMenu::OptionsMenu() :
44   language_menu()
45 {
46   language_menu.reset(new LanguageMenu());
47
48   add_label(_("Options"));
49   add_hl();
50
51   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
52   // FIXME: Implement me: if (get_parent() == main_menu)
53   add_submenu(_("Select Language"), language_menu.get())
54     ->set_help(_("Select a different language to display text in"));
55
56   add_submenu(_("Select Profile"), get_profile_menu())
57     ->set_help(_("Select a profile to play with"));
58
59   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
60     ->set_help(_("Select your profile immediately after start-up"));
61   
62   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
63     ->set_help(_("Fill the entire screen"));
64
65   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
66   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
67
68   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
69   magnification->set_help(_("Change the magnification of the game area"));
70
71   // These values go from screen:640/projection:1600 to
72   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
73   magnification->list.push_back("auto");
74   magnification->list.push_back("40%");
75   magnification->list.push_back("50%");
76   magnification->list.push_back("62.5%");
77   magnification->list.push_back("80%");
78   magnification->list.push_back("100%");
79   magnification->list.push_back("125%");
80   magnification->list.push_back("160%");
81   magnification->list.push_back("200%");
82   magnification->list.push_back("250%");
83
84   SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
85
86   if (modes == (SDL_Rect **)0) 
87   { // No resolutions at all available, bad
88
89   }
90   else if(modes == (SDL_Rect **)-1) 
91   { // All resolutions should work, so we fall back to hardcoded defaults
92     fullscreen_res->list.push_back("640x480");
93     fullscreen_res->list.push_back("800x600");
94     fullscreen_res->list.push_back("1024x768");
95     fullscreen_res->list.push_back("1152x864");
96     fullscreen_res->list.push_back("1280x960");
97     fullscreen_res->list.push_back("1280x1024");
98     fullscreen_res->list.push_back("1440x900");
99     fullscreen_res->list.push_back("1680x1050");
100     fullscreen_res->list.push_back("1600x1200");
101     fullscreen_res->list.push_back("1920x1080");
102     fullscreen_res->list.push_back("1920x1200");
103   }
104   else 
105   {
106     for(int i = 0; modes[i]; ++i)
107     {
108       std::ostringstream out;          
109       out << modes[i]->w << "x" << modes[i]->h;
110       fullscreen_res->list.push_back(out.str());
111     }
112   }
113
114   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
115   aspect->set_help(_("Adjust the aspect ratio"));
116   
117   aspect->list.push_back("auto");
118   aspect->list.push_back("5:4");
119   aspect->list.push_back("4:3");
120   aspect->list.push_back("16:10");
121   aspect->list.push_back("16:9");
122   aspect->list.push_back("1368:768");
123
124   if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
125   {
126     std::ostringstream out;
127     out << g_config->aspect_width << ":" << g_config->aspect_height;
128     std::string aspect_ratio = out.str();
129     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
130     {
131       if(*i == aspect_ratio)
132       {
133         aspect_ratio.clear();
134         break;
135       }
136     }
137
138     if (!aspect_ratio.empty())
139     {
140       aspect->selected = aspect->list.size();
141       aspect->list.push_back(aspect_ratio);
142     }
143   }
144   
145   if (sound_manager->is_audio_enabled()) {
146     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
147       ->set_help(_("Disable all sound effects"));
148     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
149       ->set_help(_("Disable all music"));
150   } else {
151     add_inactive(MNID_SOUND, _("Sound (disabled)"));
152     add_inactive(MNID_MUSIC, _("Music (disabled)"));
153   }
154   
155   add_submenu(_("Setup Keyboard"), g_main_controller->get_key_options_menu())
156     ->set_help(_("Configure key-action mappings"));
157
158   add_submenu(_("Setup Joystick") ,g_main_controller->get_joystick_options_menu())
159     ->set_help(_("Configure joystick control-action mappings"));
160   add_hl();
161   add_back(_("Back"));
162 }
163
164 OptionsMenu::~OptionsMenu()
165 {
166 }
167
168 void
169 OptionsMenu::menu_action(MenuItem* item)
170 {
171   switch (item->id) {
172     case MNID_ASPECTRATIO:
173     { 
174       if (item->list[item->selected] == "auto")
175       {
176         g_config->aspect_width  = 0; // Magic values
177         g_config->aspect_height = 0;
178         Renderer::instance()->apply_config();
179         Menu::recalc_pos();
180       }
181       else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
182       {
183         Renderer::instance()->apply_config();
184         Menu::recalc_pos();
185       }
186       else
187       {
188         assert(!"This must not be reached");
189       }
190     }
191     break;
192
193     case MNID_MAGNIFICATION:
194       if (item->list[item->selected] == "auto")
195       {
196         g_config->magnification = 0.0f; // Magic value 
197       }
198       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
199       {
200         g_config->magnification /= 100.0f;
201       }
202       Renderer::instance()->apply_config();
203       Menu::recalc_pos();
204       break;
205
206     case MNID_FULLSCREEN_RESOLUTION:
207       if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
208       {
209         // do nothing, changes are only applied when toggling fullscreen mode
210       }      
211       break;
212
213     case MNID_FULLSCREEN:
214       if(g_config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
215         g_config->use_fullscreen = !g_config->use_fullscreen;
216         init_video(); // FIXME: Should call apply_config instead
217         Menu::recalc_pos();
218         g_config->save();
219       }
220       break;
221
222     case MNID_SOUND:
223       if(g_config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
224         g_config->sound_enabled = !g_config->sound_enabled;
225         sound_manager->enable_sound(g_config->sound_enabled);
226         g_config->save();
227       }
228       break;
229
230     case MNID_MUSIC:
231       if(g_config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
232         g_config->music_enabled = !g_config->music_enabled;
233         sound_manager->enable_music(g_config->music_enabled);
234         g_config->save();
235       }
236       break;
237
238     default:
239       break;
240   }
241 }
242
243 Menu* get_options_menu()
244 {
245   //static OptionsMenu menu;
246   options_menu = new OptionsMenu();
247   return options_menu;
248 }
249
250 void free_options_menu()
251 {
252   delete options_menu;
253   options_menu = 0;
254 }
255
256 /* EOF */