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