18c2dd230f1287e995807906f7f76d8f5f41f7c2
[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.h"
22
23 #include <cstdlib>
24 #include <string>
25 #include <stdexcept>
26 #include <sstream>
27 #include <fstream>
28
29 #include "app/setup.h"
30 #include "app/globals.h"
31 #include "audio/sound_manager.h"
32 #include "lisp/parser.h"
33 #include "lisp/lisp.h"
34 #include "lisp/writer.h"
35 #include "control/joystickkeyboardcontroller.h"
36 #include "main.h"
37
38 using namespace SuperTux;
39
40 Config* config = 0;
41
42 Config::Config()
43 {
44   use_fullscreen = true;
45   show_fps = false;
46   sound_enabled = true;
47   music_enabled = true;
48   cheats_enabled = false;
49
50   screenwidth = 800;
51   screenheight = 600;
52   use_gl = true;
53
54   audio_frequency = MIX_DEFAULT_FREQUENCY;
55   audio_channels = MIX_DEFAULT_CHANNELS;
56   audio_chunksize = 2048;
57   audio_voices = MIX_CHANNELS;
58 }
59
60 Config::~Config()
61 {}
62
63 void
64 Config::load()
65 {
66   lisp::Parser parser;
67   std::auto_ptr<lisp::Lisp> root (parser.parse(user_dir + "/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("cheats", cheats_enabled);
75
76   const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
77   if(config_video_lisp) {
78     config_video_lisp->get("fullscreen", use_fullscreen);
79     config_video_lisp->get("width", screenwidth);
80     config_video_lisp->get("height", screenheight);
81   }
82
83   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
84   if(config_audio_lisp) {
85     config_audio_lisp->get("sound_enabled", sound_enabled);
86     config_audio_lisp->get("music_enabled", music_enabled);
87     config_audio_lisp->get("frequency", audio_frequency);
88     config_audio_lisp->get("channels", audio_channels);
89     config_audio_lisp->get("voices", audio_voices);
90     config_audio_lisp->get("chunksize", audio_chunksize);
91   }
92
93   const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control");
94   if(config_control_lisp && main_controller) {
95     main_controller->read(*config_control_lisp);
96   }
97 }
98
99 void
100 Config::save()
101 {
102   std::string configfile = user_dir + "/config";
103   std::ofstream file( (user_dir + "/config").c_str() );
104   if(!file.good()) {
105     std::stringstream msg;
106     msg << "Couldn't write config file '" << configfile << "'";
107     throw std::runtime_error(msg.str());
108   }
109   lisp::Writer writer(file);
110
111   writer.start_list("supertux-config");
112
113   writer.write_bool("show_fps", show_fps);
114   writer.write_bool("cheats", cheats_enabled);
115
116   writer.start_list("video");
117   writer.write_bool("fullscreen", use_fullscreen);
118   writer.write_int("width", screenwidth);
119   writer.write_int("height", screenheight);
120   writer.end_list("video");
121
122   writer.start_list("audio");
123   writer.write_bool("sound_enabled", sound_enabled);
124   writer.write_bool("music_enabled", music_enabled);
125   writer.write_int("frequency", audio_frequency);
126   writer.write_int("channels", audio_channels);
127   writer.write_int("voices", audio_voices);
128   writer.write_int("chunksize", audio_chunksize);
129   writer.end_list("audio");
130
131   if(main_controller) {
132     writer.start_list("control");
133     main_controller->write(writer);
134     writer.end_list("control");
135   }
136   
137   writer.end_list("supertux-config");
138 }