【问题标题】:allegro 5 writing files when using physfsallegro 5 使用 physfs 时写入文件
【发布时间】:2014-08-30 11:57:02
【问题描述】:

我目前正在尝试找出一种使用 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 的情况下工作,我想我正确处理了配置文件本身。 任何帮助将不胜感激!

干杯, 汉尼斯

【问题讨论】:

    标签: c++ file config allegro physfs


    【解决方案1】:

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

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

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

    汉内斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多