1 /*
2  * Config.cpp
3  * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include <fstream>
21 #include <iostream>
22 #include <cstdlib>
23 #include <cstring>
24 
25 #include <limits.h>
26 
27 #include "Config.h"
28 
Config()29 Config::Config()
30 {
31   std::cout << "instantiating config class" << std::endl;
32   thirdperson = nocolors = visions = 0;
33 
34   screenwidth = 640;
35   screenheight = 480;
36   fullscreen = 0;
37   grabmouse = 0;
38   usermousesensitivity = .7;
39 #ifdef DEBUG
40   debug = 1;
41 #else
42   debug = 0;
43 #endif
44   vblsync = 1;
45   blood = 1;
46   blurness = 0;
47   mainmenuness = 1;
48   customlevels = 0;
49   musictoggle = 1;
50   azertykeyboard = 0;
51 
52   char cfgFile[PATH_MAX];
53   strcpy(cfgFile, getenv("HOME"));
54   strcat(cfgFile, "/.blackshades.config");
55 
56   if(!ConfigExist(cfgFile)) {
57     WriteConfig(cfgFile);
58   }
59 
60   ReadConfig(cfgFile);
61 
62   //highscore
63   hsFile = "Data/Highscore";
64   highscore = 0;
65   beatgame = 0;
66 
67   ReadHighScore();
68 }
69 
ConfigExist(const char * configFile)70 bool Config::ConfigExist(const char *configFile)
71 {
72   std::ifstream ipstream(configFile);
73 
74   if(ipstream) {
75     ipstream.close();
76     return true;
77   }
78 
79   return false;
80 }
81 
ReadConfig(const char * configFile)82 bool Config::ReadConfig(const char *configFile)
83 {
84   //Read config
85   std::ifstream ipstream(configFile);
86 
87   if(ipstream) {
88     ipstream.ignore(256, '\n');
89     ipstream >> screenwidth;
90     ipstream.ignore(256, '\n');
91     ipstream.ignore(256, '\n');
92     ipstream >> screenheight;
93     ipstream.ignore(256, '\n');
94     ipstream.ignore(256, '\n');
95     ipstream >> fullscreen;
96     ipstream.ignore(256, '\n');
97     ipstream.ignore(256, '\n');
98     ipstream >> grabmouse;
99     ipstream.ignore(256, '\n');
100     ipstream.ignore(256, '\n');
101     ipstream >> usermousesensitivity;
102     ipstream.ignore(256, '\n');
103     ipstream.ignore(256, '\n');
104     ipstream >> debug;
105     ipstream.ignore(256, '\n');
106     ipstream.ignore(256, '\n');
107     ipstream >> vblsync;
108     ipstream.ignore(256, '\n');
109     ipstream.ignore(256, '\n');
110     ipstream >> blood;
111     ipstream.ignore(256, '\n');
112     ipstream.ignore(256, '\n');
113     ipstream >> blurness;
114     ipstream.ignore(256, '\n');
115     ipstream.ignore(256, '\n');
116     ipstream >> mainmenuness;
117     ipstream.ignore(256, '\n');
118     ipstream.ignore(256, '\n');
119     ipstream >> customlevels;
120     ipstream.ignore(256, '\n');
121     ipstream.ignore(256, '\n');
122     ipstream >> musictoggle;
123     ipstream.ignore(256, '\n');
124     ipstream.ignore(256, '\n');
125     ipstream >> azertykeyboard;
126     ipstream.close();
127     std::cout << "debug: " << debug << std::endl;
128     return true;
129   }
130 
131   return false;
132 }
133 
WriteConfig(const char * configFile)134 void Config::WriteConfig(const char *configFile)
135 {
136   std::ofstream opstream(configFile);
137   opstream << "Screenwidth:\n";
138   opstream << screenwidth;
139   opstream << "\nScreenheight:\n";
140   opstream << screenheight;
141   opstream << "\nFullscreen:\n";
142   opstream << fullscreen;
143   opstream << "\nGrab mouse:\n";
144   opstream << grabmouse;
145   opstream << "\nMouse sensitivity:\n";
146   opstream << usermousesensitivity;
147   opstream << "\nShow fps and other info:\n";
148   opstream << debug;
149   opstream << "\nVBL sync:\n";
150   opstream << vblsync;
151   opstream << "\nBlood:\n";
152   opstream << blood;
153   opstream << "\nBlur:\n";
154   opstream << blurness;
155   opstream << "\nMain Menu:\n";
156   opstream << mainmenuness;
157   opstream << "\nCustom levels:\n";
158   opstream << customlevels;
159   opstream << "\nMusic:\n";
160   opstream << musictoggle;
161   opstream << "\nazerty keyboard:\n";
162   opstream << azertykeyboard;
163   opstream.close();
164 }
165 
ReadHighScore()166 bool Config::ReadHighScore()
167 {
168   //Read high score
169   std::ifstream ipstream(hsFile);
170   if(!ipstream) {
171     WriteHighScore(0);
172   }
173 
174   if(ipstream) {
175     ipstream >> highscore;
176     ipstream.ignore(256, '\n');
177     ipstream >> beatgame;
178     ipstream.close();
179     return true;
180   }
181   return false;
182 }
183 
WriteHighScore(int score)184 void Config::WriteHighScore(int score)
185 {
186   std::ifstream ipstream(hsFile);
187   if(!ipstream || score > highscore) {
188     std::ofstream opstream(hsFile);
189     highscore = score;
190     opstream << highscore;
191     opstream << "\n";
192     opstream << beatgame;
193     opstream.close();
194   }
195 }
196