diff options
-rw-r--r-- | src/newgrf_townname.cpp | 25 | ||||
-rw-r--r-- | src/newgrf_townname.h | 5 | ||||
-rw-r--r-- | src/settings_gui.cpp | 39 |
3 files changed, 33 insertions, 36 deletions
diff --git a/src/newgrf_townname.cpp b/src/newgrf_townname.cpp index 207114f61..a7bc4deea 100644 --- a/src/newgrf_townname.cpp +++ b/src/newgrf_townname.cpp @@ -17,9 +17,12 @@ #include "core/alloc_func.hpp" #include "string_func.h" +#include "table/strings.h" + #include "safeguards.h" static GRFTownName *_grf_townnames = nullptr; +static std::vector<StringID> _grf_townname_names; GRFTownName *GetGRFTownName(uint32 grfid) { @@ -101,16 +104,24 @@ char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, cons return buf; } -StringID *GetGRFTownNameList() + +/** Allocate memory for the NewGRF town names. */ +void InitGRFTownGeneratorNames() { - int nb_names = 0, n = 0; - for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) nb_names += t->nb_gen; - StringID *list = MallocT<StringID>(nb_names + 1); + _grf_townname_names.clear(); for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) { - for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j]; + for (int j = 0; j < t->nb_gen; j++) _grf_townname_names.push_back(t->name[j]); } - list[n] = INVALID_STRING_ID; - return list; +} + +const std::vector<StringID>& GetGRFTownNameList() +{ + return _grf_townname_names; +} + +StringID GetGRFTownNameName(uint gen) +{ + return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED; } void CleanUpGRFTownNames() diff --git a/src/newgrf_townname.h b/src/newgrf_townname.h index 6406e7434..d8fc462aa 100644 --- a/src/newgrf_townname.h +++ b/src/newgrf_townname.h @@ -13,6 +13,7 @@ #ifndef NEWGRF_TOWNNAME_H #define NEWGRF_TOWNNAME_H +#include <vector> #include "strings_type.h" struct NamePart { @@ -45,9 +46,11 @@ GRFTownName *AddGRFTownName(uint32 grfid); GRFTownName *GetGRFTownName(uint32 grfid); void DelGRFTownName(uint32 grfid); void CleanUpGRFTownNames(); -StringID *GetGRFTownNameList(); char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last); uint32 GetGRFTownNameId(int gen); uint16 GetGRFTownNameType(int gen); +StringID GetGRFTownNameName(uint gen); + +const std::vector<StringID>& GetGRFTownNameList(); #endif /* NEWGRF_TOWNNAME_H */ diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 6f1901284..5a095d9d8 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -74,35 +74,10 @@ static const StringID _font_zoom_dropdown[] = { INVALID_STRING_ID, }; -static StringID *_grf_names = nullptr; ///< Pointer to town names defined by NewGRFs. -static int _nb_grf_names = 0; ///< Number of town names defined by NewGRFs. - static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window. static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd); -/** Allocate memory for the NewGRF town names. */ -void InitGRFTownGeneratorNames() -{ - free(_grf_names); - _grf_names = GetGRFTownNameList(); - _nb_grf_names = 0; - for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++; -} - -/** - * Get a town name. - * @param town_name Number of the wanted town name. - * @return Name of the town as string ID. - */ -static inline StringID TownName(int town_name) -{ - if (town_name < BUILTIN_TOWNNAME_GENERATOR_COUNT) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name; - town_name -= BUILTIN_TOWNNAME_GENERATOR_COUNT; - if (town_name < _nb_grf_names) return _grf_names[town_name]; - return STR_UNDEFINED; -} - /** * Get index of the current screen resolution. * @return Index of the current screen resolution if it is a known resolution, _resolutions.size() otherwise. @@ -244,9 +219,10 @@ struct GameOptionsWindow : Window { int enabled_item = (_game_mode == GM_MENU || Town::GetNumItems() == 0) ? -1 : *selected_index; /* Add and sort newgrf townnames generators */ - for (int i = 0; i < _nb_grf_names; i++) { + const auto &grf_names = GetGRFTownNameList(); + for (uint i = 0; i < grf_names.size(); i++) { int result = BUILTIN_TOWNNAME_GENERATOR_COUNT + i; - list.emplace_back(new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0)); + list.emplace_back(new DropDownListStringItem(grf_names[i], result, enabled_item != result && enabled_item >= 0)); } std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc); @@ -331,7 +307,14 @@ struct GameOptionsWindow : Window { switch (widget) { case WID_GO_CURRENCY_DROPDOWN: SetDParam(0, _currency_specs[this->opt->locale.currency].name); break; case WID_GO_ROADSIDE_DROPDOWN: SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break; - case WID_GO_TOWNNAME_DROPDOWN: SetDParam(0, TownName(this->opt->game_creation.town_name)); break; + case WID_GO_TOWNNAME_DROPDOWN: { + int gen = this->opt->game_creation.town_name; + StringID name = gen < BUILTIN_TOWNNAME_GENERATOR_COUNT ? + STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + gen : + GetGRFTownNameName(gen - BUILTIN_TOWNNAME_GENERATOR_COUNT); + SetDParam(0, name); + break; + } case WID_GO_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break; case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break; case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _resolutions.size() ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break; |