diff options
author | smatz <smatz@openttd.org> | 2008-03-02 00:25:54 +0000 |
---|---|---|
committer | smatz <smatz@openttd.org> | 2008-03-02 00:25:54 +0000 |
commit | d28cc5be804be6f7239456f6c61e61e394b23d38 (patch) | |
tree | 5c0bcb68f46575b0376dbb6a97fd245ef173f99d | |
parent | 4a395562f20720f075c0e2c1ea21c335696d60e9 (diff) | |
download | openttd-d28cc5be804be6f7239456f6c61e61e394b23d38.tar.xz |
(svn r12324) -Fix (r6789): vehicles could be sorted in a wrong order when a vehicle name changed - cached name was not invalidated
-rw-r--r-- | src/vehicle_gui.cpp | 24 |
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); |