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