2 // Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
18 #include "supertux/menu/options_menu.hpp"
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"
37 MNID_FULLSCREEN_RESOLUTION,
45 OptionsMenu::OptionsMenu(bool complete)
47 add_label(_("Options"));
52 // Language and profile changes are only be possible in the
53 // main menu, since elsewhere it might not always work fully
54 add_submenu(_("Select Language"), MenuStorage::LANGUAGE_MENU)
55 ->set_help(_("Select a different language to display text in"));
57 add_submenu(_("Select Profile"), MenuStorage::PROFILE_MENU)
58 ->set_help(_("Select a profile to play with"));
61 add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
62 ->set_help(_("Select your profile immediately after start-up"));
64 add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
65 ->set_help(_("Fill the entire screen"));
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)"));
70 MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
71 magnification->set_help(_("Change the magnification of the game area"));
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
87 std::ostringstream out;
88 out << (g_config->magnification*100) << "%";
89 std::string magn = out.str();
91 for (std::vector<std::string>::iterator i = magnification->list.begin(); i != magnification->list.end(); ++i)
95 magnification->selected = count;
102 if (!magn.empty()) //magnification not in our list but accept anyway
104 magnification->selected = magnification->list.size();
105 magnification->list.push_back(magn);
109 int display_mode_count = SDL_GetNumDisplayModes(0);
110 for(int i = 0; i < display_mode_count; ++i)
112 SDL_DisplayMode mode;
113 int ret = SDL_GetDisplayMode(0, i, &mode);
116 log_warning << "failed to get display mode: " << SDL_GetError() << std::endl;
120 std::ostringstream out;
121 out << mode.w << "x" << mode.h << "@" << mode.refresh_rate;
122 fullscreen_res->list.push_back(out.str());
125 fullscreen_res->list.push_back("Desktop");
127 std::ostringstream out;
128 std::string fullscreen_size_str = "Desktop";
129 if (g_config->fullscreen_size != Size(0, 0))
131 out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
132 fullscreen_size_str = out.str();
135 for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i)
137 if (*i == fullscreen_size_str)
139 fullscreen_size_str.clear();
140 fullscreen_res->selected = cnt;
145 if (!fullscreen_size_str.empty())
147 fullscreen_res->selected = fullscreen_res->list.size();
148 fullscreen_res->list.push_back(fullscreen_size_str);
151 MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
152 aspect->set_help(_("Adjust the aspect ratio"));
154 aspect->list.push_back(_("auto"));
155 aspect->list.push_back("5:4");
156 aspect->list.push_back("4:3");
157 aspect->list.push_back("16:10");
158 aspect->list.push_back("16:9");
159 aspect->list.push_back("1368:768");
161 if (g_config->aspect_size != Size(0, 0))
163 std::ostringstream out;
164 out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
165 std::string aspect_ratio = out.str();
167 for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
169 if(*i == aspect_ratio)
171 aspect_ratio.clear();
172 aspect->selected = cnt;
178 if (!aspect_ratio.empty())
180 aspect->selected = aspect->list.size();
181 aspect->list.push_back(aspect_ratio);
185 if (sound_manager->is_audio_enabled()) {
186 add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
187 ->set_help(_("Disable all sound effects"));
188 add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
189 ->set_help(_("Disable all music"));
191 add_inactive(MNID_SOUND, _("Sound (disabled)"));
192 add_inactive(MNID_MUSIC, _("Music (disabled)"));
195 add_submenu(_("Setup Keyboard"), MenuStorage::KEYBOARD_MENU)
196 ->set_help(_("Configure key-action mappings"));
198 add_submenu(_("Setup Joystick"), MenuStorage::JOYSTICK_MENU)
199 ->set_help(_("Configure joystick control-action mappings"));
204 OptionsMenu::~OptionsMenu()
209 OptionsMenu::menu_action(MenuItem* item)
212 case MNID_ASPECTRATIO:
214 if (item->list[item->selected] == _("auto"))
216 g_config->aspect_size = Size(0, 0); // Magic values
217 Renderer::instance()->apply_config();
218 MenuManager::instance().recalc_pos();
220 else if (sscanf(item->list[item->selected].c_str(), "%d:%d",
221 &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
223 Renderer::instance()->apply_config();
224 MenuManager::instance().recalc_pos();
228 assert(!"This must not be reached");
233 case MNID_MAGNIFICATION:
234 if (item->list[item->selected] == _("auto"))
236 g_config->magnification = 0.0f; // Magic value
238 else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
240 g_config->magnification /= 100.0f;
242 Renderer::instance()->apply_config();
243 MenuManager::instance().recalc_pos();
246 case MNID_FULLSCREEN_RESOLUTION:
251 if (item->list[item->selected] == "Desktop")
253 g_config->fullscreen_size.width = 0;
254 g_config->fullscreen_size.height = 0;
255 g_config->fullscreen_refresh_rate = 0;
257 else if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
258 &width, &height, &refresh_rate) == 3)
260 // do nothing, changes are only applied when toggling fullscreen mode
261 g_config->fullscreen_size.width = width;
262 g_config->fullscreen_size.height = height;
263 g_config->fullscreen_refresh_rate = refresh_rate;
268 case MNID_FULLSCREEN:
269 if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
270 g_config->use_fullscreen = !g_config->use_fullscreen;
271 Renderer::instance()->apply_config();
272 MenuManager::instance().recalc_pos();
278 if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
279 g_config->sound_enabled = !g_config->sound_enabled;
280 sound_manager->enable_sound(g_config->sound_enabled);
286 if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
287 g_config->music_enabled = !g_config->music_enabled;
288 sound_manager->enable_music(g_config->music_enabled);
299 OptionsMenu::check_menu()