1 #include <Config/Globals.hpp> 2 #include <Engine/EngineGlobals.hpp> 3 #include <Engine/Helpers/INI.hpp> 4 #include <Engine/Helpers/File.hpp> 5 #include <Engine/Helpers/String.hpp> 6 #include <Engine/InputManager.hpp> 7 #include <Entities/BoardParser.hpp> 8 #include <Entities/ScoreFile.hpp> 24 std::string Globals::Config::directory =
"";
25 std::string Globals::Config::file =
"";
26 std::string Globals::Config::scoresFile =
"";
28 unsigned int Globals::Game::starting_speed = 1;
29 int Globals::Game::fruits_at_once = 1;
30 bool Globals::Game::random_walls =
false;
31 bool Globals::Game::teleport =
false;
32 std::string Globals::Game::current_level =
"";
34 ColorPair Globals::Theme::player_head;
35 ColorPair Globals::Theme::player_head_dead;
36 ColorPair Globals::Theme::player_body;
37 ColorPair Globals::Theme::fruit;
39 Globals::Game::BoardSize Globals::Game::board_size = LARGE;
41 Globals::Game::BoardSize Globals::Game::intToBoardSize(
int val)
44 return Globals::Game::SMALL;
47 return Globals::Game::MEDIUM;
49 return Globals::Game::LARGE;
51 int Globals::Game::boardSizeToInt(Globals::Game::BoardSize size)
53 if (size == Globals::Game::SMALL)
56 if (size == Globals::Game::MEDIUM)
62 int Globals::Game::board_scroll_delay = 1000;
64 bool Globals::Game::board_scroll_up =
false;
65 bool Globals::Game::board_scroll_down =
false;
66 bool Globals::Game::board_scroll_left =
false;
67 bool Globals::Game::board_scroll_right =
false;
69 bool Globals::Error::has_config_file =
true;
70 bool Globals::Error::has_score_file =
true;
71 bool Globals::Error::old_version_score_file =
false;
72 bool Globals::Error::strange_score_file =
false;
83 Globals::Config::directory = (Utils::File::getHome() +
87 if (Utils::String::front(Globals::Config::directory) !=
'/')
91 Globals::Config::directory = (
"/tmp/" +
92 Globals::Config::directory);
95 Globals::Config::file = (Globals::Config::directory +
98 Globals::Config::scoresFile = (Globals::Config::directory +
99 "arcade.nsnakescores");
101 if (! Utils::File::isDirectory(Globals::Config::directory))
102 Utils::File::mkdir_p(Globals::Config::directory);
104 if (! Utils::File::isDirectory(Globals::Config::directory))
108 Globals::Config::directory =
"/dev/";
109 Globals::Config::file =
"/dev/null";
114 InputManager::bind(
"left", KEY_LEFT);
115 InputManager::bind(
"right", KEY_RIGHT);
116 InputManager::bind(
"up", KEY_UP);
117 InputManager::bind(
"down", KEY_DOWN);
118 InputManager::bind(
"pause",
'p');
119 InputManager::bind(
"help",
'h');
120 InputManager::bind(
"quit",
'q');
124 Globals::Theme::player_head = Colors::pair(
"green",
"default",
true);
125 Globals::Theme::player_head_dead = Colors::pair(
"red",
"default",
true);
126 Globals::Theme::player_body = Colors::pair(
"green",
"default",
true);
128 Globals::Theme::fruit = Colors::pair(
"red",
"default",
true);
144 if (! Utils::File::exists(Globals::Config::file))
147 INI::Parser* ini = NULL;
150 ini =
new INI::Parser(Globals::Config::file);
152 catch(std::runtime_error& e)
161 std::string buffer =
"";
171 #define INI_GET(var, out, in) \ 173 buffer = (* ini)(out)[in]; \ 174 if (! buffer.empty()) \ 176 Utils::String::convert(buffer, var); \ 180 INI_GET(EngineGlobals::Screen::center_horizontally,
"screen",
"center_horizontal");
181 INI_GET(EngineGlobals::Screen::center_vertically,
"screen",
"center_vertical");
183 INI_GET(EngineGlobals::Screen::show_borders,
"screen",
"borders");
184 INI_GET(EngineGlobals::Screen::fancy_borders,
"screen",
"fancy_borders");
185 INI_GET(EngineGlobals::Screen::outer_border,
"screen",
"outer_border");
187 INI_GET(Globals::Game::random_walls,
"game",
"random_walls");
188 INI_GET(Globals::Game::fruits_at_once,
"game",
"fruits_at_once");
189 INI_GET(Globals::Game::teleport,
"game",
"teleport");
190 INI_GET(Globals::Game::board_scroll_delay,
"game",
"board_scroll_delay");
192 INI_GET(Globals::Game::board_scroll_up,
"game",
"board_scroll_up");
193 INI_GET(Globals::Game::board_scroll_down,
"game",
"board_scroll_down");
194 INI_GET(Globals::Game::board_scroll_left,
"game",
"board_scroll_left");
195 INI_GET(Globals::Game::board_scroll_right,
"game",
"board_scroll_right");
199 buffer = (* ini)(
"game")[
"starting_speed"];
200 if (! buffer.empty())
202 int starting_speed = Globals::Game::starting_speed;
203 Utils::String::convert(buffer, starting_speed);
204 Globals::Game::starting_speed = starting_speed;
212 INI_GET(tmp,
"input",
"left");
213 InputManager::bind(
"left", InputManager::stringToKey(tmp));
215 INI_GET(tmp,
"input",
"right");
216 InputManager::bind(
"right", InputManager::stringToKey(tmp));
218 INI_GET(tmp,
"input",
"up");
219 InputManager::bind(
"up", InputManager::stringToKey(tmp));
221 INI_GET(tmp,
"input",
"down");
222 InputManager::bind(
"down", InputManager::stringToKey(tmp));
224 INI_GET(tmp,
"input",
"pause");
225 InputManager::bind(
"pause", InputManager::stringToKey(tmp));
227 INI_GET(tmp,
"input",
"help");
228 InputManager::bind(
"help", InputManager::stringToKey(tmp));
230 INI_GET(tmp,
"input",
"quit");
231 InputManager::bind(
"quit", InputManager::stringToKey(tmp));
235 INI_GET(board_size,
"game",
"board_size");
236 Globals::Game::board_size = Globals::Game::intToBoardSize(board_size);
239 INI_GET(tmp,
"gui_colors",
"text");
240 EngineGlobals::Theme::text = ColorPair::fromString(tmp);
242 INI_GET(tmp,
"gui_colors",
"hilite_text");
243 EngineGlobals::Theme::hilite_text = ColorPair::fromString(tmp);
245 INI_GET(tmp,
"gui_colors",
"textbox");
246 EngineGlobals::Theme::textbox = ColorPair::fromString(tmp);
248 INI_GET(tmp,
"game_colors",
"player_head");
249 Globals::Theme::player_head = ColorPair::fromString(tmp);
251 INI_GET(tmp,
"game_colors",
"player_head_dead");
252 Globals::Theme::player_head_dead = ColorPair::fromString(tmp);
254 INI_GET(tmp,
"game_colors",
"player_body");
255 Globals::Theme::player_body = ColorPair::fromString(tmp);
257 INI_GET(tmp,
"game_colors",
"fruit");
258 Globals::Theme::fruit = ColorPair::fromString(tmp);
269 ini =
new INI::Parser(Globals::Config::file);
271 catch(std::runtime_error& e)
273 ini =
new INI::Parser();
282 #define INI_SET(out, in, var) \ 284 buffer = Utils::String::toString(var); \ 285 ini->top().addGroup(out); \ 286 (* ini)(out).addKey(in, buffer); \ 289 INI_SET(
"screen",
"center_horizontal", EngineGlobals::Screen::center_horizontally);
290 INI_SET(
"screen",
"center_vertical", EngineGlobals::Screen::center_vertically);
292 INI_SET(
"screen",
"borders", EngineGlobals::Screen::show_borders);
293 INI_SET(
"screen",
"fancy_borders", EngineGlobals::Screen::fancy_borders);
294 INI_SET(
"screen",
"outer_border", EngineGlobals::Screen::outer_border);
296 INI_SET(
"game",
"random_walls", Globals::Game::random_walls);
297 INI_SET(
"game",
"fruits_at_once", Globals::Game::fruits_at_once);
298 INI_SET(
"game",
"teleport", Globals::Game::teleport);
300 INI_SET(
"game",
"board_scroll_delay", Globals::Game::board_scroll_delay);
302 INI_SET(
"game",
"board_scroll_up", Globals::Game::board_scroll_up);
303 INI_SET(
"game",
"board_scroll_down", Globals::Game::board_scroll_down);
304 INI_SET(
"game",
"board_scroll_left", Globals::Game::board_scroll_left);
305 INI_SET(
"game",
"board_scroll_right", Globals::Game::board_scroll_right);
309 int starting_speed = Globals::Game::starting_speed;
310 buffer = Utils::String::toString(starting_speed);
311 ini->top().addGroup(
"game");
312 (* ini)(
"game").addKey(
"starting_speed", buffer);
319 key = InputManager::keyToString(InputManager::getBind(
"left"));
320 INI_SET(
"input",
"left", key);
322 key = InputManager::keyToString(InputManager::getBind(
"right"));
323 INI_SET(
"input",
"right", key);
325 key = InputManager::keyToString(InputManager::getBind(
"up"));
326 INI_SET(
"input",
"up", key);
328 key = InputManager::keyToString(InputManager::getBind(
"down"));
329 INI_SET(
"input",
"down", key);
331 key = InputManager::keyToString(InputManager::getBind(
"pause"));
332 INI_SET(
"input",
"pause", key);
334 key = InputManager::keyToString(InputManager::getBind(
"help"));
335 INI_SET(
"input",
"help", key);
337 key = InputManager::keyToString(InputManager::getBind(
"quit"));
338 INI_SET(
"input",
"quit", key);
341 int board_size = Globals::Game::boardSizeToInt(Globals::Game::board_size);
342 INI_SET(
"game",
"board_size", board_size);
345 INI_SET(
"gui_colors",
"text", EngineGlobals::Theme::text.toString());
347 INI_SET(
"gui_colors",
"hilite_text", EngineGlobals::Theme::hilite_text.toString());
349 INI_SET(
"gui_colors",
"textbox", EngineGlobals::Theme::textbox.toString());
352 INI_SET(
"game_colors",
"player_head", Globals::Theme::player_head.toString());
354 INI_SET(
"game_colors",
"player_head_dead", Globals::Theme::player_head_dead.toString());
356 INI_SET(
"game_colors",
"player_body", Globals::Theme::player_body.toString());
358 INI_SET(
"game_colors",
"fruit", Globals::Theme::fruit.toString());
362 ini->saveAs(Globals::Config::file);
364 catch(std::runtime_error& e)
373 if (! Globals::Error::has_config_file)
375 std::cout <<
"Warning: We could not create the configuration file.\n" 376 <<
" Please check permissions to the path:\n" 377 <<
" " + Globals::Config::file
380 if (! Globals::Error::has_score_file)
382 std::cout <<
"Warning: We could not create the score file.\n" 383 <<
" Please check permissions to the path:\n" 384 <<
" " + Globals::Config::scoresFile
387 if (Globals::Error::old_version_score_file)
389 std::cout <<
"Warning: Your high score file is from an old nsnake version." 392 if (Globals::Error::strange_score_file)
395 Utils::File::create(Globals::Config::scoresFile);
397 std::cout <<
"Error: Corrupted high score file!\n" 398 <<
" We're sorry, but we had to erase it" void saveFile()
Loads global variables to the default file.
void loadFile()
Loads global variables from the default file.
char version[3]
Game version (format MMP - Major Minor Patch).
static std::string directory
Default directory where the level files are.
void init()
Initializes global variables with default values.
void warnErrors()
Warns the user about any errors and warnings found during the program's execution.
static std::string directory
Default directory where we store the game score files.