Supertux can now run even if we were unable to open an audio device
[supertux.git] / src / options_menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include "options_menu.hpp"
23 #include "gui/menu.hpp"
24 #include "audio/sound_manager.hpp"
25 #include "control/joystickkeyboardcontroller.hpp"
26 #include "main.hpp"
27 #include "gettext.hpp"
28 #include "gameconfig.hpp"
29
30 Menu* options_menu   = 0;
31
32 enum OptionsMenuIDs {
33   MNID_FULLSCREEN,
34   MNID_SOUND,
35   MNID_MUSIC
36 };
37
38 class OptionsMenu : public Menu
39 {
40 public:
41   OptionsMenu();
42   virtual ~OptionsMenu();
43
44   virtual void menu_action(MenuItem* item);
45 };
46
47 OptionsMenu::OptionsMenu()
48 {
49   add_label(_("Options"));
50   add_hl();
51   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen);
52   if (sound_manager->is_audio_enabled()) {
53     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled);
54     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled);
55   }
56   else
57   {
58     add_deactive(MNID_SOUND, _("Sound disabled"));
59     add_deactive(MNID_SOUND, _("Music disabled"));
60   }
61   add_submenu(_("Setup Keys"), main_controller->get_key_options_menu());
62   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu());
63   add_hl();
64   add_back(_("Back"));
65 }
66
67 OptionsMenu::~OptionsMenu()
68 {
69 }
70
71 void
72 OptionsMenu::menu_action(MenuItem* item)
73 {
74   switch (item->id) {
75     case MNID_FULLSCREEN:
76       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
77         config->use_fullscreen = !config->use_fullscreen;
78         init_video();
79         config->save();
80       }
81       break;
82     case MNID_SOUND:
83       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
84         config->sound_enabled = !config->sound_enabled;
85         sound_manager->enable_sound(config->sound_enabled);
86         config->save();
87       }
88       break;
89     case MNID_MUSIC:
90       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
91         config->music_enabled = !config->music_enabled;
92         sound_manager->enable_music(config->music_enabled);
93         config->save();
94       }
95       break;
96     default:
97       break;
98   }
99 }
100
101 Menu* get_options_menu()
102 {
103   if(options_menu == NULL)
104     options_menu = new OptionsMenu();
105
106   return options_menu;
107 }
108
109 void free_options_menu()
110 {
111   delete options_menu;
112   options_menu = NULL;
113 }