4b50531d43bab187eed8c3708d45f2b503e05f77
[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
27 using namespace SuperTux;
28
29 #ifdef WIN32
30 const char * config_filename = "/st_config.dat";
31 #else
32 const char * config_filename = "/config";
33 #endif
34
35 Config* SuperTux::config = 0;
36
37 static void defaults ()
38 {
39   /* Set defaults: */
40   debug_mode = false;
41   audio_device = true;
42
43   use_fullscreen = false;
44   show_fps = false;
45   use_gl = false;
46
47   use_sound = true;
48   use_music = true;
49 }
50
51 void Config::load()
52 {
53   FILE * file = NULL;
54
55   defaults();
56
57   /* override defaults from config file */
58
59   file = opendata(config_filename, "r");
60
61   if (file == NULL)
62     return;
63
64   /* read config file */
65
66   lisp_stream_t   stream;
67   lisp_object_t * root_obj = NULL;
68
69   lisp_stream_init_file (&stream, file);
70   root_obj = lisp_read (&stream);
71
72   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
73     return;
74
75   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-config") != 0)
76     return;
77
78   LispReader reader(lisp_cdr(root_obj));
79
80   reader.read_bool("fullscreen", use_fullscreen);
81   reader.read_bool("sound",      use_sound);
82   reader.read_bool("music",      use_music);
83   reader.read_bool("show_fps",   show_fps);
84
85   std::string video;
86   reader.read_string ("video", video);
87   if (video == "opengl")
88     use_gl = true;
89   else
90     use_gl = false;
91
92   reader.read_int ("joystick", joystick_num);
93
94   if (joystick_num >= 0)
95     {
96     reader.read_int ("joystick-x", joystick_keymap.x_axis);
97     reader.read_int ("joystick-y", joystick_keymap.y_axis);
98     reader.read_int ("joystick-a", joystick_keymap.a_button);
99     reader.read_int ("joystick-b", joystick_keymap.b_button);
100     reader.read_int ("joystick-start", joystick_keymap.start_button);
101     reader.read_int ("joystick-deadzone", joystick_keymap.dead_zone);
102     }
103
104   customload(reader);
105
106   lisp_free(root_obj);
107 }
108
109 void Config::save ()
110 {
111   /* write settings to config file */
112   FILE * config = opendata(config_filename, "w");
113
114   if(config)
115     {
116       fprintf(config, "(supertux-config\n");
117       fprintf(config, "\t;; the following options can be set to #t or #f:\n");
118       fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f");
119       fprintf(config, "\t(sound      %s)\n", use_sound      ? "#t" : "#f");
120       fprintf(config, "\t(music      %s)\n", use_music      ? "#t" : "#f");
121       fprintf(config, "\t(show_fps   %s)\n", show_fps       ? "#t" : "#f");
122
123       fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n");
124       fprintf(config, "\t(video      \"%s\")\n", use_gl ? "opengl" : "sdl");
125
126       if(use_joystick)
127         {
128         fprintf(config, "\n\t;; joystick number:\n");
129         fprintf(config, "\t(joystick   %d)\n", joystick_num);
130
131         fprintf(config, "\t(joystick-x   %d)\n", joystick_keymap.x_axis);
132         fprintf(config, "\t(joystick-y   %d)\n", joystick_keymap.y_axis);
133         fprintf(config, "\t(joystick-a   %d)\n", joystick_keymap.a_button);
134         fprintf(config, "\t(joystick-b   %d)\n", joystick_keymap.b_button);
135         fprintf(config, "\t(joystick-start  %d)\n", joystick_keymap.start_button);
136         fprintf(config, "\t(joystick-deadzone  %d)\n", joystick_keymap.dead_zone);
137         }
138         
139         customsave(config);
140
141       fprintf(config, ")\n");
142     }
143 }
144
145 /* EOF */