]> git.jsancho.org Git - lugaru.git/blob - Source/Settings.cpp
Disabling exception catching when building debug, to ease gdb use
[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
56     crouchkey = SDL_SCANCODE_LSHIFT;
57     jumpkey = SDL_SCANCODE_SPACE;
58     leftkey = SDL_SCANCODE_A;
59     forwardkey = SDL_SCANCODE_W;
60     backkey = SDL_SCANCODE_S;
61     rightkey = SDL_SCANCODE_D;
62     drawkey = SDL_SCANCODE_E;
63     throwkey = SDL_SCANCODE_Q;
64     attackkey = MOUSEBUTTON1;
65     consolekey = SDL_SCANCODE_GRAVE;
66 }
67
68 void SaveSettings()
69 {
70     if (newdetail < 0)
71         newdetail = 0;
72     if (newdetail > 2)
73         newdetail = 2;
74     if (newscreenwidth > 3000)
75         newscreenwidth = screenwidth;
76     if (newscreenwidth < 0)
77         newscreenwidth = screenwidth;
78     if (newscreenheight > 3000)
79         newscreenheight = screenheight;
80     if (newscreenheight < 0)
81         newscreenheight = screenheight;
82     errno = 0;
83     ofstream opstream(Folders::getConfigFilePath());
84     if (opstream.fail()) {
85         perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str());
86         return;
87     }
88     opstream << "Screenwidth:\n";
89     opstream << newscreenwidth;
90     opstream << "\nScreenheight:\n";
91     opstream << newscreenheight;
92     opstream << "\nFullscreen:\n";
93     opstream << fullscreen;
94     opstream << "\nMouse sensitivity:\n";
95     opstream << usermousesensitivity;
96     opstream << "\nBlur(0,1):\n";
97     opstream << ismotionblur;
98     opstream << "\nOverall Detail(0,1,2) higher=better:\n";
99     opstream << newdetail;
100     opstream << "\nFloating jump:\n";
101     opstream << floatjump;
102     opstream << "\nMouse jump:\n";
103     opstream << mousejump;
104     opstream << "\nAmbient sound:\n";
105     opstream << ambientsound;
106     opstream << "\nBlood (0,1,2):\n";
107     opstream << bloodtoggle;
108     opstream << "\nAuto slomo:\n";
109     opstream << autoslomo;
110     opstream << "\nFoliage:\n";
111     opstream << foliage;
112     opstream << "\nMusic:\n";
113     opstream << musictoggle;
114     opstream << "\nTrilinear:\n";
115     opstream << trilinear;
116     opstream << "\nDecals(shadows,blood puddles,etc):\n";
117     opstream << decals;
118     opstream << "\nInvert mouse:\n";
119     opstream << invertmouse;
120     opstream << "\nGamespeed:\n";
121     if (oldgamespeed == 0)
122         oldgamespeed = 1;
123     opstream << oldgamespeed;
124     opstream << "\nDifficulty(0,1,2) higher=harder:\n";
125     opstream << difficulty;
126     opstream << "\nDamage effects(blackout, doublevision):\n";
127     opstream << damageeffects;
128     opstream << "\nText:\n";
129     opstream << texttoggle;
130     opstream << "\nShow Points:\n";
131     opstream << showpoints;
132     opstream << "\nAlways Blur:\n";
133     opstream << alwaysblur;
134     opstream << "\nImmediate mode (turn on on G5):\n";
135     opstream << immediate;
136     opstream << "\nVelocity blur:\n";
137     opstream << velocityblur;
138     opstream << "\nVolume:\n";
139     opstream << volume;
140     opstream << "\nForward key:\n";
141     opstream << forwardkey;
142     opstream << "\nBack key:\n";
143     opstream << backkey;
144     opstream << "\nLeft key:\n";
145     opstream << leftkey;
146     opstream << "\nRight key:\n";
147     opstream << rightkey;
148     opstream << "\nJump key:\n";
149     opstream << jumpkey;
150     opstream << "\nCrouch key:\n";
151     opstream << crouchkey;
152     opstream << "\nDraw key:\n";
153     opstream << drawkey;
154     opstream << "\nThrow key:\n";
155     opstream << throwkey;
156     opstream << "\nAttack key:\n";
157     opstream << attackkey;
158     opstream << "\nConsole key:\n";
159     opstream << consolekey;
160     opstream << "\nDamage bar:\n";
161     opstream << showdamagebar;
162     opstream << "\nStereoMode:\n";
163     opstream << stereomode;
164     opstream << "\nStereoSeparation:\n";
165     opstream << stereoseparation;
166     opstream << "\nStereoReverse:\n";
167     opstream << stereoreverse;
168     opstream.close();
169 }
170
171 bool LoadSettings()
172 {
173     errno = 0;
174     ifstream ipstream(Folders::getConfigFilePath(), std::ios::in);
175     if ( ipstream.fail() ) {
176         perror(("Couldn't read config file " + Folders::getConfigFilePath()).c_str());
177         return false;
178     }
179     char setting[256];
180     char string[256];
181
182     printf("Loading config\n");
183     while (!ipstream.eof()) {
184         ipstream.getline( setting, sizeof(setting) );
185
186         // skip blank lines
187         // assume lines starting with spaces are all blank
188         if ( strlen(setting) == 0 || setting[0] == ' ' || setting[0] == '\t')
189             continue;
190         //~ printf("setting : %s\n",setting);
191
192         if ( ipstream.eof() || ipstream.fail() ) {
193             fprintf(stderr, "Error reading config file: Got setting name '%s', but value can't be read\n", setting);
194             ipstream.close();
195             return false;
196         }
197
198
199         if ( !strncmp(setting, "Screenwidth", 11) ) {
200             ipstream >> kContextWidth;
201         } else if ( !strncmp(setting, "Screenheight", 12) ) {
202             ipstream >> kContextHeight;
203         } else if ( !strncmp(setting, "Fullscreen", 10) ) {
204             ipstream >> fullscreen;
205         } else if ( !strncmp(setting, "Mouse sensitivity", 17) ) {
206             ipstream >> usermousesensitivity;
207         } else if ( !strncmp(setting, "Blur", 4) ) {
208             ipstream >> ismotionblur;
209         } else if ( !strncmp(setting, "Overall Detail", 14) ) {
210             ipstream >> detail;
211         } else if ( !strncmp(setting, "Floating jump", 13) ) {
212             ipstream >> floatjump;
213         } else if ( !strncmp(setting, "Mouse jump", 10) ) {
214             ipstream >> mousejump;
215         } else if ( !strncmp(setting, "Ambient sound", 13) ) {
216             ipstream >> ambientsound;
217         } else if ( !strncmp(setting, "Blood ", 6) ) {
218             ipstream >> bloodtoggle;
219         } else if ( !strncmp(setting, "Auto slomo", 10) ) {
220             ipstream >> autoslomo;
221         } else if ( !strncmp(setting, "Foliage", 7) ) {
222             ipstream >> foliage;
223         } else if ( !strncmp(setting, "Music", 5) ) {
224             ipstream >> musictoggle;
225         } else if ( !strncmp(setting, "Trilinear", 9) ) {
226             ipstream >> trilinear;
227         } else if ( !strncmp(setting, "Decals", 6) ) {
228             ipstream >> decals;
229         } else if ( !strncmp(setting, "Invert mouse", 12) ) {
230             ipstream >> invertmouse;
231         } else if ( !strncmp(setting, "Gamespeed", 9) ) {
232             ipstream >> gamespeed;
233             oldgamespeed = gamespeed;
234             if (oldgamespeed == 0) {
235                 gamespeed = 1;
236                 oldgamespeed = 1;
237             }
238         } else if ( !strncmp(setting, "Difficulty", 10) ) {
239             ipstream >> difficulty;
240         } else if ( !strncmp(setting, "Damage effects", 14) ) {
241             ipstream >> damageeffects;
242         } else if ( !strncmp(setting, "Text", 4) ) {
243             ipstream >> texttoggle;
244         } else if ( !strncmp(setting, "Show Points", 11) ) {
245             ipstream >> showpoints;
246         } else if ( !strncmp(setting, "Always Blur", 11) ) {
247             ipstream >> alwaysblur;
248         } else if ( !strncmp(setting, "Immediate mode ", 15) ) {
249             ipstream >> immediate;
250         } else if ( !strncmp(setting, "Velocity blur", 13) ) {
251             ipstream >> velocityblur;
252         } else if ( !strncmp(setting, "Volume", 6) ) {
253             ipstream >> volume;
254         } else if ( !strncmp(setting, "Forward key", 11) ) {
255             ipstream >> forwardkey;
256         } else if ( !strncmp(setting, "Back key", 8) ) {
257             ipstream >> backkey;
258         } else if ( !strncmp(setting, "Left key", 8) ) {
259             ipstream >> leftkey;
260         } else if ( !strncmp(setting, "Right key", 9) ) {
261             ipstream >> rightkey;
262         } else if ( !strncmp(setting, "Jump key", 8) ) {
263             ipstream >> jumpkey;
264         } else if ( !strncmp(setting, "Crouch key", 10) ) {
265             ipstream >> crouchkey;
266         } else if ( !strncmp(setting, "Draw key", 8) ) {
267             ipstream >> drawkey;
268         } else if ( !strncmp(setting, "Throw key", 9) ) {
269             ipstream >> throwkey;
270         } else if ( !strncmp(setting, "Attack key", 10) ) {
271             ipstream >> attackkey;
272         } else if ( !strncmp(setting, "Console key", 11) ) {
273             ipstream >> consolekey;
274         } else if ( !strncmp(setting, "Damage bar", 10) ) {
275             ipstream >> showdamagebar;
276         } else if ( !strncmp(setting, "StereoMode", 10) ) {
277             int i;
278             ipstream >> i;
279             stereomode = (StereoMode)i;
280         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
281             ipstream >> stereoseparation;
282         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
283             ipstream >> stereoreverse;
284         } else {
285             ipstream >> string;
286             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
287         }
288
289         if ( ipstream.fail() ) {
290             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
291             ipstream.close();
292             return false;
293         }
294
295         if ( ipstream.bad() ) {
296             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
297             ipstream.close();
298             return false;
299         }
300     }
301
302     ipstream.close();
303
304     if (detail > 2)
305         detail = 2;
306     if (detail < 0)
307         detail = 0;
308     if (screenwidth < 0)
309         screenwidth = 1024;
310     if (screenheight < 0)
311         screenheight = 768;
312
313     return true;
314 }