summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-03-02 00:25:54 +0000
committersmatz <smatz@openttd.org>2008-03-02 00:25:54 +0000
commit620695587d8bf24bc77ebbd4fb1b3207f1eeaeda (patch)
tree5c0bcb68f46575b0376dbb6a97fd245ef173f99d /src
parent8dacca9a71d69aae159b2e81a0fb55f22ef928d5 (diff)
downloadopenttd-620695587d8bf24bc77ebbd4fb1b3207f1eeaeda.tar.xz
(svn r12324) -Fix (r6789): vehicles could be sorted in a wrong order when a vehicle name changed - cached name was not invalidated
Diffstat (limited to 'src')
-rw-r--r--src/vehicle_gui.cpp24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp
index bc35c2479..57fabec4e 100644
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -145,10 +145,17 @@ void BuildVehicleList(vehiclelist_d *vl, PlayerID owner, uint16 index, uint16 wi
vl->l.flags |= VL_RESORT;
}
+/* cached values for VehicleNameSorter to spare many GetString() calls */
+static const Vehicle *_last_vehicle[2] = { NULL, NULL };
+static char _last_name[2][64] = { "", "" };
+
void SortVehicleList(vehiclelist_d *vl)
{
if (!(vl->l.flags & VL_RESORT)) return;
+ /* invalidate cached values for name sorter - vehicle names could change */
+ _last_vehicle[0] = _last_vehicle[1] = NULL;
+
_internal_sort_order = (vl->l.flags & VL_DESC) != 0;
qsort((void*)vl->sort_list, vl->l.list_length, sizeof(vl->sort_list[0]),
_vehicle_sorter[vl->l.sort_type]);
@@ -546,26 +553,23 @@ static int CDECL VehicleNumberSorter(const void *a, const void *b)
static int CDECL VehicleNameSorter(const void *a, const void *b)
{
- static const Vehicle *last_vehicle[2] = { NULL, NULL };
- static char last_name[2][64] = { "", "" };
-
const Vehicle* va = *(const Vehicle**)a;
const Vehicle* vb = *(const Vehicle**)b;
int r;
- if (va != last_vehicle[0]) {
- last_vehicle[0] = va;
+ if (va != _last_vehicle[0]) {
+ _last_vehicle[0] = va;
SetDParam(0, va->index);
- GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
+ GetString(_last_name[0], STR_VEHICLE_NAME, lastof(_last_name[0]));
}
- if (vb != last_vehicle[1]) {
- last_vehicle[1] = vb;
+ if (vb != _last_vehicle[1]) {
+ _last_vehicle[1] = vb;
SetDParam(0, vb->index);
- GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
+ GetString(_last_name[1], STR_VEHICLE_NAME, lastof(_last_name[1]));
}
- r = strcmp(last_name[0], last_name[1]); // sort by name
+ r = strcmp(_last_name[0], _last_name[1]); // sort by name
VEHICLEUNITNUMBERSORTER(r, va, vb);