Esempio n. 1
0
Profile::Profile(std::string name):
	name(name),
	directory(Globals::Config::directory + name + "/"),
	fileSettings(directory + "settings.ini"),
	fileTheme(directory + "theme.ini"),
	fileStatistics(directory + "statistics.bin")
{
	// Make sure we can load a default profile at startup
	// (meaning the global settings file must have a profile
	//  name at `profiles:default`)

	if (! Utils::File::exists(Globals::Config::file))
	{
		INI::Parser ini;
		ini.create();

		ini.top().addGroup("profiles");
		ini("profiles").addKey("default", name);

		try
		{
			ini.saveAs(Globals::Config::file);
		}
		catch(std::runtime_error& e)
		{
			// Couldn't save the file...
			// ... do nothing
		}
	}
	else
	{
		INI::Parser ini(Globals::Config::file);

		std::string buffer = ini("profiles")["default"];
		if (buffer.empty())
		{
			ini.top().addGroup("profiles");
			ini("profiles").addKey("default", name);

			try
			{
				ini.saveAs(Globals::Config::file);
			}
			catch(std::runtime_error& e)
			{
				// Couldn't save the file...
				// ... do nothing
			}
		}
	}

	// Making sure default directories and files exist.

	if (! Utils::File::isDirectory(this->directory))
		Utils::File::mkdir_p(this->directory);

	if (! Utils::File::exists(this->fileSettings))
		Utils::File::create(this->fileSettings);

	if (! Utils::File::exists(this->fileTheme))
		Utils::File::create(this->fileTheme);

	if (! Utils::File::exists(this->fileStatistics))
		Utils::File::create(this->fileStatistics);

	// Now that the directories exist, let's make sure
	// it's on the global list of existing Profiles
	if (std::find(Profile::profiles.begin(), Profile::profiles.end(), name) == Profile::profiles.end())
	{
		Profile::profiles.push_back(name);
		std::sort(Profile::profiles.begin(), Profile::profiles.end());
	}

	// Starting all settings with default, hardcoded values.

	// Screen

	settings.screen.center_horizontally = true;
	settings.screen.center_vertically   = true;

	settings.screen.show_borders  = true;
	settings.screen.fancy_borders = true;
	settings.screen.outer_border  = true;

	settings.screen.use_colors = true;

	settings.screen.show_statistics = true;

	settings.screen.animation_menu = std::string("random");
	settings.screen.animation_game = std::string("none");

	// Game
	settings.game.next_pieces = 7;

	settings.game.has_ghost = true;
	settings.game.can_hold  = true;

	settings.game.has_game_over_animation = true;
	settings.game.line_clear_delay = 200; // ms

	// Theme

	settings.theme.text        = ColorPair();
	settings.theme.hilite_text = Colors::pair("cyan", "default");
	settings.theme.textbox     = settings.theme.hilite_text.reverse();

	settings.theme.piece_has_colors = true;
	settings.theme.ghost_has_colors = true;
	settings.theme.show_pivot_block = false;
	settings.theme.lock_piece_color = false;

	settings.theme.clear_line = new Block(Colors::pair("white", "default"),
	                ':', ':');

	settings.theme.piece_colorless = new Block(Colors::pair("black", "white"),
	                ' ', ' ');

	settings.theme.piece = new Block(Colors::pair("black", "white"),
	                ' ', ' ');

	settings.theme.ghost = new Block(Colors::pair("black", "white", true),
	                '[', ']');

	settings.theme.locked = new Block(Colors::pair("white", "white"),
	                ' ', ' ');

	settings.theme.invisible = new Block(Colors::pair("white", "default"),
	                                      ' ', ' ');

	settings.theme.piece_S = new Block(Colors::pair("white", "green"),
	                ' ', ' ');

	settings.theme.piece_Z = new Block(Colors::pair("white", "red"),
	                ' ', ' ');

	settings.theme.piece_O = new Block(Colors::pair("white", "yellow"),
	                ' ', ' ');

	settings.theme.piece_I = new Block(Colors::pair("white", "cyan", true),
	                ' ', ' ');

	settings.theme.piece_L = new Block(Colors::pair("white", "yellow", true),
	                ' ', ' ');

	settings.theme.piece_J = new Block(Colors::pair("white", "blue"),
	                ' ', ' ');

	settings.theme.piece_T = new Block(Colors::pair("white", "magenta"),
	                ' ', ' ');

	this->resetKeybindings();

	this->scores = new ScoreFile();
}
Esempio n. 2
0
void ScoreFile::save()
{
	// Score files are dependent of the level name.
	std::string score_file = (ScoreFile::directory +
	                          this->level_name +
	                          "." +
	                          ScoreFile::extension);

	// Will fall back to default high score file
	// if no level was specified
	if (this->level_name.empty())
		score_file = Globals::Config::scoresFile;

	// Tries to create file if it doesn't exist.
	// If we can't create it at all let's just give up.
	if (! Utils::File::exists(score_file))
	{
		Utils::File::create(score_file);

		if (! Utils::File::exists(score_file))
			throw ScoreFileException("Could not create file '" + score_file + "'");
	}

	// We'll recreate the whole score file from scratch
	INI::Parser ini;
	ini.create();
	ini.top().addKey("version", std::string(VERSION));

	// Adding each score entry on the file
	for (size_t i = 0; i < (this->entries.size()); i++)
	{
		std::string score_name = "score" + Utils::String::toString(i);

		ini.top().addGroup(score_name);

		ini(score_name).addKey("level", this->entries[i].level);
		ini(score_name).addKey("points", Utils::String::toString(this->entries[i].points));
		ini(score_name).addKey("speed",  Utils::String::toString(this->entries[i].speed));
		ini(score_name).addKey("fruits", Utils::String::toString(this->entries[i].fruits));

		ini(score_name).addKey("random_walls", Utils::String::toString(this->entries[i].random_walls));
		ini(score_name).addKey("teleport", Utils::String::toString(this->entries[i].teleport));

		int board_size = Globals::Game::boardSizeToInt(this->entries[i].board_size);
		ini(score_name).addKey("board_size", Utils::String::toString(board_size));

		ini(score_name).addKey("board_scroll_delay", Utils::String::toString(this->entries[i].board_scroll_delay));
		ini(score_name).addKey("board_scroll_left", Utils::String::toString(this->entries[i].board_scroll_left));
		ini(score_name).addKey("board_scroll_right", Utils::String::toString(this->entries[i].board_scroll_right));
		ini(score_name).addKey("board_scroll_up", Utils::String::toString(this->entries[i].board_scroll_up));
		ini(score_name).addKey("board_scroll_down", Utils::String::toString(this->entries[i].board_scroll_down));
	}

	std::stringstream contents;
	ini.dump(contents);

	std::ofstream file;
	file.open(score_file.c_str());
	file << Utils::Base64::encode(contents.str());
	file.close();
}
Esempio n. 3
0
void Globals::saveFile()
{
	// Even if the file doesn't exist, we'll create it.
	INI::Parser* ini;

	try
	{
		ini = new INI::Parser(Globals::Config::file);
	}
	catch(std::runtime_error& e)
	{
		ini = new INI::Parser();
		ini->create();
	}

	// Will be used on this macro below
	std::string buffer;

// Other macro to avoid typing, similar to the one
// at loadFile()
#define INI_SET(out, in, var)	               \
	{                                          \
		buffer = Utils::String::toString(var); \
		ini->top().addGroup(out);              \
		(* ini)(out).addKey(in, buffer);       \
	}

	INI_SET("screen", "center_horizontal", EngineGlobals::Screen::center_horizontally);
	INI_SET("screen", "center_vertical",   EngineGlobals::Screen::center_vertically);

	INI_SET("screen", "borders",       EngineGlobals::Screen::show_borders);
	INI_SET("screen", "fancy_borders", EngineGlobals::Screen::fancy_borders);
	INI_SET("screen", "outer_border",  EngineGlobals::Screen::outer_border);

	INI_SET("game", "random_walls",     Globals::Game::random_walls);
	INI_SET("game", "fruits_at_once",   Globals::Game::fruits_at_once);
	INI_SET("game", "teleport",         Globals::Game::teleport);

	INI_SET("game", "board_scroll_delay", Globals::Game::board_scroll_delay);

	INI_SET("game", "board_scroll_up",    Globals::Game::board_scroll_up);
	INI_SET("game", "board_scroll_down",  Globals::Game::board_scroll_down);
	INI_SET("game", "board_scroll_left",  Globals::Game::board_scroll_left);
	INI_SET("game", "board_scroll_right", Globals::Game::board_scroll_right);

	// unsigned ints are the exception - their overloading
	// is ambiguous... I should consider removing them altogether
	int starting_speed = Globals::Game::starting_speed;
	buffer = Utils::String::toString(starting_speed);
	ini->top().addGroup("game");
	(* ini)("game").addKey("starting_speed", buffer);

	// Special Cases

	// Input Keys
	std::string key;

	key = InputManager::keyToString(InputManager::getBind("left"));
	INI_SET("input", "left", key);

	key = InputManager::keyToString(InputManager::getBind("right"));
	INI_SET("input", "right", key);

	key = InputManager::keyToString(InputManager::getBind("up"));
	INI_SET("input", "up", key);

	key = InputManager::keyToString(InputManager::getBind("down"));
	INI_SET("input", "down", key);

	key = InputManager::keyToString(InputManager::getBind("pause"));
	INI_SET("input", "pause", key);

	key = InputManager::keyToString(InputManager::getBind("help"));
	INI_SET("input", "help", key);

	key = InputManager::keyToString(InputManager::getBind("quit"));
	INI_SET("input", "quit", key);

	// Board size
	int board_size = Globals::Game::boardSizeToInt(Globals::Game::board_size);
	INI_SET("game", "board_size", board_size);

	try
	{
		ini->saveAs(Globals::Config::file);
	}
	catch(std::runtime_error& e)
	{
		// Couldn't save the file...
		// ... do nothing
	}
	SAFE_DELETE(ini);
}