Move some more globals to supertux/globals.hpp
[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/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_width (800),
31   fullscreen_height(600),
32   window_width (800),
33   window_height(600),
34   aspect_width (0), // auto detect
35   aspect_height(0),
36   magnification(1.0f),
37   use_fullscreen(false),
38   video(AUTO_VIDEO),
39   try_vsync(true),
40   show_fps(false),
41   sound_enabled(true),
42   music_enabled(true),
43   console_enabled(false),
44   random_seed(0),          // set by time(), by default (unless in config)
45   start_level(),
46   enable_script_debugger(false),
47   start_demo(),
48   record_demo(),
49   locale()
50 {
51 }
52
53 Config::~Config()
54 {}
55
56 void
57 Config::load()
58 {
59   lisp::Parser parser;
60   const lisp::Lisp* root = parser.parse("config");
61
62   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
63   if(!config_lisp)
64     throw std::runtime_error("File is not a supertux-config file");
65
66   config_lisp->get("show_fps", show_fps);
67   config_lisp->get("console", console_enabled);
68   config_lisp->get("locale", locale);
69   config_lisp->get("random_seed", random_seed);
70
71   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
72   if(config_video_lisp) {
73     config_video_lisp->get("fullscreen", use_fullscreen);
74     std::string video_string;
75     config_video_lisp->get("video", video_string);
76     video = get_video_system(video_string);
77     config_video_lisp->get("vsync", try_vsync);
78
79     config_video_lisp->get("fullscreen_width",  fullscreen_width);
80     config_video_lisp->get("fullscreen_height", fullscreen_height);
81
82     config_video_lisp->get("window_width",  window_width);
83     config_video_lisp->get("window_height", window_height);
84
85     config_video_lisp->get("aspect_width",  aspect_width);
86     config_video_lisp->get("aspect_height", aspect_height);
87   }
88
89   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
90   if(config_audio_lisp) {
91     config_audio_lisp->get("sound_enabled", sound_enabled);
92     config_audio_lisp->get("music_enabled", music_enabled);
93   }
94
95   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
96   if(config_control_lisp && g_main_controller) {
97     g_main_controller->read(*config_control_lisp);
98   }
99
100   const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons");
101   if(config_addons_lisp) {
102     AddonManager::get_instance().read(*config_addons_lisp);
103   }
104 }
105
106 void
107 Config::save()
108 {
109   lisp::Writer writer("config");
110
111   writer.start_list("supertux-config");
112
113   writer.write("show_fps", show_fps);
114   writer.write("console", console_enabled);
115   writer.write("locale", locale);
116
117   writer.start_list("video");
118   writer.write("fullscreen", use_fullscreen);
119   writer.write("video", get_video_string(video));
120   writer.write("vsync", try_vsync);
121
122   writer.write("fullscreen_width",  fullscreen_width);
123   writer.write("fullscreen_height", fullscreen_height);
124
125   writer.write("window_width",  window_width);
126   writer.write("window_height", window_height);
127
128   writer.write("aspect_width",  aspect_width);
129   writer.write("aspect_height", aspect_height);
130
131   writer.end_list("video");
132
133   writer.start_list("audio");
134   writer.write("sound_enabled", sound_enabled);
135   writer.write("music_enabled", music_enabled);
136   writer.end_list("audio");
137
138   if(g_main_controller) {
139     writer.start_list("control");
140     g_main_controller->write(writer);
141     writer.end_list("control");
142   }
143
144   writer.start_list("addons");
145   AddonManager::get_instance().write(writer);
146   writer.end_list("addons");
147
148   writer.end_list("supertux-config");
149 }
150
151 /* EOF */