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