Allegro 5循环使用相同名称的配置文件值

Allegro 5 loop through config file values with the same name

本文关键字:配置文件 循环 Allegro      更新时间:2023-10-16

我有一个配置文件,用于存储地图

name=map1
width=5
height=5
[tiles]
   l=0,0,1,0,0
   l=0,1,1,1,0
   l=0,1,0,1,0
   l=0,1,0,1,0
   l=0,0,0,0,0
[/tiles]

我如何循环遍历[tiles]部分,将他的line(l)值存储到我的向量中?

注意:我放置allegro5标记是因为它具有加载配置文件的功能

正如您所发现的,Allegro只会接受许多条目中的最后一个使用相同的密钥。虽然你可以给每一行一个不同的键,但你可以相反,利用=分配是可选的这一事实:

[tiles]
   0,0,1,0,0
   0,1,1,1,0
   0,1,0,1,0
   0,1,0,1,0
   0,0,0,0,0
[/tiles]

现在,每行的数据都存储在"key"本身中,"value"为已忽略。

int main() {
    ALLEGRO_CONFIG *cfg;
    ALLEGRO_CONFIG_ENTRY *entry;
    const char* row;
    al_init();
    cfg = al_load_config_file("config.cfg");
    row = al_get_first_config_entry(cfg, "tiles", &entry);
    while (entry) {
        printf("%sn", row);
        row = al_get_next_config_entry(&entry);
    }
    return 0;
}

经过一段时间的阅读allegro5参考资料,我找到了一个可以做到这一点的方法,并尝试了一次又一次,所以我自己的问题有了答案:

首先,[tile]部分的入口可能有不同的名称,比如:

[tiles]
   a=0,0,1,0,0
   b=0,1,1,1,0
   c=0,1,0,1,0
   d=0,1,0,1,0
   e=0,0,0,0,0
[/tiles]

那么代码是:

ALLEGRO_CONFIG*config_file=al_load_config_file("example.map");
vector<const char*>lines
ALLEGRO_CONFIG_ENTRY** entry_iterator;
const char* entry;
entry=al_get_first_config_entry(config_file, "tiles", entry_iterator);
while((entry=al_get_next_config_entry(entry_iterator))!=NULL){
        lines.push_back(al_get_config_value(config_file, "tiles", entry));
};