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