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