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