首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >allegro 5使用物理文件时编写文件

allegro 5使用物理文件时编写文件
EN

Stack Overflow用户
提问于 2014-08-30 19:57:03
回答 1查看 368关注 0票数 0

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

读取配置文件可以很好地工作,但在写入更改后的配置时,什么都不会发生(例如,文件不会被重写,因此会保持原来的状态)。

此外,当不使用物理文件系统时,一切都工作得很好。

下面是我使用的代码:

代码语言:javascript
复制
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);
}
...
}

在另一个函数中:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2014-08-30 23:46:44

与此同时,我自己解决了这个问题。显然,物理文件系统不能写入归档文件。

因此,我需要PHYSFS_setWriteDir("jhdsaf"),将cfg-file保存在那个文件夹中,然后用cfg-file的更新版本替换原始的zip-file,就在游戏结束之前(在卸载所有资源之后,因为zip文件还在使用中)。

如果任何人对代码感兴趣,请回复这篇文章!

汉尼斯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25582094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档