]> git.jsancho.org Git - lugaru.git/blob - Source/Settings.cpp
Reading debug mode from config again, but command line option overrides it
[lugaru.git] / Source / Settings.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Settings.h"
22 #include "Game.h"
23 #include "Input.h"
24 #include "Utils/Folders.h"
25
26 using namespace Game;
27
28 void DefaultSettings()
29 {
30     detail = 2;
31     ismotionblur = 1;
32     usermousesensitivity = 1;
33     newscreenwidth = kContextWidth = 1024;
34     newscreenheight = kContextHeight = 768;
35     fullscreen = 0;
36     floatjump = 0;
37     autoslomo = 1;
38     decals = 1;
39     invertmouse = 0;
40     bloodtoggle = 0;
41     foliage = 1;
42     musictoggle = 1;
43     trilinear = 1;
44     gamespeed = 1;
45     difficulty = 1;
46     damageeffects = 0;
47     texttoggle = 1;
48     alwaysblur = 0;
49     showpoints = 0;
50     showdamagebar = 0;
51     immediate = 0;
52     velocityblur = 0;
53     volume = 0.8f;
54     ambientsound = 1;
55     debugmode = 0;
56
57     crouchkey = SDL_SCANCODE_LSHIFT;
58     jumpkey = SDL_SCANCODE_SPACE;
59     leftkey = SDL_SCANCODE_A;
60     forwardkey = SDL_SCANCODE_W;
61     backkey = SDL_SCANCODE_S;
62     rightkey = SDL_SCANCODE_D;
63     drawkey = SDL_SCANCODE_E;
64     throwkey = SDL_SCANCODE_Q;
65     attackkey = MOUSEBUTTON1;
66     consolekey = SDL_SCANCODE_GRAVE;
67 }
68
69 void SaveSettings()
70 {
71     if (newdetail < 0)
72         newdetail = 0;
73     if (newdetail > 2)
74         newdetail = 2;
75     if (newscreenwidth > 3000)
76         newscreenwidth = screenwidth;
77     if (newscreenwidth < 0)
78         newscreenwidth = screenwidth;
79     if (newscreenheight > 3000)
80         newscreenheight = screenheight;
81     if (newscreenheight < 0)
82         newscreenheight = screenheight;
83     errno = 0;
84     ofstream opstream(Folders::getConfigFilePath());
85     if (opstream.fail()) {
86         perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str());
87         return;
88     }
89     opstream << "Screenwidth:\n";
90     opstream << newscreenwidth;
91     opstream << "\nScreenheight:\n";
92     opstream << newscreenheight;
93     opstream << "\nFullscreen:\n";
94     opstream << fullscreen;
95     opstream << "\nMouse sensitivity:\n";
96     opstream << usermousesensitivity;
97     opstream << "\nBlur(0,1):\n";
98     opstream << ismotionblur;
99     opstream << "\nOverall Detail(0,1,2) higher=better:\n";
100     opstream << newdetail;
101     opstream << "\nFloating jump:\n";
102     opstream << floatjump;
103     opstream << "\nMouse jump:\n";
104     opstream << mousejump;
105     opstream << "\nAmbient sound:\n";
106     opstream << ambientsound;
107     opstream << "\nBlood (0,1,2):\n";
108     opstream << bloodtoggle;
109     opstream << "\nAuto slomo:\n";
110     opstream << autoslomo;
111     opstream << "\nFoliage:\n";
112     opstream << foliage;
113     opstream << "\nMusic:\n";
114     opstream << musictoggle;
115     opstream << "\nTrilinear:\n";
116     opstream << trilinear;
117     opstream << "\nDecals(shadows,blood puddles,etc):\n";
118     opstream << decals;
119     opstream << "\nInvert mouse:\n";
120     opstream << invertmouse;
121     opstream << "\nGamespeed:\n";
122     if (oldgamespeed == 0)
123         oldgamespeed = 1;
124     opstream << oldgamespeed;
125     opstream << "\nDifficulty(0,1,2) higher=harder:\n";
126     opstream << difficulty;
127     opstream << "\nDamage effects(blackout, doublevision):\n";
128     opstream << damageeffects;
129     opstream << "\nText:\n";
130     opstream << texttoggle;
131     opstream << "\nShow Points:\n";
132     opstream << showpoints;
133     opstream << "\nAlways Blur:\n";
134     opstream << alwaysblur;
135     opstream << "\nImmediate mode (turn on on G5):\n";
136     opstream << immediate;
137     opstream << "\nVelocity blur:\n";
138     opstream << velocityblur;
139     opstream << "\nVolume:\n";
140     opstream << volume;
141     opstream << "\nForward key:\n";
142     opstream << forwardkey;
143     opstream << "\nBack key:\n";
144     opstream << backkey;
145     opstream << "\nLeft key:\n";
146     opstream << leftkey;
147     opstream << "\nRight key:\n";
148     opstream << rightkey;
149     opstream << "\nJump key:\n";
150     opstream << jumpkey;
151     opstream << "\nCrouch key:\n";
152     opstream << crouchkey;
153     opstream << "\nDraw key:\n";
154     opstream << drawkey;
155     opstream << "\nThrow key:\n";
156     opstream << throwkey;
157     opstream << "\nAttack key:\n";
158     opstream << attackkey;
159     opstream << "\nConsole key:\n";
160     opstream << consolekey;
161     opstream << "\nDamage bar:\n";
162     opstream << showdamagebar;
163     opstream << "\nStereoMode:\n";
164     opstream << stereomode;
165     opstream << "\nStereoSeparation:\n";
166     opstream << stereoseparation;
167     opstream << "\nStereoReverse:\n";
168     opstream << stereoreverse;
169     opstream.close();
170 }
171
172 bool LoadSettings()
173 {
174     errno = 0;
175     ifstream ipstream(Folders::getConfigFilePath(), std::ios::in);
176     if ( ipstream.fail() ) {
177         perror(("Couldn't read config file " + Folders::getConfigFilePath()).c_str());
178         return false;
179     }
180     char setting[256];
181     char string[256];
182
183     printf("Loading config\n");
184     while (!ipstream.eof()) {
185         ipstream.getline( setting, sizeof(setting) );
186
187         // skip blank lines
188         // assume lines starting with spaces are all blank
189         if ( strlen(setting) == 0 || setting[0] == ' ' || setting[0] == '\t')
190             continue;
191         //~ printf("setting : %s\n",setting);
192
193         if ( ipstream.eof() || ipstream.fail() ) {
194             fprintf(stderr, "Error reading config file: Got setting name '%s', but value can't be read\n", setting);
195             ipstream.close();
196             return false;
197         }
198
199
200         if ( !strncmp(setting, "Screenwidth", 11) ) {
201             ipstream >> kContextWidth;
202         } else if ( !strncmp(setting, "Screenheight", 12) ) {
203             ipstream >> kContextHeight;
204         } else if ( !strncmp(setting, "Fullscreen", 10) ) {
205             ipstream >> fullscreen;
206         } else if ( !strncmp(setting, "Mouse sensitivity", 17) ) {
207             ipstream >> usermousesensitivity;
208         } else if ( !strncmp(setting, "Blur", 4) ) {
209             ipstream >> ismotionblur;
210         } else if ( !strncmp(setting, "Overall Detail", 14) ) {
211             ipstream >> detail;
212         } else if ( !strncmp(setting, "Floating jump", 13) ) {
213             ipstream >> floatjump;
214         } else if ( !strncmp(setting, "Mouse jump", 10) ) {
215             ipstream >> mousejump;
216         } else if ( !strncmp(setting, "Ambient sound", 13) ) {
217             ipstream >> ambientsound;
218         } else if ( !strncmp(setting, "Blood ", 6) ) {
219             ipstream >> bloodtoggle;
220         } else if ( !strncmp(setting, "Auto slomo", 10) ) {
221             ipstream >> autoslomo;
222         } else if ( !strncmp(setting, "Foliage", 7) ) {
223             ipstream >> foliage;
224         } else if ( !strncmp(setting, "Music", 5) ) {
225             ipstream >> musictoggle;
226         } else if ( !strncmp(setting, "Trilinear", 9) ) {
227             ipstream >> trilinear;
228         } else if ( !strncmp(setting, "Decals", 6) ) {
229             ipstream >> decals;
230         } else if ( !strncmp(setting, "Invert mouse", 12) ) {
231             ipstream >> invertmouse;
232         } else if ( !strncmp(setting, "Gamespeed", 9) ) {
233             ipstream >> gamespeed;
234             oldgamespeed = gamespeed;
235             if (oldgamespeed == 0) {
236                 gamespeed = 1;
237                 oldgamespeed = 1;
238             }
239         } else if ( !strncmp(setting, "Difficulty", 10) ) {
240             ipstream >> difficulty;
241         } else if ( !strncmp(setting, "Damage effects", 14) ) {
242             ipstream >> damageeffects;
243         } else if ( !strncmp(setting, "Text", 4) ) {
244             ipstream >> texttoggle;
245         } else if ( !strncmp(setting, "Debug", 5) ) {
246             ipstream >> debugmode;
247         } else if ( !strncmp(setting, "Show Points", 11) ) {
248             ipstream >> showpoints;
249         } else if ( !strncmp(setting, "Always Blur", 11) ) {
250             ipstream >> alwaysblur;
251         } else if ( !strncmp(setting, "Immediate mode ", 15) ) {
252             ipstream >> immediate;
253         } else if ( !strncmp(setting, "Velocity blur", 13) ) {
254             ipstream >> velocityblur;
255         } else if ( !strncmp(setting, "Volume", 6) ) {
256             ipstream >> volume;
257         } else if ( !strncmp(setting, "Forward key", 11) ) {
258             ipstream >> forwardkey;
259         } else if ( !strncmp(setting, "Back key", 8) ) {
260             ipstream >> backkey;
261         } else if ( !strncmp(setting, "Left key", 8) ) {
262             ipstream >> leftkey;
263         } else if ( !strncmp(setting, "Right key", 9) ) {
264             ipstream >> rightkey;
265         } else if ( !strncmp(setting, "Jump key", 8) ) {
266             ipstream >> jumpkey;
267         } else if ( !strncmp(setting, "Crouch key", 10) ) {
268             ipstream >> crouchkey;
269         } else if ( !strncmp(setting, "Draw key", 8) ) {
270             ipstream >> drawkey;
271         } else if ( !strncmp(setting, "Throw key", 9) ) {
272             ipstream >> throwkey;
273         } else if ( !strncmp(setting, "Attack key", 10) ) {
274             ipstream >> attackkey;
275         } else if ( !strncmp(setting, "Console key", 11) ) {
276             ipstream >> consolekey;
277         } else if ( !strncmp(setting, "Damage bar", 10) ) {
278             ipstream >> showdamagebar;
279         } else if ( !strncmp(setting, "StereoMode", 10) ) {
280             int i;
281             ipstream >> i;
282             stereomode = (StereoMode)i;
283         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
284             ipstream >> stereoseparation;
285         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
286             ipstream >> stereoreverse;
287         } else {
288             ipstream >> string;
289             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
290         }
291
292         if ( ipstream.fail() ) {
293             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
294             ipstream.close();
295             return false;
296         }
297
298         if ( ipstream.bad() ) {
299             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
300             ipstream.close();
301             return false;
302         }
303     }
304
305     ipstream.close();
306
307     if (detail > 2)
308         detail = 2;
309     if (detail < 0)
310         detail = 0;
311     if (screenwidth < 0)
312         screenwidth = 1024;
313     if (screenheight < 0)
314         screenheight = 768;
315
316     return true;
317 }