]> git.jsancho.org Git - lugaru.git/blob - Source/Settings.cpp
Migrated to SDL2
[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 = 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     chatkey = SDL_SCANCODE_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 << 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 << "\nChat key:\n";
161     opstream << 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 >> 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, "Chat key", 8) ) {
282             ipstream >> chatkey;
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 = 640;
319     if (screenheight < 0)
320         screenheight = 480;
321
322     return true;
323 }