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