mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: use std::array for _sorted_industry_types
This commit is contained in:
parent
60da17418a
commit
48f99fd980
|
@ -183,23 +183,23 @@ static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixTy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES]; ///< Industry types sorted by name.
|
std::array<IndustryType, NUM_INDUSTRYTYPES> _sorted_industry_types; ///< Industry types sorted by name.
|
||||||
|
|
||||||
/** Sort industry types by their name. */
|
/** Sort industry types by their name. */
|
||||||
static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
|
static bool IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b)
|
||||||
{
|
{
|
||||||
static char industry_name[2][64];
|
static char industry_name[2][64];
|
||||||
|
|
||||||
const IndustrySpec *indsp1 = GetIndustrySpec(*a);
|
const IndustrySpec *indsp1 = GetIndustrySpec(a);
|
||||||
GetString(industry_name[0], indsp1->name, lastof(industry_name[0]));
|
GetString(industry_name[0], indsp1->name, lastof(industry_name[0]));
|
||||||
|
|
||||||
const IndustrySpec *indsp2 = GetIndustrySpec(*b);
|
const IndustrySpec *indsp2 = GetIndustrySpec(b);
|
||||||
GetString(industry_name[1], indsp2->name, lastof(industry_name[1]));
|
GetString(industry_name[1], indsp2->name, lastof(industry_name[1]));
|
||||||
|
|
||||||
int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
|
int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting).
|
||||||
|
|
||||||
/* If the names are equal, sort by industry type. */
|
/* If the names are equal, sort by industry type. */
|
||||||
return (r != 0) ? r : (*a - *b);
|
return (r != 0) ? r < 0 : (a < b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,7 +213,7 @@ void SortIndustryTypes()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sort industry types by name. */
|
/* Sort industry types by name. */
|
||||||
QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
|
std::sort(_sorted_industry_types.begin(), _sorted_industry_types.end(), IndustryTypeNameSorter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -302,8 +302,7 @@ class BuildIndustryWindow : public Window {
|
||||||
* The tests performed after the enabled allow to load the industries
|
* The tests performed after the enabled allow to load the industries
|
||||||
* In the same way they are inserted by grf (if any)
|
* In the same way they are inserted by grf (if any)
|
||||||
*/
|
*/
|
||||||
for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
|
for (IndustryType ind : _sorted_industry_types) {
|
||||||
IndustryType ind = _sorted_industry_types[i];
|
|
||||||
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
||||||
if (indsp->enabled) {
|
if (indsp->enabled) {
|
||||||
/* Rule is that editor mode loads all industries.
|
/* Rule is that editor mode loads all industries.
|
||||||
|
@ -2723,8 +2722,7 @@ struct IndustryCargoesWindow : public Window {
|
||||||
|
|
||||||
case WID_IC_IND_DROPDOWN: {
|
case WID_IC_IND_DROPDOWN: {
|
||||||
DropDownList lst;
|
DropDownList lst;
|
||||||
for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
|
for (IndustryType ind : _sorted_industry_types) {
|
||||||
IndustryType ind = _sorted_industry_types[i];
|
|
||||||
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
||||||
if (!indsp->enabled) continue;
|
if (!indsp->enabled) continue;
|
||||||
lst.emplace_back(new DropDownListStringItem(indsp->name, ind, false));
|
lst.emplace_back(new DropDownListStringItem(indsp->name, ind, false));
|
||||||
|
@ -2811,10 +2809,10 @@ const int IndustryCargoesWindow::VERT_TEXT_PADDING = 5; ///< Vertical padding ar
|
||||||
static void ShowIndustryCargoesWindow(IndustryType id)
|
static void ShowIndustryCargoesWindow(IndustryType id)
|
||||||
{
|
{
|
||||||
if (id >= NUM_INDUSTRYTYPES) {
|
if (id >= NUM_INDUSTRYTYPES) {
|
||||||
for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
|
for (IndustryType ind : _sorted_industry_types) {
|
||||||
const IndustrySpec *indsp = GetIndustrySpec(_sorted_industry_types[i]);
|
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
||||||
if (indsp->enabled) {
|
if (indsp->enabled) {
|
||||||
id = _sorted_industry_types[i];
|
id = ind;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#ifndef INDUSTRYTYPE_H
|
#ifndef INDUSTRYTYPE_H
|
||||||
#define INDUSTRYTYPE_H
|
#define INDUSTRYTYPE_H
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include "map_type.h"
|
#include "map_type.h"
|
||||||
#include "slope_type.h"
|
#include "slope_type.h"
|
||||||
#include "industry_type.h"
|
#include "industry_type.h"
|
||||||
|
@ -179,7 +180,7 @@ extern IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
|
||||||
/* industry_gui.cpp */
|
/* industry_gui.cpp */
|
||||||
void SortIndustryTypes();
|
void SortIndustryTypes();
|
||||||
/* Industry types sorted alphabetically by name. */
|
/* Industry types sorted alphabetically by name. */
|
||||||
extern IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
|
extern std::array<IndustryType, NUM_INDUSTRYTYPES> _sorted_industry_types;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do industry gfx ID translation for NewGRFs.
|
* Do industry gfx ID translation for NewGRFs.
|
||||||
|
|
|
@ -178,8 +178,7 @@ void BuildIndustriesLegend()
|
||||||
uint j = 0;
|
uint j = 0;
|
||||||
|
|
||||||
/* Add each name */
|
/* Add each name */
|
||||||
for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
|
for (IndustryType ind : _sorted_industry_types) {
|
||||||
IndustryType ind = _sorted_industry_types[i];
|
|
||||||
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
const IndustrySpec *indsp = GetIndustrySpec(ind);
|
||||||
if (indsp->enabled) {
|
if (indsp->enabled) {
|
||||||
_legend_from_industries[j].legend = indsp->name;
|
_legend_from_industries[j].legend = indsp->name;
|
||||||
|
|
Loading…
Reference in New Issue