bool IniParser::save() noexcept
{
	// Check if current file is correct
	IniParser oldFileParser = *this;
	oldFileParser.load();
	if (mIniTree == oldFileParser.mIniTree) return true;

	// Opens the file and clears it
	std::ofstream file{mPath, std::ofstream::out | std::ofstream::trunc};
	if (!file.is_open()) return false;

	// Adds the global items first
	auto globalItr = mIniTree.find("");
	if (globalItr != mIniTree.end()) {
		for (auto& keyPair : globalItr->second) {
			file << keyPair.first << "=" << keyPair.second << "\n";
		}
		file << "\n";
	}

	// Adds the rest of the sections and items
	for (auto& sectionPair : mIniTree) {
		if (sectionPair.first == "") continue;
		file << "[" << sectionPair.first << "]" << "\n";
		
		for (auto& keyPair : sectionPair.second) {
			file << keyPair.first << "=" << keyPair.second << "\n";
		}
		file << "\n";
	}

	file.flush();
	return true;
}
Exemple #2
0
void Game::init(const std::string& path)
{
	// Set application directory
	size_t last_dir = path.find_last_of("/\\");
	m_app_dir = path.substr(0, last_dir + 1);
	if (m_app_dir.empty())
		m_app_dir = "./";

	// Init resources search directory
	Resources::setSearchPath(m_app_dir + "/resources/");

	// Init levels
	std::cout << "* loading levels from " << WB_LEVEL_FILE << std::endl;
	LevelManager::getInstance().openFromFile(m_app_dir + WB_LEVEL_FILE);

	// Init GUI module
	gui::Theme::loadFont(m_app_dir + "/resources/images/font.png");

	SoundSystem::openMusicFromFile(m_app_dir + "/resources/musics/evolution_sphere.ogg");

	// Load configuration from settings file
	IniParser config;
	std::cout << "* loading settings from " << WB_SETTINGS_FILE << std::endl;
	if (config.load(m_app_dir + WB_SETTINGS_FILE))
	{
		config.seek_section("Wallbreaker");
		size_t app_width = config.get("app_width", APP_WIDTH * 2);
		size_t app_height = config.get("app_height", APP_HEIGHT * 2);
		setResolution(app_width, app_height);

		Settings::highscore = config.get("highscore", 0);

		SoundSystem::enableSound(config.get("sound", true));
		SoundSystem::enableMusic(config.get("music", true));
	}
	else
	{
		setResolution(APP_WIDTH * 2, APP_HEIGHT * 2);
	}

	SoundSystem::playMusic();
}