Changeset - r24804:1320798728bb
[Not reviewed]
master
0 3 0
frosch - 4 years ago 2021-01-12 21:39:31
frosch@openttd.org
Codechange: move non-GUI code to non-GUI source files.
3 files changed with 33 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/newgrf_townname.cpp
Show inline comments
 
@@ -8,27 +8,30 @@
 
/**
 
 * @file newgrf_townname.cpp
 
 * Implementation of  Action 0F "universal holder" structure and functions.
 
 * This file implements a linked-lists of townname generators,
 
 * holding everything that the newgrf action 0F will send over to OpenTTD.
 
 */
 

	
 
#include "stdafx.h"
 
#include "newgrf_townname.h"
 
#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)
 
{
 
	GRFTownName *t = _grf_townnames;
 
	for (; t != nullptr; t = t->next) {
 
		if (t->grfid == grfid) return t;
 
	}
 
	return nullptr;
 
}
 

	
 
GRFTownName *AddGRFTownName(uint32 grfid)
 
{
 
@@ -92,34 +95,42 @@ char *GRFTownNameGenerate(char *buf, uin
 
{
 
	strecpy(buf, "", last);
 
	for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
 
		if (t->grfid == grfid) {
 
			assert(gen < t->nb_gen);
 
			buf = RandomPart(buf, t, seed, t->id[gen], last);
 
			break;
 
		}
 
	}
 
	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()
 
{
 
	while (_grf_townnames != nullptr) DelGRFTownName(_grf_townnames->grfid);
 
}
 

	
 
uint32 GetGRFTownNameId(int gen)
 
{
 
	for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
 
		if (gen < t->nb_gen) return t->grfid;
 
		gen -= t->nb_gen;
src/newgrf_townname.h
Show inline comments
 
@@ -4,24 +4,25 @@
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/**
 
 * @file newgrf_townname.h
 
 * Header of Action 0F "universal holder" structure and functions
 
 */
 

	
 
#ifndef NEWGRF_TOWNNAME_H
 
#define NEWGRF_TOWNNAME_H
 

	
 
#include <vector>
 
#include "strings_type.h"
 

	
 
struct NamePart {
 
	byte prob;     ///< The relative probability of the following name to appear in the bottom 7 bits.
 
	union {
 
		char *text;    ///< If probability bit 7 is clear
 
		byte id;       ///< If probability bit 7 is set
 
	} data;
 
};
 

	
 
struct NamePartList {
 
	byte partcount;
 
@@ -36,18 +37,20 @@ struct GRFTownName {
 
	byte nb_gen;
 
	byte id[128];
 
	StringID name[128];
 
	byte nbparts[128];
 
	NamePartList *partlist[128];
 
	GRFTownName *next;
 
};
 

	
 
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 */
src/settings_gui.cpp
Show inline comments
 
@@ -65,53 +65,28 @@ static const StringID _gui_zoom_dropdown
 
	STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_4X_ZOOM,
 
	INVALID_STRING_ID,
 
};
 

	
 
static const StringID _font_zoom_dropdown[] = {
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_AUTO,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_NORMAL,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_2X_ZOOM,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_4X_ZOOM,
 
	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.
 
 */
 
static uint GetCurRes()
 
{
 
	uint i;
 

	
 
	for (i = 0; i != _resolutions.size(); i++) {
 
		if (_resolutions[i] == Dimension(_screen.width, _screen.height)) break;
 
	}
 
	return i;
 
@@ -235,27 +210,28 @@ struct GameOptionsWindow : Window {
 
				for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
 
					list.emplace_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_TOWNNAME_DROPDOWN: { // Setup townname dropdown
 
				*selected_index = this->opt->game_creation.town_name;
 

	
 
				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);
 

	
 
				size_t newgrf_size = list.size();
 
				/* Insert newgrf_names at the top of the list */
 
				if (newgrf_size > 0) {
 
					list.emplace_back(new DropDownListItem(-1, false)); // separator line
 
					newgrf_size++;
 
				}
 

	
 
				/* Add and sort original townnames generators */
 
				for (int i = 0; i < BUILTIN_TOWNNAME_GENERATOR_COUNT; i++) {
 
@@ -322,25 +298,32 @@ struct GameOptionsWindow : Window {
 
				list = BuildMusicSetDropDownList(selected_index);
 
				break;
 
		}
 

	
 
		return list;
 
	}
 

	
 
	void SetStringParameters(int widget) const override
 
	{
 
		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;
 
			case WID_GO_GUI_ZOOM_DROPDOWN:   SetDParam(0, _gui_zoom_dropdown[_gui_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _gui_zoom_cfg + 1 : 0]); break;
 
			case WID_GO_FONT_ZOOM_DROPDOWN:  SetDParam(0, _font_zoom_dropdown[_font_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _font_zoom_cfg + 1 : 0]); break;
 
			case WID_GO_BASE_GRF_DROPDOWN:   SetDParamStr(0, BaseGraphics::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_GRF_STATUS:     SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
 
			case WID_GO_BASE_SFX_DROPDOWN:   SetDParamStr(0, BaseSounds::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_MUSIC_STATUS:   SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
 
		}
 
	}
0 comments (0 inline, 0 general)