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