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