]> git.jsancho.org Git - lugaru.git/blob - Source/Settings.cpp
Add copyright header to source files to match license for project
[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 = 1;
31     ismotionblur = 0;
32     usermousesensitivity = 1;
33     newscreenwidth = kContextWidth = 640;
34     newscreenheight = kContextHeight = 480;
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 = SDLK_LSHIFT;
59     jumpkey = SDLK_SPACE;
60     leftkey = SDLK_a;
61     forwardkey = SDLK_w;
62     backkey = SDLK_s;
63     rightkey = SDLK_d;
64     drawkey = SDLK_e;
65     throwkey = SDLK_q;
66     attackkey = MOUSEBUTTON1;
67     consolekey = SDLK_BACKQUOTE;
68     chatkey = SDLK_t;
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 << "\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 << Input::keyToChar(forwardkey);
142     opstream << "\nBack key:\n";
143     opstream << Input::keyToChar(backkey);
144     opstream << "\nLeft key:\n";
145     opstream << Input::keyToChar(leftkey);
146     opstream << "\nRight key:\n";
147     opstream << Input::keyToChar(rightkey);
148     opstream << "\nJump key:\n";
149     opstream << Input::keyToChar(jumpkey);
150     opstream << "\nCrouch key:\n";
151     opstream << Input::keyToChar(crouchkey);
152     opstream << "\nDraw key:\n";
153     opstream << Input::keyToChar(drawkey);
154     opstream << "\nThrow key:\n";
155     opstream << Input::keyToChar(throwkey);
156     opstream << "\nAttack key:\n";
157     opstream << Input::keyToChar(attackkey);
158     opstream << "\nConsole key:\n";
159     opstream << Input::keyToChar(consolekey);
160     opstream << "\nChat key:\n";
161     opstream << Input::keyToChar(chatkey);
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, "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.getline( string, sizeof(string) );
263             forwardkey = Input::CharToKey(string);
264         } else if ( !strncmp(setting, "Back key", 8) ) {
265             ipstream.getline( string, sizeof(string) );
266             backkey = Input::CharToKey(string);
267         } else if ( !strncmp(setting, "Left key", 8) ) {
268             ipstream.getline( string, sizeof(string) );
269             leftkey = Input::CharToKey(string);
270         } else if ( !strncmp(setting, "Right key", 9) ) {
271             ipstream.getline( string, sizeof(string) );
272             rightkey = Input::CharToKey(string);
273         } else if ( !strncmp(setting, "Jump key", 8) ) {
274             ipstream.getline( string, sizeof(string) );
275             jumpkey = Input::CharToKey(string);
276         } else if ( !strncmp(setting, "Crouch key", 10) ) {
277             ipstream.getline( string, sizeof(string) );
278             crouchkey = Input::CharToKey(string);
279         } else if ( !strncmp(setting, "Draw key", 8) ) {
280             ipstream.getline( string, sizeof(string) );
281             drawkey = Input::CharToKey(string);
282         } else if ( !strncmp(setting, "Throw key", 9) ) {
283             ipstream.getline( string, sizeof(string) );
284             throwkey = Input::CharToKey(string);
285         } else if ( !strncmp(setting, "Attack key", 10) ) {
286             ipstream.getline( string, sizeof(string) );
287             attackkey = Input::CharToKey(string);
288         } else if ( !strncmp(setting, "Console key", 11) ) {
289             ipstream.getline( string, sizeof(string) );
290             consolekey = Input::CharToKey(string);
291         } else if ( !strncmp(setting, "Chat key", 8) ) {
292             ipstream.getline( string, sizeof(string) );
293             chatkey = Input::CharToKey(string);
294         } else if ( !strncmp(setting, "Damage bar", 10) ) {
295             ipstream >> showdamagebar;
296         } else if ( !strncmp(setting, "StereoMode", 10) ) {
297             int i;
298             ipstream >> i;
299             stereomode = (StereoMode)i;
300         } else if ( !strncmp(setting, "StereoSeparation", 16) ) {
301             ipstream >> stereoseparation;
302         } else if ( !strncmp(setting, "StereoReverse", 13) ) {
303             ipstream >> stereoreverse;
304         } else {
305             ipstream >> string;
306             fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
307         }
308
309         if ( ipstream.fail() ) {
310             fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
311             ipstream.close();
312             return false;
313         }
314
315         if ( ipstream.bad() ) {
316             fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
317             ipstream.close();
318             return false;
319         }
320     }
321
322     ipstream.close();
323
324     if (detail > 2)
325         detail = 2;
326     if (detail < 0)
327         detail = 0;
328     if (screenwidth < 0)
329         screenwidth = 640;
330     if (screenheight < 0)
331         screenheight = 480;
332
333     return true;
334 }