1e753b24d954f06c300817b6890453801934961e
[supertux.git] / src / gameconfig.cpp
1 //  $Id: configfile.cpp 2212 2004-11-28 14:57:45Z matzebraun $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Michael George <mike@georgetech.com>
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 <cstdlib>
24 #include <string>
25 #include <stdexcept>
26 #include <sstream>
27 #include <fstream>
28
29 #include "lisp/parser.hpp"
30 #include "lisp/lisp.hpp"
31 #include "lisp/writer.hpp"
32 #include "control/joystickkeyboardcontroller.hpp"
33 #include "resources.hpp"
34 #include "main.hpp"
35
36 Config* config = 0;
37
38 Config::Config()
39 {
40   use_fullscreen = true;
41   show_fps = false;
42   sound_enabled = true;
43   music_enabled = true;
44   cheats_enabled = false;
45
46   screenwidth = 800;
47   screenheight = 600;
48   use_gl = true;
49 }
50
51 Config::~Config()
52 {}
53
54 void
55 Config::load()
56 {
57   lisp::Parser parser;
58   std::auto_ptr<lisp::Lisp> root (parser.parse("config"));
59
60   const lisp::Lisp* config_lisp = root->get_lisp("supertux-config");
61   if(!config_lisp)
62     throw std::runtime_error("File is not a supertux-config file");
63
64   config_lisp->get("show_fps", show_fps);
65   config_lisp->get("cheats", cheats_enabled);
66
67   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
68   if(config_video_lisp) {
69     config_video_lisp->get("fullscreen", use_fullscreen);
70     config_video_lisp->get("width", screenwidth);
71     config_video_lisp->get("height", screenheight);
72   }
73
74   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
75   if(config_audio_lisp) {
76     config_audio_lisp->get("sound_enabled", sound_enabled);
77     config_audio_lisp->get("music_enabled", music_enabled);
78   }
79
80   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
81   if(config_control_lisp && main_controller) {
82     main_controller->read(*config_control_lisp);
83   }
84 }
85
86 void
87 Config::save()
88 {
89   lisp::Writer writer("config");
90
91   writer.start_list("supertux-config");
92
93   writer.write_bool("show_fps", show_fps);
94   writer.write_bool("cheats", cheats_enabled);
95
96   writer.start_list("video");
97   writer.write_bool("fullscreen", use_fullscreen);
98   writer.write_int("width", screenwidth);
99   writer.write_int("height", screenheight);
100   writer.end_list("video");
101
102   writer.start_list("audio");
103   writer.write_bool("sound_enabled", sound_enabled);
104   writer.write_bool("music_enabled", music_enabled);
105   writer.end_list("audio");
106
107   if(main_controller) {
108     writer.start_list("control");
109     main_controller->write(writer);
110     writer.end_list("control");
111   }
112   
113   writer.end_list("supertux-config");
114 }