(svn r5907) Remove more indirection by using pointers instead of IDs. Also fix some bogus warnings on MSVC by using (void*) casts
authortron
Tue, 15 Aug 2006 07:07:17 +0000
changeset 4277 345e1bd9525a
parent 4276 cbdc8fa50e8b
child 4278 761e05083f9c
(svn r5907) Remove more indirection by using pointers instead of IDs. Also fix some bogus warnings on MSVC by using (void*) casts
industry.h
industry_gui.c
openttd.c
station_gui.c
town.h
town_gui.c
vehicle_gui.c
--- a/industry.h	Tue Aug 15 01:28:07 2006 +0000
+++ b/industry.h	Tue Aug 15 07:07:17 2006 +0000
@@ -96,7 +96,7 @@
 
 VARDEF int _total_industries; // For the AI: the amount of industries active
 
-VARDEF uint16 *_industry_sort;
+VARDEF const Industry** _industry_sort;
 VARDEF bool _industry_sort_dirty;
 
 
--- a/industry_gui.c	Tue Aug 15 01:28:07 2006 +0000
+++ b/industry_gui.c	Tue Aug 15 07:07:17 2006 +0000
@@ -470,16 +470,15 @@
 static uint _num_industry_sort;
 
 static char _bufcache[96];
-static uint16 _last_industry_idx;
+static const Industry* _last_industry;
 
 static byte _industry_sort_order;
 
 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
 {
+	const Industry* i = *(const Industry**)a;
+	const Industry* j = *(const Industry**)b;
 	char buf1[96];
-	uint16 val;
-	Industry *i = GetIndustry(*(const uint16*)a);
-	Industry *j = GetIndustry(*(const uint16*)b);
 	int r = 0;
 
 	switch (_industry_sort_order >> 1) {
@@ -523,8 +522,8 @@
 		SetDParam(0, i->town->index);
 		GetString(buf1, STR_TOWN);
 
-		if ( (val=*(const uint16*)b) != _last_industry_idx) {
-			_last_industry_idx = val;
+		if (j != _last_industry) {
+			_last_industry = j;
 			SetDParam(0, j->town->index);
 			GetString(_bufcache, STR_TOWN);
 		}
@@ -537,21 +536,21 @@
 
 static void MakeSortedIndustryList(void)
 {
-	Industry *i;
+	const Industry* i;
 	int n = 0;
 
 	/* Create array for sorting */
-	_industry_sort = realloc(_industry_sort, GetIndustryPoolSize() * sizeof(_industry_sort[0]));
+	_industry_sort = realloc((void*)_industry_sort, GetIndustryPoolSize() * sizeof(_industry_sort[0]));
 	if (_industry_sort == NULL)
 		error("Could not allocate memory for the industry-sorting-list");
 
 	FOR_ALL_INDUSTRIES(i) {
-		if (i->xy != 0) _industry_sort[n++] = i->index;
+		if (i->xy != 0) _industry_sort[n++] = i;
 	}
 	_num_industry_sort = n;
-	_last_industry_idx = 0xFFFF; // used for "cache"
+	_last_industry = NULL; // used for "cache"
 
-	qsort(_industry_sort, n, sizeof(_industry_sort[0]), GeneralIndustrySorter);
+	qsort((void*)_industry_sort, n, sizeof(_industry_sort[0]), GeneralIndustrySorter);
 
 	DEBUG(misc, 1) ("Resorting Industries list...");
 }
@@ -579,7 +578,7 @@
 		n = 0;
 
 		while (p < _num_industry_sort) {
-			const Industry *i = GetIndustry(_industry_sort[p]);
+			const Industry* i = _industry_sort[p];
 
 			SetDParam(0, i->index);
 			if (i->produced_cargo[0] != CT_INVALID) {
@@ -637,7 +636,7 @@
 			if (!IS_INT_INSIDE(y, 0, w->vscroll.cap)) return;
 			p = y + w->vscroll.pos;
 			if (p < _num_industry_sort) {
-				ScrollMainWindowToTile(GetIndustry(_industry_sort[p])->xy);
+				ScrollMainWindowToTile(_industry_sort[p]->xy);
 			}
 		} break;
 		}
--- a/openttd.c	Tue Aug 15 01:28:07 2006 +0000
+++ b/openttd.c	Tue Aug 15 07:07:17 2006 +0000
@@ -260,8 +260,8 @@
 	CleanPool(&_sign_pool);
 	CleanPool(&_order_pool);
 
-	free(_town_sort);
-	free(_industry_sort);
+	free((void*)_town_sort);
+	free((void*)_industry_sort);
 }
 
 static void UnInitializeGame(void)
--- a/station_gui.c	Tue Aug 15 01:28:07 2006 +0000
+++ b/station_gui.c	Tue Aug 15 07:07:17 2006 +0000
@@ -69,7 +69,7 @@
 };
 
 static char _bufcache[64];
-static uint16 _last_station_idx;
+static const Station* _last_station;
 static int _internal_sort_order;
 
 static int CDECL StationNameSorter(const void *a, const void *b)
@@ -83,8 +83,8 @@
 	argv[0] = st1->index;
 	GetStringWithArgs(buf1, STR_STATION, argv);
 
-	if (st2->index != _last_station_idx) {
-		_last_station_idx = st2->index;
+	if (st2 != _last_station) {
+		_last_station = st2;
 		argv[0] = st2->index;
 		GetStringWithArgs(_bufcache, STR_STATION, argv);
 	}
@@ -207,7 +207,7 @@
 		}
 	}
 
-	free(sl->sort_list);
+	free((void*)sl->sort_list);
 	sl->sort_list = malloc(n * sizeof(sl->sort_list[0]));
 	if (n != 0 && sl->sort_list == NULL) error("Could not allocate memory for the station-sorting-list");
 	sl->list_length = n;
@@ -216,7 +216,7 @@
 
 	sl->flags &= ~SL_REBUILD;
 	sl->flags |= SL_RESORT;
-	free(station_sort);
+	free((void*)station_sort);
 }
 
 static void SortStationsList(plstations_d *sl)
@@ -231,7 +231,7 @@
 	if (!(sl->flags & SL_RESORT)) return;
 
 	_internal_sort_order = sl->flags & SL_ORDER;
-	_last_station_idx = 0; // used for "cache" in namesorting
+	_last_station = NULL; // used for "cache" in namesorting
 	qsort(sl->sort_list, sl->list_length, sizeof(sl->sort_list[0]), _station_sorter[sl->sort_type]);
 
 	sl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
--- a/town.h	Tue Aug 15 01:28:07 2006 +0000
+++ b/town.h	Tue Aug 15 07:07:17 2006 +0000
@@ -151,7 +151,7 @@
 
 bool CheckforTownRating(uint32 flags, Town *t, byte type);
 
-VARDEF TownID *_town_sort;
+VARDEF const Town** _town_sort;
 
 extern MemoryPool _town_pool;
 
--- a/town_gui.c	Tue Aug 15 01:28:07 2006 +0000
+++ b/town_gui.c	Tue Aug 15 07:07:17 2006 +0000
@@ -368,25 +368,25 @@
 static uint _num_town_sort;
 
 static char _bufcache[64];
-static uint16 _last_town_idx;
+static const Town* _last_town;
 
 static int CDECL TownNameSorter(const void *a, const void *b)
 {
+	const Town* ta = *(const Town**)a;
+	const Town* tb = *(const Town**)b;
 	char buf1[64];
-	uint16 val;
 	int r;
 	int32 argv[1];
 
-	argv[0] = *(const uint16*)a;
+	argv[0] = ta->index;
 	GetStringWithArgs(buf1, STR_TOWN, argv);
 
 	/* If 'b' is the same town as in the last round, use the cached value
 	 *  We do this to speed stuff up ('b' is called with the same value a lot of
-	*  times after eachother) */
-	val = *(const uint16*)b;
-	if (val != _last_town_idx) {
-		_last_town_idx = val;
-		argv[0] = val;
+	 *  times after eachother) */
+	if (tb != _last_town) {
+		_last_town = tb;
+		argv[0] = tb->index;
 		GetStringWithArgs(_bufcache, STR_TOWN, argv);
 	}
 
@@ -397,8 +397,8 @@
 
 static int CDECL TownPopSorter(const void *a, const void *b)
 {
-	const Town *ta = GetTown(*(const uint16*)a);
-	const Town *tb = GetTown(*(const uint16*)b);
+	const Town* ta = *(const Town**)a;
+	const Town* tb = *(const Town**)b;
 	int r = ta->population - tb->population;
 	if (_town_sort_order & 1) r = -r;
 	return r;
@@ -410,18 +410,18 @@
 	uint n = 0;
 
 	/* Create array for sorting */
-	_town_sort = realloc(_town_sort, GetTownPoolSize() * sizeof(_town_sort[0]));
+	_town_sort = realloc((void*)_town_sort, GetTownPoolSize() * sizeof(_town_sort[0]));
 	if (_town_sort == NULL)
 		error("Could not allocate memory for the town-sorting-list");
 
 	FOR_ALL_TOWNS(t) {
-		if (t->xy != 0) _town_sort[n++] = t->index;
+		if (t->xy != 0) _town_sort[n++] = t;
 	}
 
 	_num_town_sort = n;
 
-	_last_town_idx = 0; // used for "cache"
-	qsort(_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter);
+	_last_town = NULL; // used for "cache"
+	qsort((void*)_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter);
 
 	DEBUG(misc, 1) ("Resorting Towns list...");
 }
@@ -442,13 +442,12 @@
 		DoDrawString(_town_sort_order & 1 ? DOWNARROW : UPARROW, (_town_sort_order <= 1) ? 88 : 187, 15, 0x10);
 
 		{
-			const Town *t;
 			int n = 0;
 			uint16 i = w->vscroll.pos;
 			int y = 28;
 
 			while (i < _num_town_sort) {
-				t = GetTown(_town_sort[i]);
+				const Town* t = _town_sort[i];
 
 				assert(t->xy);
 
@@ -480,6 +479,8 @@
 		} break;
 
 		case 5: { /* Click on Town Matrix */
+			const Town* t;
+
 			uint16 id_v = (e->click.pt.y - 28) / 10;
 
 			if (id_v >= w->vscroll.cap) return; // click out of bounds
@@ -488,13 +489,11 @@
 
 			if (id_v >= _num_town_sort) return; // click out of town bounds
 
-			{
-				const Town *t = GetTown(_town_sort[id_v]);
-				assert(t->xy);
-
-				ScrollMainWindowToTile(t->xy);
-			}
-		}	break;
+			t = _town_sort[id_v];
+			assert(t->xy);
+			ScrollMainWindowToTile(t->xy);
+			break;
+		}
 		}
 		break;
 
--- a/vehicle_gui.c	Tue Aug 15 01:28:07 2006 +0000
+++ b/vehicle_gui.c	Tue Aug 15 07:07:17 2006 +0000
@@ -27,7 +27,7 @@
 Sorting _sorting;
 
 static uint32 _internal_name_sorter_id; // internal StringID for default vehicle-names
-static uint32 _last_vehicle_idx;        // cached index to hopefully speed up name-sorting
+static const Vehicle* _last_vehicle; // cached vehicle to hopefully speed up name-sorting
 static bool   _internal_sort_order;     // descending/ascending
 
 static uint16 _player_num_engines[TOTAL_NUM_ENGINES];
@@ -161,7 +161,7 @@
 		}
 	}
 
-	free(vl->sort_list);
+	free((void*)vl->sort_list);
 	vl->sort_list = malloc(n * sizeof(vl->sort_list[0]));
 	if (n != 0 && vl->sort_list == NULL) {
 		error("Could not allocate memory for the vehicle-sorting-list");
@@ -169,7 +169,7 @@
 	vl->list_length = n;
 
 	for (i = 0; i < n; ++i) vl->sort_list[i] = sort_list[i];
-	free(sort_list);
+	free((void*)sort_list);
 
 	vl->flags &= ~VL_REBUILD;
 	vl->flags |= VL_RESORT;
@@ -181,7 +181,7 @@
 
 	_internal_sort_order = vl->flags & VL_DESC;
 	_internal_name_sorter_id = STR_SV_TRAIN_NAME;
-	_last_vehicle_idx = 0; // used for "cache" in namesorting
+	_last_vehicle = NULL; // used for "cache" in namesorting
 	qsort(vl->sort_list, vl->list_length, sizeof(vl->sort_list[0]),
 		_vehicle_sorter[vl->sort_type]);
 
@@ -289,7 +289,7 @@
 	return (_internal_sort_order & 1) ? -r : r;
 }
 
-static char _bufcache[64];	// used together with _last_vehicle_idx to hopefully speed up stringsorting
+static char _bufcache[64]; // used together with _last_vehicle to hopefully speed up stringsorting
 static int CDECL VehicleNameSorter(const void *a, const void *b)
 {
 	const Vehicle* va = *(const Vehicle**)a;
@@ -302,8 +302,8 @@
 		GetString(buf1, STR_JUST_STRING);
 	}
 
-	if (vb->index != _last_vehicle_idx) {
-		_last_vehicle_idx = vb->index;
+	if (vb != _last_vehicle) {
+		_last_vehicle = vb;
 		_bufcache[0] = '\0';
 		if (vb->string_id != _internal_name_sorter_id) {
 			SetDParam(0, vb->string_id);