static const char *get_next_config_section(ALLEGRO_CONFIG *cfg, ALLEGRO_CONFIG_SECTION **section) { if (*section == NULL) return al_get_first_config_section(cfg, section); else return al_get_next_config_section(section); }
void GameGui::LoadGui(BuildingManager& manager) { // more gross allegro config file stuff // it might be fine to just hardcode this stuff auto buildingMenu = new Gwen::Controls::TabControl(m_canvas.get(), "Buildings"); buildingMenu->SetBounds(SCREEN_WIDTH - 262 - 150,SCREEN_HEIGHT - 135,262,135); auto mapBox = new Gwen::Controls::ImagePanel(m_canvas.get(), "Map"); mapBox->SetBounds(SCREEN_WIDTH - 150, SCREEN_HEIGHT - 150, 150, 150); ALLEGRO_CONFIG *uiConfig = al_load_config_file("ui/building_ui.cfg"); ALLEGRO_CONFIG_SECTION *iter; // this first section is the 'global' section, we ignore it const char * cfgSection = al_get_first_config_section(uiConfig, &iter); const char * cfgEntry; ALLEGRO_CONFIG_ENTRY *eIter; // load tab pages from building_ui.cfg while(true) { cfgSection = al_get_next_config_section(&iter); // did we pass by the last section in the file? if(!cfgSection) break; auto section = std::string(cfgSection); Gwen::Controls::TabButton* resExtractors = buildingMenu->AddPage(section); cfgEntry = al_get_first_config_entry(uiConfig, cfgSection, &eIter); for(int i = 0; cfgEntry; i++) { auto entry = std::string(cfgEntry); CommandButton* but; if (section == "Terraform") but = CommandButton::CreateTerraformButton(resExtractors->GetPage()); else if (section == "Transportation") but = CommandButton::CreatePathwayButton(resExtractors->GetPage()); else but = CommandButton::CreateBuildingButton(resExtractors->GetPage(), manager.GetTemplateByName(entry)); but->SetImage(al_get_config_value(uiConfig, cfgSection, cfgEntry)); but->SetToolTip(cfgEntry); but->SetShouldDrawBackground(false); but->SetBounds((i/2) * 50, 0, 50, 50); but->onPress.Add(&m_iHandler, &InputHandler::OnBuildingButton); if(i%2) but->SetPos((i/2) * 50, 52); cfgEntry = al_get_next_config_entry(&eIter); } } al_destroy_config(uiConfig); }
list* load_all_generator_data() { list *names = list_new(); ALLEGRO_CONFIG *cfg = al_load_config_file(DATA_PATH); // open config file ALLEGRO_CONFIG_SECTION *section = NULL; al_get_first_config_section(cfg, §ion); char const *name = al_get_next_config_section(§ion); while (name != NULL) { load_generator_data(name, cfg); list_push(names, strdup(name)); name = al_get_next_config_section(§ion); } al_destroy_config(cfg); return names; }
int main(void) { ALLEGRO_CONFIG *cfg; const char *value; ALLEGRO_CONFIG_SECTION *iterator; ALLEGRO_CONFIG_ENTRY *iterator2; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); cfg = al_load_config_file("data/sample.cfg"); if (!cfg) { abort_example("Couldn't load data/sample.cfg\n"); } value = al_get_config_value(cfg, NULL, "old_var"); TEST("global var", value && !strcmp(value, "old global value")); value = al_get_config_value(cfg, "section", "old_var"); TEST("section var", value && !strcmp(value, "old section value")); value = al_get_config_value(cfg, "", "mysha.xpm"); TEST("long value", value && strlen(value) == 1394); /* Test whether iterating through our whole sample.cfg returns all * sections and entries, in order. */ value = al_get_first_config_section(cfg, &iterator); TEST("section1", value && !strcmp(value, "")); value = al_get_first_config_entry(cfg, value, &iterator2); TEST("entry1", value && !strcmp(value, "old_var")); value = al_get_next_config_entry(&iterator2); TEST("entry2", value && !strcmp(value, "mysha.xpm")); value = al_get_next_config_entry(&iterator2); TEST("entry3", value == NULL); value = al_get_next_config_section(&iterator); TEST("section2", value && !strcmp(value, "section")); value = al_get_first_config_entry(cfg, value, &iterator2); TEST("entry4", value && !strcmp(value, "old_var")); value = al_get_next_config_entry(&iterator2); TEST("entry5", value == NULL); value = al_get_next_config_section(&iterator); TEST("section3", value); value = al_get_first_config_entry(cfg, value, &iterator2); TEST("entry6", value); value = al_get_next_config_entry(&iterator2); TEST("entry7", value == NULL); value = al_get_next_config_section(&iterator); TEST("section4", value == NULL); al_set_config_value(cfg, "", "new_var", "new value"); al_set_config_value(cfg, "section", "old_var", "new value"); TEST("save_config", al_save_config_file("test.cfg", cfg)); log_printf("Done\n"); al_destroy_config(cfg); close_log(true); return passed ? 0 : 1; }