Turned AddonManager into a Currenton
[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/input_manager.hpp"
23 #include "lisp/writer.hpp"
24 #include "lisp/parser.hpp"
25 #include "util/reader.hpp"
26 #include "supertux/globals.hpp"
27
28 Config::Config() :
29   profile(1),
30   fullscreen_size(0, 0),
31   fullscreen_refresh_rate(0),
32   window_size(800, 600),
33   aspect_size(0, 0), // auto detect
34   magnification(0.0f),
35   use_fullscreen(false),
36   video(VideoSystem::AUTO_VIDEO),
37   try_vsync(true),
38   show_fps(false),
39   sound_enabled(true),
40   music_enabled(true),
41   console_enabled(false),
42   random_seed(0), // set by time(), by default (unless in config)
43   start_level(),
44   enable_script_debugger(false),
45   start_demo(),
46   record_demo(),
47   locale()
48 {
49 }
50
51 Config::~Config()
52 {}
53
54 void
55 Config::load()
56 {
57   lisp::Parser parser;
58   const lisp::Lisp* root = parser.parse("config");
59
60   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
61   if(!config_lisp)
62   {
63     throw std::runtime_error("File is not a supertux-config file");
64   }
65
66   config_lisp->get("profile", profile);
67   config_lisp->get("show_fps", show_fps);
68   config_lisp->get("console", console_enabled);
69   config_lisp->get("locale", locale);
70   config_lisp->get("random_seed", random_seed);
71
72   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
73   if(config_video_lisp)
74   {
75     config_video_lisp->get("fullscreen", use_fullscreen);
76     std::string video_string;
77     config_video_lisp->get("video", video_string);
78     video = VideoSystem::get_video_system(video_string);
79     config_video_lisp->get("vsync", try_vsync);
80
81     config_video_lisp->get("fullscreen_width",  fullscreen_size.width);
82     config_video_lisp->get("fullscreen_height", fullscreen_size.height);
83     config_video_lisp->get("fullscreen_refresh_rate", fullscreen_refresh_rate);
84
85     config_video_lisp->get("window_width",  window_size.width);
86     config_video_lisp->get("window_height", window_size.height);
87
88     config_video_lisp->get("aspect_width",  aspect_size.width);
89     config_video_lisp->get("aspect_height", aspect_size.height);
90
91     config_video_lisp->get("magnification", magnification);
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 && InputManager::current())
102   {
103     InputManager::current()->read(*config_control_lisp);
104   }
105
106   const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons");
107   if(config_addons_lisp && AddonManager::current())
108   {
109     AddonManager::current()->read(*config_addons_lisp);
110   }
111 }
112
113 void
114 Config::save()
115 {
116   lisp::Writer writer("config");
117
118   writer.start_list("supertux-config");
119
120   writer.write("profile", profile);
121   writer.write("show_fps", show_fps);
122   writer.write("console", console_enabled);
123   writer.write("locale", locale);
124
125   writer.start_list("video");
126   writer.write("fullscreen", use_fullscreen);
127   writer.write("video", VideoSystem::get_video_string(video));
128   writer.write("vsync", try_vsync);
129
130   writer.write("fullscreen_width",  fullscreen_size.width);
131   writer.write("fullscreen_height", fullscreen_size.height);
132   writer.write("fullscreen_refresh_rate", fullscreen_refresh_rate);
133
134   writer.write("window_width",  window_size.width);
135   writer.write("window_height", window_size.height);
136
137   writer.write("aspect_width",  aspect_size.width);
138   writer.write("aspect_height", aspect_size.height);
139
140   writer.write("magnification", magnification);
141
142   writer.end_list("video");
143
144   writer.start_list("audio");
145   writer.write("sound_enabled", sound_enabled);
146   writer.write("music_enabled", music_enabled);
147   writer.end_list("audio");
148
149   if (InputManager::current())
150   {
151     writer.start_list("control");
152     InputManager::current()->write(writer);
153     writer.end_list("control");
154   }
155
156   if (AddonManager::current())
157   {
158     writer.start_list("addons");
159     AddonManager::current()->write(writer);
160     writer.end_list("addons");
161   }
162
163   writer.end_list("supertux-config");
164 }
165
166 /* EOF */