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