0

我目前正在尝试找出一种使用 physfs 和 allegro 5 将文件(确切地说是 allegro 配置文件)写入已安装的 zip 文件的方法。

读取配置文件可以正常工作,但是在写入更改的配置时,什么也没有发生(例如,文件没有被重写,因此仍处于旧状态)。

此外,当不使用 physfs 时,一切正常。

这是我使用的代码:

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);
    }
}
...
}

由于代码在没有 physfs 的情况下工作,我想我正确处理了配置文件本身。任何帮助将不胜感激!

干杯,汉内斯

4

1 回答 1

0

与此同时,我自己解决了这个问题。显然,physfs 无法写入档案。

因此,我需要 PHYSFS_setWriteDir("jhdsaf"),将 cfg 文件保存在该文件夹中,然后在游戏关闭之前用 cfg 文件的更新版本替换原始 zip 文件(在所有资源被卸载后,因为否则该拉链仍在使用中)。

如果有人对执行此操作的代码感兴趣,请回复此帖子!

汉内斯

于 2014-08-30T15:46:44.003 回答