- replaced a few pure pointers with std::vector<>
[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
27 #ifdef WIN32
28 const char * config_filename = "/st_config.dat";
29 #else
30 const char * config_filename = "/config";
31 #endif
32
33 static void defaults ()
34 {
35   /* Set defaults: */
36   debug_mode = false;
37   audio_device = true;
38
39   use_fullscreen = false;
40   show_fps = false;
41   use_gl = false;
42
43   use_sound = true;
44   use_music = true;
45 }
46
47 void loadconfig(void)
48 {
49   FILE * file = NULL;
50
51   defaults();
52
53   /* override defaults from config file */
54
55   file = opendata(config_filename, "r");
56
57   if (file == NULL)
58     return;
59
60   /* read config file */
61
62   lisp_stream_t   stream;
63   lisp_object_t * root_obj = NULL;
64
65   lisp_stream_init_file (&stream, file);
66   root_obj = lisp_read (&stream);
67
68   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
69     return;
70
71   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-config") != 0)
72     return;
73
74   LispReader reader(lisp_cdr(root_obj));
75
76   reader.read_bool("fullscreen", &use_fullscreen);
77   reader.read_bool("sound",      &use_sound);
78   reader.read_bool("music",      &use_music);
79   reader.read_bool("show_fps",   &show_fps);
80
81   std::string video;
82   reader.read_string ("video", &video);
83   if (video == "opengl")
84     use_gl = true;
85   else
86     use_gl = false;
87
88   reader.read_int ("joystick", &joystick_num);
89   if (!(joystick_num >= 0))
90     use_joystick = false;
91   else
92     use_joystick = true;
93 }
94
95 void saveconfig (void)
96 {
97   /* write settings to config file */
98
99   FILE * config = opendata(config_filename, "w");
100
101   if(config)
102     {
103       fprintf(config, "(supertux-config\n");
104       fprintf(config, "\t;; the following options can be set to #t or #f:\n");
105       fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f");
106       fprintf(config, "\t(sound      %s)\n", use_sound      ? "#t" : "#f");
107       fprintf(config, "\t(music      %s)\n", use_music      ? "#t" : "#f");
108       fprintf(config, "\t(show_fps   %s)\n", show_fps       ? "#t" : "#f");
109
110       fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n");
111       fprintf(config, "\t(video      \"%s\")\n", use_gl ? "opengl" : "sdl");
112
113       fprintf(config, "\n\t;; joystick number (-1 means no joystick):\n");
114       fprintf(config, "\t(joystick   %d)\n", use_joystick ? joystick_num : -1);
115       fprintf(config, ")\n");
116     }
117 }
118
119 /* EOF */