1 #include <Config/Globals.hpp> 2 #include <Config/INI.hpp> 3 #include <Misc/Utils.hpp> 4 #include <Flow/InputManager.hpp> 5 #include <Game/BoardParser.hpp> 6 #include <Game/ScoreFile.hpp> 22 std::string Globals::Config::directory =
"";
23 std::string Globals::Config::file =
"";
24 std::string Globals::Config::scoresFile =
"";
26 bool Globals::Screen::center_horizontally =
true;
27 bool Globals::Screen::center_vertically =
true;
29 bool Globals::Screen::show_borders =
true;
30 bool Globals::Screen::fancy_borders =
true;
31 bool Globals::Screen::outer_border =
true;
33 unsigned int Globals::Game::starting_speed = 1;
34 int Globals::Game::fruits_at_once = 1;
35 bool Globals::Game::random_walls =
false;
36 bool Globals::Game::teleport =
false;
37 std::string Globals::Game::current_level =
"";
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 ColorPair Globals::Theme::text;
70 ColorPair Globals::Theme::hilite_text;
71 ColorPair Globals::Theme::textbox;
73 bool Globals::Error::has_config_file =
true;
74 bool Globals::Error::has_score_file =
true;
75 bool Globals::Error::old_version_score_file =
false;
76 bool Globals::Error::strange_score_file =
false;
85 Globals::Theme::text = 0;
86 Globals::Theme::hilite_text = Colors::pair(COLOR_CYAN, COLOR_DEFAULT);
87 Globals::Theme::textbox = (Globals::Theme::hilite_text | A_REVERSE);
96 if (Utils::String::front(Globals::Config::directory) !=
'/')
100 Globals::Config::directory = (
"/tmp/" +
101 Globals::Config::directory);
104 Globals::Config::file = (Globals::Config::directory +
107 Globals::Config::scoresFile = (Globals::Config::directory +
108 "arcade.nsnakescores");
117 Globals::Config::directory =
"/dev/";
118 Globals::Config::file =
"/dev/null";
123 InputManager::bind(
"left", KEY_LEFT);
124 InputManager::bind(
"right", KEY_RIGHT);
125 InputManager::bind(
"up", KEY_UP);
126 InputManager::bind(
"down", KEY_DOWN);
127 InputManager::bind(
"pause",
'p');
128 InputManager::bind(
"help",
'h');
129 InputManager::bind(
"quit",
'q');
145 if (! Globals::Error::has_config_file)
147 std::cout <<
"Warning: We could not create the configuration file.\n" 148 <<
" Please check permissions to the path:\n" 149 <<
" " + Globals::Config::file
152 if (! Globals::Error::has_score_file)
154 std::cout <<
"Warning: We could not create the score file.\n" 155 <<
" Please check permissions to the path:\n" 156 <<
" " + Globals::Config::scoresFile
159 if (Globals::Error::old_version_score_file)
161 std::cout <<
"Warning: Your high score file is from an old nsnake version." 164 if (Globals::Error::strange_score_file)
169 std::cout <<
"Error: Corrupted high score file!\n" 170 <<
" We're sorry, but we had to erase it" 185 catch(std::runtime_error& e)
194 std::string buffer =
"";
204 #define INI_GET(var, out, in) \ 206 buffer = (* ini)(out)[in]; \ 207 if (! buffer.empty()) \ 209 Utils::String::convert(buffer, var); \ 213 INI_GET(Globals::Screen::center_horizontally,
"screen",
"center_horizontal");
214 INI_GET(Globals::Screen::center_vertically,
"screen",
"center_vertical");
216 INI_GET(Globals::Screen::show_borders,
"screen",
"borders");
217 INI_GET(Globals::Screen::fancy_borders,
"screen",
"fancy_borders");
218 INI_GET(Globals::Screen::outer_border,
"screen",
"outer_border");
220 INI_GET(Globals::Game::random_walls,
"game",
"random_walls");
221 INI_GET(Globals::Game::fruits_at_once,
"game",
"fruits_at_once");
222 INI_GET(Globals::Game::teleport,
"game",
"teleport");
223 INI_GET(Globals::Game::board_scroll_delay,
"game",
"board_scroll_delay");
225 INI_GET(Globals::Game::board_scroll_up,
"game",
"board_scroll_up");
226 INI_GET(Globals::Game::board_scroll_down,
"game",
"board_scroll_down");
227 INI_GET(Globals::Game::board_scroll_left,
"game",
"board_scroll_left");
228 INI_GET(Globals::Game::board_scroll_right,
"game",
"board_scroll_right");
232 buffer = (* ini)(
"game")[
"starting_speed"];
233 if (! buffer.empty())
235 int starting_speed = Globals::Game::starting_speed;
236 Utils::String::convert(buffer, starting_speed);
237 Globals::Game::starting_speed = starting_speed;
245 INI_GET(tmp,
"input",
"left");
246 InputManager::bind(
"left", InputManager::stringToKey(tmp));
248 INI_GET(tmp,
"input",
"right");
249 InputManager::bind(
"right", InputManager::stringToKey(tmp));
251 INI_GET(tmp,
"input",
"up");
252 InputManager::bind(
"up", InputManager::stringToKey(tmp));
254 INI_GET(tmp,
"input",
"down");
255 InputManager::bind(
"down", InputManager::stringToKey(tmp));
257 INI_GET(tmp,
"input",
"pause");
258 InputManager::bind(
"pause", InputManager::stringToKey(tmp));
260 INI_GET(tmp,
"input",
"help");
261 InputManager::bind(
"help", InputManager::stringToKey(tmp));
263 INI_GET(tmp,
"input",
"quit");
264 InputManager::bind(
"quit", InputManager::stringToKey(tmp));
268 INI_GET(board_size,
"game",
"board_size");
269 Globals::Game::board_size = Globals::Game::intToBoardSize(board_size);
282 catch(std::runtime_error& e)
292 #define INI_SET(out, in, var) \ 294 buffer = Utils::String::toString(var); \ 295 ini->top().addGroup(out); \ 296 (* ini)(out).addKey(in, buffer); \ 299 INI_SET(
"screen",
"center_horizontal", Globals::Screen::center_horizontally);
300 INI_SET(
"screen",
"center_vertical", Globals::Screen::center_vertically);
302 INI_SET(
"screen",
"borders", Globals::Screen::show_borders);
303 INI_SET(
"screen",
"fancy_borders", Globals::Screen::fancy_borders);
304 INI_SET(
"screen",
"outer_border", Globals::Screen::outer_border);
306 INI_SET(
"game",
"random_walls", Globals::Game::random_walls);
307 INI_SET(
"game",
"fruits_at_once", Globals::Game::fruits_at_once);
308 INI_SET(
"game",
"teleport", Globals::Game::teleport);
310 INI_SET(
"game",
"board_scroll_delay", Globals::Game::board_scroll_delay);
312 INI_SET(
"game",
"board_scroll_up", Globals::Game::board_scroll_up);
313 INI_SET(
"game",
"board_scroll_down", Globals::Game::board_scroll_down);
314 INI_SET(
"game",
"board_scroll_left", Globals::Game::board_scroll_left);
315 INI_SET(
"game",
"board_scroll_right", Globals::Game::board_scroll_right);
319 int starting_speed = Globals::Game::starting_speed;
320 buffer = Utils::String::toString(starting_speed);
322 (* ini)(
"game").addKey(
"starting_speed", buffer);
329 key = InputManager::keyToString(InputManager::getBind(
"left"));
330 INI_SET(
"input",
"left", key);
332 key = InputManager::keyToString(InputManager::getBind(
"right"));
333 INI_SET(
"input",
"right", key);
335 key = InputManager::keyToString(InputManager::getBind(
"up"));
336 INI_SET(
"input",
"up", key);
338 key = InputManager::keyToString(InputManager::getBind(
"down"));
339 INI_SET(
"input",
"down", key);
341 key = InputManager::keyToString(InputManager::getBind(
"pause"));
342 INI_SET(
"input",
"pause", key);
344 key = InputManager::keyToString(InputManager::getBind(
"help"));
345 INI_SET(
"input",
"help", key);
347 key = InputManager::keyToString(InputManager::getBind(
"quit"));
348 INI_SET(
"input",
"quit", key);
351 int board_size = Globals::Game::boardSizeToInt(Globals::Game::board_size);
352 INI_SET(
"game",
"board_size", board_size);
356 ini->
saveAs(Globals::Config::file);
358 catch(std::runtime_error& e)
Level & top()
Returns the top level of this INI file.
void saveFile()
Saves current configurations to the default file name.
std::string getHome()
Gets the full path of the home directory for the user running this program.
void saveAs(std::string filename)
Save all the internal INI contents on a file with #filename.
void exit()
Warns the user about any errors and warnings found during the program's execution.
void addGroup(std::string name)
Creates a new child group with #name.
Loads, reads and parses the contents of an INI file (or string).
void loadFile()
Loads configuration from the default file name.
bool isDirectory(std::string path)
Tells if #path is a directory.
char version[3]
Game version (format MMP - Major Minor Patch).
static std::string directory
Default directory where the level files are.
bool create(std::string path)
Creates empty file #path.
void init()
Allocates necessary variables.
void mkdir_p(std::string path)
Creates #path directory hierarchy recursively, just like UNIX command mkdir -p.
static std::string directory
Default directory where we store the game score files.
bool exists(std::string path)
Tells if #path exists.
void create()
Creates a blank INI registry.