]> git.jsancho.org Git - lugaru.git/blob - Source/User/Settings.cpp
Sorted all source files in folders
[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 "Game.h"
22 #include "User/Settings.h"
23 #include "Utils/Folders.h"
24 #include "Utils/Input.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     damageeffects = 0;
46     texttoggle = 1;
47     alwaysblur = 0;
48     showpoints = 0;
49     showdamagebar = 0;
50     immediate = 0;
51     velocityblur = 0;
52     volume = 0.8f;
53     ambientsound = 1;
54     devtools = 0;
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     newdetail = detail;
68 }
69
70 void SaveSettings()
71 {
72     if (newdetail < 0)
73         newdetail = 0;
74     if (newdetail > 2)
75         newdetail = 2;
76     if (newscreenwidth > 3000)
77         newscreenwidth = screenwidth;
78     if (newscreenwidth < 0)
79         newscreenwidth = screenwidth;
80     if (newscreenheight > 3000)
81         newscreenheight = screenheight;
82     if (newscreenheight < 0)
83         newscreenheight = screenheight;
84     errno = 0;
85     ofstream opstream(Folders::getConfigFilePath());
86     if (opstream.fail()) {
87         perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str());
88         return;
89     }
90     opstream << "Screenwidth:\n";
91     opstream << newscreenwidth;
92     opstream << "\nScreenheight:\n";
93     opstream << newscreenheight;
94     opstream << "\nFullscreen:\n";
95     opstream << fullscreen;
96     opstream << "\nMouse sensitivity:\n";
97     opstream << usermousesensitivity;
98     opstream << "\nBlur(0,1):\n";
99     opstream << ismotionblur;
100     opstream << "\nOverall Detail(0,1,2) higher=better:\n";
101     opstream << newdetail;
102     opstream << "\nFloating jump:\n";
103     opstream << floatjump;
104     opstream << "\nMouse jump:\n";
105     opstream << mousejump;
106     opstream << "\nAmbient sound:\n";
107     opstream << ambientsound;
108     opstream << "\nBlood (0,1,2):\n";
109     opstream << bloodtoggle;
110     opstream << "\nAuto slomo:\n";
111     opstream << autoslomo;
112     opstream << "\nFoliage:\n";
113     opstream << foliage;
114     opstream << "\nMusic:\n";
115     opstream << musictoggle;
116     opstream << "\nTrilinear:\n";
117     opstream << trilinear;
118     opstream << "\nDecals(shadows,blood puddles,etc):\n";
119     opstream << decals;
120     opstream << "\nInvert mouse:\n";
121     opstream << invertmouse;
122     opstream << "\nGamespeed:\n";
123     if (oldgamespeed == 0)
124         oldgamespeed = 1;
125     opstream << oldgamespeed;
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 << "\n";
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, "Damage effects", 14) ) {
240             ipstream >> damageeffects;
241         } else if ( !strncmp(setting, "Text", 4) ) {
242             ipstream >> texttoggle;
243         } else if ( !strncmp(setting, "Devtools", 5) ) {
244             ipstream >> devtools;
245         } else if ( !strncmp(setting, "Show Points", 11) ) {
246             ipstream >> showpoints;
247         } else if ( !strncmp(setting, "Always Blur", 11) ) {
248             ipstream >> alwaysblur;
249         } else if ( !strncmp(setting, "Immediate mode ", 15) ) {
250             ipstream >> immediate;
251         } else if ( !strncmp(setting, "Velocity blur", 13) ) {
252             ipstream >> velocityblur;
253         } else if ( !strncmp(setting, "Volume", 6) ) {
254             ipstream >> volume;
255         } else if ( !strncmp(setting, "Forward key", 11) ) {
256             ipstream >> forwardkey;
257         } else if ( !strncmp(setting, "Back key", 8) ) {
258             ipstream >> backkey;
259         } else if ( !strncmp(setting, "Left key", 8) ) {
260             ipstream >> leftkey;
261         } else if ( !strncmp(setting, "Right key", 9) ) {
262             ipstream >> rightkey;
263         } else if ( !strncmp(setting, "Jump key", 8) ) {
264             ipstream >> jumpkey;
265         } else if ( !strncmp(setting, "Crouch key", 10) ) {
266             ipstream >> crouchkey;
267         } else if ( !strncmp(setting, "Draw key", 8) ) {
268             ipstream >> drawkey;
269         } else if ( !strncmp(setting, "Throw key", 9) ) {
270             ipstream >> throwkey;
271         } else if ( !strncmp(setting, "Attack key", 10) ) {
272             ipstream >> attackkey;
273         } else if ( !strncmp(setting, "Console key", 11) ) {
274             ipstream >> consolekey;
275         } else if ( !strncmp(setting, "Damage bar", 10) ) {
276             ipstream >> showdamagebar;
277         } else if ( !strncmp(setting, "StereoMode", 10) ) {
278             int i;
279             ipstream >> i;
280             stereomode = (StereoMode)i;
281         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
282             ipstream >> stereoseparation;
283         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
284             ipstream >> stereoreverse;
285         } else {
286             ipstream >> string;
287             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
288         }
289
290         if ( ipstream.fail() ) {
291             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
292             ipstream.close();
293             return false;
294         }
295
296         if ( ipstream.bad() ) {
297             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
298             ipstream.close();
299             return false;
300         }
301     }
302
303     ipstream.close();
304
305     if (detail > 2)
306         detail = 2;
307     if (detail < 0)
308         detail = 0;
309     if (screenwidth < 0)
310         screenwidth = 1024;
311     if (screenheight < 0)
312         screenheight = 768;
313
314     newdetail = detail;
315     return true;
316 }