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