]> git.jsancho.org Git - lugaru.git/blob - Source/Settings.cpp
License: Update GPLv2+ header to match current FSF recommendation
[lugaru.git] / Source / Settings.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Settings.h"
21 #include "Game.h"
22 #include "Input.h"
23
24 using namespace Game;
25
26 void DefaultSettings()
27 {
28     detail = 2;
29     ismotionblur = 1;
30     usermousesensitivity = 1;
31     newscreenwidth = kContextWidth = 1024;
32     newscreenheight = kContextHeight = 768;
33     fullscreen = 0;
34     kBitsPerPixel = 32;
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     vblsync = 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(ConvertFileName(":Data:config.txt", "w"));
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 << "\nVBL Sync:\n";
129     opstream << vblsync;
130     opstream << "\nShow Points:\n";
131     opstream << showpoints;
132     opstream << "\nAlways Blur:\n";
133     opstream << alwaysblur;
134     opstream << "\nImmediate mode (turn on on G5):\n";
135     opstream << immediate;
136     opstream << "\nVelocity blur:\n";
137     opstream << velocityblur;
138     opstream << "\nVolume:\n";
139     opstream << volume;
140     opstream << "\nForward key:\n";
141     opstream << forwardkey;
142     opstream << "\nBack key:\n";
143     opstream << backkey;
144     opstream << "\nLeft key:\n";
145     opstream << leftkey;
146     opstream << "\nRight key:\n";
147     opstream << rightkey;
148     opstream << "\nJump key:\n";
149     opstream << jumpkey;
150     opstream << "\nCrouch key:\n";
151     opstream << crouchkey;
152     opstream << "\nDraw key:\n";
153     opstream << drawkey;
154     opstream << "\nThrow key:\n";
155     opstream << throwkey;
156     opstream << "\nAttack key:\n";
157     opstream << attackkey;
158     opstream << "\nConsole key:\n";
159     opstream << consolekey;
160     opstream << "\nDamage bar:\n";
161     opstream << showdamagebar;
162     opstream << "\nStereoMode:\n";
163     opstream << stereomode;
164     opstream << "\nStereoSeparation:\n";
165     opstream << stereoseparation;
166     opstream << "\nStereoReverse:\n";
167     opstream << stereoreverse;
168     opstream.close();
169 }
170
171 bool LoadSettings()
172 {
173     ifstream ipstream(ConvertFileName(":Data:config.txt"), std::ios::in);
174     if ( !ipstream || ipstream.fail() ) {
175         printf("Config file not found\n");
176         return false;
177     }
178     char setting[256];
179     char string[256];
180
181     printf("Loading config\n");
182     while (!ipstream.eof()) {
183         ipstream.getline( setting, sizeof(setting) );
184
185         // skip blank lines
186         // assume lines starting with spaces are all blank
187         if ( strlen(setting) == 0 || setting[0] == ' ' || setting[0] == '\t')
188             continue;
189         //~ printf("setting : %s\n",setting);
190
191         if ( ipstream.eof() || ipstream.fail() ) {
192             fprintf(stderr, "Error reading config file: Got setting name '%s', but value can't be read\n", setting);
193             ipstream.close();
194             return false;
195         }
196
197
198         if ( !strncmp(setting, "Screenwidth", 11) ) {
199             ipstream >> kContextWidth;
200         } else if ( !strncmp(setting, "Screenheight", 12) ) {
201             ipstream >> kContextHeight;
202         } else if ( !strncmp(setting, "Fullscreen", 10) ) {
203             ipstream >> fullscreen;
204         } else if ( !strncmp(setting, "Mouse sensitivity", 17) ) {
205             ipstream >> usermousesensitivity;
206         } else if ( !strncmp(setting, "Blur", 4) ) {
207             ipstream >> ismotionblur;
208         } else if ( !strncmp(setting, "Overall Detail", 14) ) {
209             ipstream >> detail;
210             if (detail != 0)
211                 kBitsPerPixel = 32;
212             else
213                 kBitsPerPixel = 16;
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, "VBL Sync", 8) ) {
250             ipstream >> vblsync;
251         } else if ( !strncmp(setting, "Show Points", 11) ) {
252             ipstream >> showpoints;
253         } else if ( !strncmp(setting, "Always Blur", 11) ) {
254             ipstream >> alwaysblur;
255         } else if ( !strncmp(setting, "Immediate mode ", 15) ) {
256             ipstream >> immediate;
257         } else if ( !strncmp(setting, "Velocity blur", 13) ) {
258             ipstream >> velocityblur;
259         } else if ( !strncmp(setting, "Volume", 6) ) {
260             ipstream >> volume;
261         } else if ( !strncmp(setting, "Forward key", 11) ) {
262             ipstream >> forwardkey;
263         } else if ( !strncmp(setting, "Back key", 8) ) {
264             ipstream >> backkey;
265         } else if ( !strncmp(setting, "Left key", 8) ) {
266             ipstream >> leftkey;
267         } else if ( !strncmp(setting, "Right key", 9) ) {
268             ipstream >> rightkey;
269         } else if ( !strncmp(setting, "Jump key", 8) ) {
270             ipstream >> jumpkey;
271         } else if ( !strncmp(setting, "Crouch key", 10) ) {
272             ipstream >> crouchkey;
273         } else if ( !strncmp(setting, "Draw key", 8) ) {
274             ipstream >> drawkey;
275         } else if ( !strncmp(setting, "Throw key", 9) ) {
276             ipstream >> throwkey;
277         } else if ( !strncmp(setting, "Attack key", 10) ) {
278             ipstream >> attackkey;
279         } else if ( !strncmp(setting, "Console key", 11) ) {
280             ipstream >> consolekey;
281         } else if ( !strncmp(setting, "Damage bar", 10) ) {
282             ipstream >> showdamagebar;
283         } else if ( !strncmp(setting, "StereoMode", 10) ) {
284             int i;
285             ipstream >> i;
286             stereomode = (StereoMode)i;
287         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
288             ipstream >> stereoseparation;
289         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
290             ipstream >> stereoreverse;
291         } else {
292             ipstream >> string;
293             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
294         }
295
296         if ( ipstream.fail() ) {
297             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
298             ipstream.close();
299             return false;
300         }
301
302         if ( ipstream.bad() ) {
303             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
304             ipstream.close();
305             return false;
306         }
307     }
308
309     ipstream.close();
310
311     if (detail > 2)
312         detail = 2;
313     if (detail < 0)
314         detail = 0;
315     if (screenwidth < 0)
316         screenwidth = 1024;
317     if (screenheight < 0)
318         screenheight = 768;
319
320     return true;
321 }