- Major changes in Setup-API.
[supertux.git] / lib / utils / configfile.cpp
1 //  $Id$
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
20 #include <cstdlib>
21 #include <string>
22
23 #include "../utils/configfile.h"
24 #include "../app/setup.h"
25 #include "../app/globals.h"
26 #include "../audio/sound_manager.h"
27
28 using namespace SuperTux;
29
30 #ifdef WIN32
31 const char * config_filename = "/st_config.dat";
32 #else
33 const char * config_filename = "/config";
34 #endif
35
36 Config* SuperTux::config = 0;
37
38 static void defaults ()
39 {
40   /* Set defaults: */
41   debug_mode = false;
42   SoundManager::get()->set_audio_device_available(true);
43
44   use_fullscreen = false;
45   show_fps = false;
46   use_gl = false;
47
48   SoundManager::get()->enable_sound(true);
49   SoundManager::get()->enable_music(true);
50 }
51
52 FILE * SuperTux::opendata(const char * rel_filename, const char * mode)
53 {
54   char * filename = NULL;
55   FILE * fi;
56
57   filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
58                                              strlen(rel_filename) + 1));
59
60   strcpy(filename, st_dir);
61   /* Open the high score file: */
62
63   strcat(filename, rel_filename);
64
65   /* Try opening the file: */
66   fi = fopen(filename, mode);
67
68   if (fi == NULL)
69     {
70       fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
71
72       if (strcmp(mode, "r") == 0)
73         fprintf(stderr, "for read!!!\n");
74       else if (strcmp(mode, "w") == 0)
75         fprintf(stderr, "for write!!!\n");
76     }
77   free( filename );
78
79   return(fi);
80 }
81
82 void Config::load()
83 {
84   FILE * file = NULL;
85
86   defaults();
87
88   /* override defaults from config file */
89
90   file = opendata(config_filename, "r");
91
92   if (file == NULL)
93     return;
94
95   /* read config file */
96
97   lisp_stream_t   stream;
98   lisp_object_t * root_obj = NULL;
99
100   lisp_stream_init_file (&stream, file);
101   root_obj = lisp_read (&stream);
102
103   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
104     return;
105
106   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-config") != 0)
107     return;
108
109   LispReader reader(lisp_cdr(root_obj));
110
111   reader.read_bool("fullscreen", use_fullscreen);
112   bool temp;
113   reader.read_bool("sound",     temp);
114   SoundManager::get()->enable_sound(temp);
115   reader.read_bool("music",      temp);
116   SoundManager::get()->enable_music(temp);
117   reader.read_bool("show_fps",   show_fps);
118
119   std::string video;
120   reader.read_string ("video", video);
121   if (video == "opengl")
122     use_gl = true;
123   else
124     use_gl = false;
125
126   reader.read_int ("joystick", joystick_num);
127
128   if (joystick_num >= 0)
129     {
130     reader.read_int ("joystick-x", joystick_keymap.x_axis);
131     reader.read_int ("joystick-y", joystick_keymap.y_axis);
132     reader.read_int ("joystick-a", joystick_keymap.a_button);
133     reader.read_int ("joystick-b", joystick_keymap.b_button);
134     reader.read_int ("joystick-start", joystick_keymap.start_button);
135     reader.read_int ("joystick-deadzone", joystick_keymap.dead_zone);
136     }
137
138   customload(reader);
139
140   lisp_free(root_obj);
141 }
142
143 void Config::save ()
144 {
145   /* write settings to config file */
146   FILE * config = opendata(config_filename, "w");
147
148   if(config)
149     {
150       fprintf(config, "(supertux-config\n");
151       fprintf(config, "\t;; the following options can be set to #t or #f:\n");
152       fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f");
153       fprintf(config, "\t(sound      %s)\n", SoundManager::get()->sound_enabled()      ? "#t" : "#f");
154       fprintf(config, "\t(music      %s)\n", SoundManager::get()->music_enabled()      ? "#t" : "#f");
155       fprintf(config, "\t(show_fps   %s)\n", show_fps       ? "#t" : "#f");
156
157       fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n");
158       fprintf(config, "\t(video      \"%s\")\n", use_gl ? "opengl" : "sdl");
159
160       if(use_joystick)
161         {
162         fprintf(config, "\n\t;; joystick number:\n");
163         fprintf(config, "\t(joystick   %d)\n", joystick_num);
164
165         fprintf(config, "\t(joystick-x   %d)\n", joystick_keymap.x_axis);
166         fprintf(config, "\t(joystick-y   %d)\n", joystick_keymap.y_axis);
167         fprintf(config, "\t(joystick-a   %d)\n", joystick_keymap.a_button);
168         fprintf(config, "\t(joystick-b   %d)\n", joystick_keymap.b_button);
169         fprintf(config, "\t(joystick-start  %d)\n", joystick_keymap.start_button);
170         fprintf(config, "\t(joystick-deadzone  %d)\n", joystick_keymap.dead_zone);
171         }
172         
173         customsave(config);
174
175       fprintf(config, ")\n");
176     }
177 }
178
179 /* EOF */