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); }
/** Increments the iterator to point to the next entry. @return reference to this. */ const_iterator &operator ++() { m_name = al_get_next_config_entry(&m_entry); return *this; }
static bool _al_xwin_get_keyboard_mapping(void) { int i; int count; int missing = 0; ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); memset(used, 0, sizeof used); memset(keycode_to_scancode, 0, sizeof keycode_to_scancode); XDisplayKeycodes(system->x11display, &min_keycode, &max_keycode); count = 1 + max_keycode - min_keycode; if (keysyms) { XFree(keysyms); } keysyms = XGetKeyboardMapping(system->x11display, min_keycode, count, &sym_per_key); ALLEGRO_INFO("%i keys, %i symbols per key.\n", count, sym_per_key); if (sym_per_key <= 0) { return false; } missing = 0; for (i = min_keycode; i <= max_keycode; i++) { KeySym sym = keysyms[sym_per_key * (i - min_keycode)]; KeySym sym2 = keysyms[sym_per_key * (i - min_keycode) + 1]; char *sym_str, *sym2_str; int allegro_key = 0; char str[1024]; sym_str = XKeysymToString(sym); sym2_str = XKeysymToString(sym2); snprintf(str, sizeof str, "key [%i: %s %s]", i, sym_str ? sym_str : "NULL", sym2_str ? sym2_str : "NULL"); /* Hack for French keyboards, to correctly map ALLEGRO_KEY_0 to ALLEGRO_KEY_9. */ if (sym2 >= XK_0 && sym2 <= XK_9) { allegro_key = find_allegro_key(sym2); } if (!allegro_key) { if (sym != NoSymbol) { allegro_key = find_allegro_key(sym); if (allegro_key == 0) { missing++; ALLEGRO_DEBUG("%s defering.\n", str); } } else { /* No KeySym for this key - ignore it. */ keycode_to_scancode[i] = -1; ALLEGRO_DEBUG("%s not assigned.\n", str); } } if (allegro_key) { bool is_double = false; if (used[allegro_key]) { is_double = true; } keycode_to_scancode[i] = allegro_key; key_names[allegro_key] = XKeysymToString(keysyms[sym_per_key * (i - min_keycode)]); used[allegro_key] = 1; ALLEGRO_DEBUG("%s%s assigned to %i.\n", str, is_double ? " *double*" : "", allegro_key); } } if (missing) { /* The keys still not assigned are just assigned arbitrarily now. */ for (i = min_keycode; i <= max_keycode; i++) { if (keycode_to_scancode[i] == 0) { find_unknown_key_assignment(i); } } } if (xmodmap) XFreeModifiermap(xmodmap); xmodmap = XGetModifierMapping(system->x11display); for (i = 0; i < 8; i++) { int j; char str[1024]; sprintf(str, "Modifier %d:", i + 1); for (j = 0; j < xmodmap->max_keypermod; j++) { KeyCode keycode = xmodmap->modifiermap[i * xmodmap->max_keypermod + j]; // XKeycodeToKeysym is deprecated //KeySym sym = XKeycodeToKeysym(system->x11display, keycode, 0); KeySym sym = XkbKeycodeToKeysym(system->x11display, keycode, 0, 0); char *sym_str = XKeysymToString(sym); sprintf(str + strlen(str), " %s", sym_str ? sym_str : "NULL"); } ALLEGRO_DEBUG("%s\n", str); } /* The [xkeymap] section can be useful, e.g. if trying to play a * game which has X and Y hardcoded as ALLEGRO_KEY_X and ALLEGRO_KEY_Y to mean * left/right movement, but on the X11 keyboard X and Y are far apart. * For normal use, a user never should have to touch [xkeymap] anymore * though, and proper written programs will not hardcode such mappings. */ ALLEGRO_CONFIG *c = al_get_system_config(); char const *key; ALLEGRO_CONFIG_ENTRY *it; key = al_get_first_config_entry(c, "xkeymap", &it); while (key) { char const *val; val = al_get_config_value(c, "xkeymap", key); int keycode = strtol(key, NULL, 10); int scancode = strtol(val, NULL, 10); if (keycode > 0 && scancode > 0) { keycode_to_scancode[keycode] = scancode; ALLEGRO_WARN("User override: KeySym %i assigned to %i.\n", keycode, scancode); } key = al_get_next_config_entry(&it); } return true; }
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; }