]> git.jsancho.org Git - lugaru.git/blob - Source/User/Settings.cpp
fix length for strncmp()
[lugaru.git] / Source / User / 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 "User/Settings.hpp"
22
23 #include "Game.hpp"
24 #include "Utils/Folders.hpp"
25 #include "Utils/Input.hpp"
26
27 using namespace Game;
28
29 void DefaultSettings()
30 {
31     detail = 2;
32     ismotionblur = 1;
33     usermousesensitivity = 1;
34     newscreenwidth = kContextWidth = 1024;
35     newscreenheight = kContextHeight = 768;
36     fullscreen = 0;
37     floatjump = 0;
38     autoslomo = 1;
39     decals = 1;
40     invertmouse = 0;
41     bloodtoggle = 0;
42     foliage = 1;
43     musictoggle = 1;
44     trilinear = 1;
45     gamespeed = 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     devtools = 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 = MOUSEBUTTON_LEFT;
66     consolekey = SDL_SCANCODE_GRAVE;
67
68     newdetail = detail;
69 }
70
71 void SaveSettings()
72 {
73     if (newdetail < 0)
74         newdetail = 0;
75     if (newdetail > 2)
76         newdetail = 2;
77     if (newscreenwidth > 3000)
78         newscreenwidth = screenwidth;
79     if (newscreenwidth < 0)
80         newscreenwidth = screenwidth;
81     if (newscreenheight > 3000)
82         newscreenheight = screenheight;
83     if (newscreenheight < 0)
84         newscreenheight = screenheight;
85     errno = 0;
86     ofstream opstream(Folders::getConfigFilePath());
87     if (opstream.fail()) {
88         perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str());
89         return;
90     }
91     opstream << "Screenwidth:\n";
92     opstream << newscreenwidth;
93     opstream << "\nScreenheight:\n";
94     opstream << newscreenheight;
95     opstream << "\nFullscreen:\n";
96     opstream << fullscreen;
97     opstream << "\nMouse sensitivity:\n";
98     opstream << usermousesensitivity;
99     opstream << "\nBlur(0,1):\n";
100     opstream << ismotionblur;
101     opstream << "\nOverall Detail(0,1,2) higher=better:\n";
102     opstream << newdetail;
103     opstream << "\nFloating jump:\n";
104     opstream << floatjump;
105     opstream << "\nMouse jump:\n";
106     opstream << mousejump;
107     opstream << "\nAmbient sound:\n";
108     opstream << ambientsound;
109     opstream << "\nBlood (0,1,2):\n";
110     opstream << bloodtoggle;
111     opstream << "\nAuto slomo:\n";
112     opstream << autoslomo;
113     opstream << "\nFoliage:\n";
114     opstream << foliage;
115     opstream << "\nMusic:\n";
116     opstream << musictoggle;
117     opstream << "\nTrilinear:\n";
118     opstream << trilinear;
119     opstream << "\nDecals(shadows,blood puddles,etc):\n";
120     opstream << decals;
121     opstream << "\nInvert mouse:\n";
122     opstream << invertmouse;
123     opstream << "\nGamespeed:\n";
124     if (oldgamespeed == 0)
125         oldgamespeed = 1;
126     opstream << oldgamespeed;
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 << "\n";
170     opstream.close();
171 }
172
173 bool LoadSettings()
174 {
175     errno = 0;
176     ifstream ipstream(Folders::getConfigFilePath(), std::ios::in);
177     if ( ipstream.fail() ) {
178         perror(("Couldn't read config file " + Folders::getConfigFilePath()).c_str());
179         return false;
180     }
181     char setting[256];
182     char string[256];
183
184     printf("Loading config\n");
185     while (!ipstream.eof()) {
186         ipstream.getline( setting, sizeof(setting) );
187
188         // skip blank lines
189         // assume lines starting with spaces are all blank
190         if ( strlen(setting) == 0 || setting[0] == ' ' || setting[0] == '\t')
191             continue;
192         //~ printf("setting : %s\n",setting);
193
194         if ( ipstream.eof() || ipstream.fail() ) {
195             fprintf(stderr, "Error reading config file: Got setting name '%s', but value can't be read\n", setting);
196             ipstream.close();
197             return false;
198         }
199
200
201         if ( !strncmp(setting, "Screenwidth", 11) ) {
202             ipstream >> kContextWidth;
203         } else if ( !strncmp(setting, "Screenheight", 12) ) {
204             ipstream >> kContextHeight;
205         } else if ( !strncmp(setting, "Fullscreen", 10) ) {
206             ipstream >> fullscreen;
207         } else if ( !strncmp(setting, "Mouse sensitivity", 17) ) {
208             ipstream >> usermousesensitivity;
209         } else if ( !strncmp(setting, "Blur", 4) ) {
210             ipstream >> ismotionblur;
211         } else if ( !strncmp(setting, "Overall Detail", 14) ) {
212             ipstream >> detail;
213         } else if ( !strncmp(setting, "Floating jump", 13) ) {
214             ipstream >> floatjump;
215         } else if ( !strncmp(setting, "Mouse jump", 10) ) {
216             ipstream >> mousejump;
217         } else if ( !strncmp(setting, "Ambient sound", 13) ) {
218             ipstream >> ambientsound;
219         } else if ( !strncmp(setting, "Blood ", 6) ) {
220             ipstream >> bloodtoggle;
221         } else if ( !strncmp(setting, "Auto slomo", 10) ) {
222             ipstream >> autoslomo;
223         } else if ( !strncmp(setting, "Foliage", 7) ) {
224             ipstream >> foliage;
225         } else if ( !strncmp(setting, "Music", 5) ) {
226             ipstream >> musictoggle;
227         } else if ( !strncmp(setting, "Trilinear", 9) ) {
228             ipstream >> trilinear;
229         } else if ( !strncmp(setting, "Decals", 6) ) {
230             ipstream >> decals;
231         } else if ( !strncmp(setting, "Invert mouse", 12) ) {
232             ipstream >> invertmouse;
233         } else if ( !strncmp(setting, "Gamespeed", 9) ) {
234             ipstream >> gamespeed;
235             oldgamespeed = gamespeed;
236             if (oldgamespeed == 0) {
237                 gamespeed = 1;
238                 oldgamespeed = 1;
239             }
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, "Devtools", 6) ) {
245             ipstream >> devtools;
246         } else if ( !strncmp(setting, "Show Points", 11) ) {
247             ipstream >> showpoints;
248         } else if ( !strncmp(setting, "Always Blur", 11) ) {
249             ipstream >> alwaysblur;
250         } else if ( !strncmp(setting, "Immediate mode ", 15) ) {
251             ipstream >> immediate;
252         } else if ( !strncmp(setting, "Velocity blur", 13) ) {
253             ipstream >> velocityblur;
254         } else if ( !strncmp(setting, "Volume", 6) ) {
255             ipstream >> volume;
256         } else if ( !strncmp(setting, "Forward key", 11) ) {
257             ipstream >> forwardkey;
258         } else if ( !strncmp(setting, "Back key", 8) ) {
259             ipstream >> backkey;
260         } else if ( !strncmp(setting, "Left key", 8) ) {
261             ipstream >> leftkey;
262         } else if ( !strncmp(setting, "Right key", 9) ) {
263             ipstream >> rightkey;
264         } else if ( !strncmp(setting, "Jump key", 8) ) {
265             ipstream >> jumpkey;
266         } else if ( !strncmp(setting, "Crouch key", 10) ) {
267             ipstream >> crouchkey;
268         } else if ( !strncmp(setting, "Draw key", 8) ) {
269             ipstream >> drawkey;
270         } else if ( !strncmp(setting, "Throw key", 9) ) {
271             ipstream >> throwkey;
272         } else if ( !strncmp(setting, "Attack key", 10) ) {
273             ipstream >> attackkey;
274         } else if ( !strncmp(setting, "Console key", 11) ) {
275             ipstream >> consolekey;
276         } else if ( !strncmp(setting, "Damage bar", 10) ) {
277             ipstream >> showdamagebar;
278         } else if ( !strncmp(setting, "StereoMode", 10) ) {
279             int i;
280             ipstream >> i;
281             stereomode = (StereoMode)i;
282         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
283             ipstream >> stereoseparation;
284         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
285             ipstream >> stereoreverse;
286         } else {
287             ipstream >> string;
288             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
289         }
290
291         if ( ipstream.fail() ) {
292             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
293             ipstream.close();
294             return false;
295         }
296
297         if ( ipstream.bad() ) {
298             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
299             ipstream.close();
300             return false;
301         }
302     }
303
304     ipstream.close();
305
306     if (detail > 2)
307         detail = 2;
308     if (detail < 0)
309         detail = 0;
310     if (screenwidth < 0)
311         screenwidth = 1024;
312     if (screenheight < 0)
313         screenheight = 768;
314
315     newdetail = detail;
316     return true;
317 }