我目前正在尝试找出一种方法,使用physfs和allegro 5将一个文件(确切地说是一个allegro配置文件)写入到一个挂载的zip文件中。
读取配置文件可以很好地工作,但在写入更改后的配置时,什么都不会发生(例如,文件不会被重写,因此会保持原来的状态)。
此外,当不使用物理文件系统时,一切都工作得很好。
下面是我使用的代码:
Game::Game(int height, int width, int newDifficulty)
{
PHYSFS_init(NULL);
if (!PHYSFS_addToSearchPath("Data.zip", 1)) {
// error handling
}
al_set_physfs_file_interface();
cfg = al_load_config_file("cfg.cfg");
if (cfg != NULL) // file exists, read from it
{
const char *score = al_get_config_value(cfg, "", "highScore");
highScore = atoi(score); // copy value
}
else // file does not exist, create it and init highScore to 0
{
cfg = al_create_config();
al_set_config_value(cfg, "", "highScore", "0");
highScore = 0;
al_save_config_file("cfg.cfg", cfg);
}
...
}在另一个函数中:
void Game::resetGame()
{
// high score
if (player->getScore() > highScore)
{
highScore = player->getScore();
// convert new highScore to char* that can be saved
stringstream strs;
strs << highScore;
string temp_str = strs.str();
char const* pchar = temp_str.c_str();
if (cfg != NULL) // file exists, read from it
{
al_set_config_value(cfg, "", "highScore", pchar);
al_save_config_file("cfg.cfg", cfg);
}
}
...
}由于代码可以在没有物理文件的情况下运行,我想我正确地处理了配置文件本身。任何帮助都将不胜感激!
干杯,hannes
发布于 2014-08-30 23:46:44
与此同时,我自己解决了这个问题。显然,物理文件系统不能写入归档文件。
因此,我需要PHYSFS_setWriteDir("jhdsaf"),将cfg-file保存在那个文件夹中,然后用cfg-file的更新版本替换原始的zip-file,就在游戏结束之前(在卸载所有资源之后,因为zip文件还在使用中)。
如果任何人对代码感兴趣,请回复这篇文章!
汉尼斯
https://stackoverflow.com/questions/25582094
复制相似问题