summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorterkhen <terkhen@openttd.org>2010-03-28 12:25:09 +0000
committerterkhen <terkhen@openttd.org>2010-03-28 12:25:09 +0000
commit217a98e69b56d74b53d915d15bd73e000005935d (patch)
treec4869b011ba7db7dc8c5a0b68a58f59c4d3cadd5
parent5193ed0fc1aecf9492a29e6ce87f07ee53d21ab6 (diff)
downloadopenttd-217a98e69b56d74b53d915d15bd73e000005935d.tar.xz
(svn r19521) -Codechange: Use a IndustryType array to sort industries by name instead of a LegendAndColour array.
-rw-r--r--src/industry_gui.cpp34
-rw-r--r--src/industrytype.h5
-rw-r--r--src/newgrf.cpp3
-rw-r--r--src/smallmap_gui.cpp38
-rw-r--r--src/strings.cpp4
5 files changed, 52 insertions, 32 deletions
diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp
index a0bebce8a..1bf565656 100644
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -96,6 +96,40 @@ static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, cons
}
}
+IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
+
+/** Sort industry types by their name. */
+static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
+{
+ static char industry_name[2][64];
+
+ const IndustrySpec *indsp1 = GetIndustrySpec(*a);
+ SetDParam(0, indsp1->name);
+ GetString(industry_name[0], STR_JUST_STRING, lastof(industry_name[0]));
+
+ const IndustrySpec *indsp2 = GetIndustrySpec(*b);
+ SetDParam(0, indsp2->name);
+ GetString(industry_name[1], STR_JUST_STRING, lastof(industry_name[1]));
+
+ int r = strcmp(industry_name[0], industry_name[1]);
+
+ /* If the names are equal, sort by industry type. */
+ return (r != 0) ? r : (*a - *b);
+}
+
+/** Initialize the list of sorted industry types.
+ */
+void SortIndustryTypes()
+{
+ /* Add each industry type to the list. */
+ for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
+ _sorted_industry_types[i] = i;
+ }
+
+ /* Sort industry types by name. */
+ QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
+}
+
/** Command callback. In case of failure to build an industry, show an error message.
* @param result Result of the command.
* @param tile Tile where the industry is placed.
diff --git a/src/industrytype.h b/src/industrytype.h
index b4d653c53..e41b0ed8e 100644
--- a/src/industrytype.h
+++ b/src/industrytype.h
@@ -183,6 +183,11 @@ void ResetIndustries();
extern IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
extern IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
+/* industry_gui.cpp */
+void SortIndustryTypes();
+/* Industry types sorted alphabetically by name. */
+extern IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
+
/**
* Do industry gfx ID translation for NewGRFs.
* @param gfx the type to get the override for.
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index 6f9b54deb..e548683b7 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -6809,6 +6809,9 @@ static void AfterLoadGRFs()
/* Add all new industries to the industry array. */
FinaliseIndustriesArray();
+ /* Sort the list of industry types. */
+ SortIndustryTypes();
+
/* Create dynamic list of industry legends for smallmap_gui.cpp */
BuildIndustriesLegend();
diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp
index 226ac6e1d..28c56e7c1 100644
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -26,7 +26,6 @@
#include "sound_func.h"
#include "window_func.h"
#include "company_base.h"
-#include "core/sort_func.hpp"
#include "table/strings.h"
#include "table/sprites.h"
@@ -152,33 +151,6 @@ static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
/** Show heightmap in industry mode of smallmap window. */
static bool _smallmap_industry_show_heightmap;
-/** Sort legends by their name. */
-static int CDECL LegendNameSorter(const LegendAndColour *a, const LegendAndColour *b)
-{
- static char industry_name[2][64];
-
- SetDParam(0, a->legend);
- GetString(industry_name[0], STR_JUST_STRING, lastof(industry_name[0]));
-
- SetDParam(0, b->legend);
- GetString(industry_name[1], STR_JUST_STRING, lastof(industry_name[1]));
-
- int r = strcmp(industry_name[0], industry_name[1]);
- /* If the names are equal, sort by industry type. */
- return (r != 0) ? r : (a->type - b->type);
-}
-
-void SortIndustriesLegend()
-{
- /* Sort industries by name. */
- GSortT(_legend_from_industries, _smallmap_industry_count, &LegendNameSorter);
-
- /* Store widget number for each industry type. */
- for (int i = 0; i < _smallmap_industry_count; i++) {
- _industry_to_list_pos[_legend_from_industries[i].type] = i;
- }
-}
-
/**
* Fills an array for the industries legends.
*/
@@ -187,16 +159,19 @@ void BuildIndustriesLegend()
uint j = 0;
/* Add each name */
- for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
- const IndustrySpec *indsp = GetIndustrySpec(i);
+ for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
+ IndustryType ind = _sorted_industry_types[i];
+ const IndustrySpec *indsp = GetIndustrySpec(ind);
if (indsp->enabled) {
_legend_from_industries[j].legend = indsp->name;
_legend_from_industries[j].colour = indsp->map_colour;
- _legend_from_industries[j].type = i;
+ _legend_from_industries[j].type = ind;
_legend_from_industries[j].show_on_map = true;
_legend_from_industries[j].col_break = false;
_legend_from_industries[j].end = false;
+ /* Store widget number for this industry type. */
+ _industry_to_list_pos[ind] = j;
j++;
}
}
@@ -1050,7 +1025,6 @@ public:
virtual void OnInit()
{
- SortIndustriesLegend();
uint min_width = 0;
this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
this->min_number_of_fixed_rows = 0;
diff --git a/src/strings.cpp b/src/strings.cpp
index 77f47843b..2d425834a 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -33,6 +33,8 @@
#include "townname_func.h"
#include "string_func.h"
#include "company_base.h"
+#include "industrytype.h"
+#include "smallmap_gui.h"
#include "table/strings.h"
#include "table/control_codes.h"
@@ -1324,6 +1326,8 @@ bool ReadLanguagePack(int lang_index)
_dynlang.curr = lang_index;
_dynlang.text_dir = (TextDirection)lang_pack->text_dir;
SetCurrentGrfLangID(_langpack->newgrflangid);
+ SortIndustryTypes();
+ BuildIndustriesLegend();
SortNetworkLanguages();
return true;
}