Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / gameconfig.cpp
1 //  SuperTux -  A Jump'n Run
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/gameconfig.hpp"
18
19 #include <stdexcept>
20
21 #include "addon/addon_manager.hpp"
22 #include "control/joystickkeyboardcontroller.hpp"
23 #include "lisp/lisp.hpp"
24 #include "lisp/writer.hpp"
25 #include "lisp/parser.hpp"
26 #include "supertux/main.hpp"
27
28 Config* g_config = 0;
29
30 Config::Config()
31 {
32   profile = 1;
33   use_fullscreen = false;
34   video = AUTO_VIDEO;
35   try_vsync = true;
36   show_fps = false;
37   sound_enabled = true;
38   music_enabled = true;
39   console_enabled = false;
40   random_seed = 0;          // set by time(), by default (unless in config)
41
42   window_width  = 800;
43   window_height = 600;
44
45   fullscreen_width  = 800;
46   fullscreen_height = 600;
47
48   magnification = 1.0f;
49
50   aspect_width  = 0; // auto detect
51   aspect_height = 0;
52
53   enable_script_debugger = false;
54
55   locale = ""; // autodetect 
56 }
57
58 Config::~Config()
59 {}
60
61 void
62 Config::load()
63 {
64   lisp::Parser parser;
65   const lisp::Lisp* root = parser.parse("config");
66
67   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
68   if(!config_lisp)
69     throw std::runtime_error("File is not a supertux-config file");
70
71   config_lisp->get("show_fps", show_fps);
72   config_lisp->get("console", console_enabled);
73   config_lisp->get("locale", locale);
74   config_lisp->get("random_seed", random_seed);
75
76   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
77   if(config_video_lisp) {
78     config_video_lisp->get("fullscreen", use_fullscreen);
79     std::string video_string;
80     config_video_lisp->get("video", video_string);
81     video = get_video_system(video_string);
82     config_video_lisp->get("vsync", try_vsync);
83
84     config_video_lisp->get("fullscreen_width",  fullscreen_width);
85     config_video_lisp->get("fullscreen_height", fullscreen_height);
86
87     config_video_lisp->get("window_width",  window_width);
88     config_video_lisp->get("window_height", window_height);
89
90     config_video_lisp->get("aspect_width",  aspect_width);
91     config_video_lisp->get("aspect_height", aspect_height);
92   }
93
94   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
95   if(config_audio_lisp) {
96     config_audio_lisp->get("sound_enabled", sound_enabled);
97     config_audio_lisp->get("music_enabled", music_enabled);
98   }
99
100   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
101   if(config_control_lisp && g_main_controller) {
102     g_main_controller->read(*config_control_lisp);
103   }
104
105   const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons");
106   if(config_addons_lisp) {
107     AddonManager::get_instance().read(*config_addons_lisp);
108   }
109 }
110
111 void
112 Config::save()
113 {
114   lisp::Writer writer("config");
115
116   writer.start_list("supertux-config");
117
118   writer.write("show_fps", show_fps);
119   writer.write("console", console_enabled);
120   writer.write("locale", locale);
121
122   writer.start_list("video");
123   writer.write("fullscreen", use_fullscreen);
124   writer.write("video", get_video_string(video));
125   writer.write("vsync", try_vsync);
126
127   writer.write("fullscreen_width",  fullscreen_width);
128   writer.write("fullscreen_height", fullscreen_height);
129
130   writer.write("window_width",  window_width);
131   writer.write("window_height", window_height);
132
133   writer.write("aspect_width",  aspect_width);
134   writer.write("aspect_height", aspect_height);
135
136   writer.end_list("video");
137
138   writer.start_list("audio");
139   writer.write("sound_enabled", sound_enabled);
140   writer.write("music_enabled", music_enabled);
141   writer.end_list("audio");
142
143   if(g_main_controller) {
144     writer.start_list("control");
145     g_main_controller->write(writer);
146     writer.end_list("control");
147   }
148
149   writer.start_list("addons");
150   AddonManager::get_instance().write(writer);
151   writer.end_list("addons");
152
153   writer.end_list("supertux-config");
154 }
155
156 /* EOF */