Refactored video/ subsystem to make adding other methods of rendering (in particular...
[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
34 Config* config = 0;
35
36 Config::Config()
37 {
38   use_fullscreen = true;
39 #ifdef HAVE_OPENGL
40   video = "opengl";
41 #else
42   video = "sdl";
43 #endif
44   try_vsync = true;
45   show_fps = false;
46   sound_enabled = true;
47   music_enabled = true;
48   console_enabled = false;
49   random_seed = 0;          // set by time(), by default (unless in config)
50
51   screenwidth = 800;
52   screenheight = 600;
53   aspect_ratio = -1;       // autodetect
54
55   enable_script_debugger = false;
56
57   locale = ""; // autodetect 
58 }
59
60 Config::~Config()
61 {}
62
63 void
64 Config::load()
65 {
66   lisp::Parser parser;
67   const lisp::Lisp* root = parser.parse("config");
68
69   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
70   if(!config_lisp)
71     throw std::runtime_error("File is not a supertux-config file");
72
73   config_lisp->get("show_fps", show_fps);
74   config_lisp->get("console", console_enabled);
75   config_lisp->get("locale", locale);
76   config_lisp->get("random_seed", random_seed);
77
78   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
79   if(config_video_lisp) {
80     config_video_lisp->get("fullscreen", use_fullscreen);
81     config_video_lisp->get("video", video);
82     config_video_lisp->get("vsync", try_vsync);
83     config_video_lisp->get("width", screenwidth);
84     config_video_lisp->get("height", screenheight);
85     config_video_lisp->get("aspect_ratio", aspect_ratio);
86   }
87
88   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
89   if(config_audio_lisp) {
90     config_audio_lisp->get("sound_enabled", sound_enabled);
91     config_audio_lisp->get("music_enabled", music_enabled);
92   }
93
94   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
95   if(config_control_lisp && main_controller) {
96     main_controller->read(*config_control_lisp);
97   }
98 }
99
100 void
101 Config::save()
102 {
103   lisp::Writer writer("config");
104
105   writer.start_list("supertux-config");
106
107   writer.write_bool("show_fps", show_fps);
108   writer.write_bool("console", console_enabled);
109   writer.write_string("locale", locale);
110
111   writer.start_list("video");
112   writer.write_bool("fullscreen", use_fullscreen);
113   writer.write_string("video", video);
114   writer.write_bool("vsync", try_vsync);
115   writer.write_int("width", screenwidth);
116   writer.write_int("height", screenheight);
117   writer.write_float("aspect_ratio", aspect_ratio);
118   writer.end_list("video");
119
120   writer.start_list("audio");
121   writer.write_bool("sound_enabled", sound_enabled);
122   writer.write_bool("music_enabled", music_enabled);
123   writer.end_list("audio");
124
125   if(main_controller) {
126     writer.start_list("control");
127     main_controller->write(writer);
128     writer.end_list("control");
129   }
130
131   writer.end_list("supertux-config");
132 }