From 56f78e64120117db32642add944707a3cde3f96f Mon Sep 17 00:00:00 2001 From: peter1138 Date: Sun, 25 May 2008 17:22:49 +0000 Subject: (svn r13248) -Codechange: Use VehicleList for depot GUI. --- src/depot_gui.cpp | 56 +++++++++++++++++++---------------------------------- src/vehicle.cpp | 35 --------------------------------- src/vehicle_func.h | 1 - src/vehicle_gui.cpp | 5 +++-- 4 files changed, 23 insertions(+), 74 deletions(-) diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 072c10659..d767c395e 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -131,7 +131,7 @@ static const WindowDesc _aircraft_depot_desc = { }; extern int WagonLengthToPixels(int len); -extern void DepotSortList(Vehicle **v, uint16 length); +extern void DepotSortList(VehicleList *list); /** * This is the Callback method after the cloning attempt of a vehicle @@ -240,20 +240,12 @@ struct DepotWindow : Window { VehicleID sel; VehicleType type; bool generate_list; - uint16 engine_list_length; - uint16 wagon_list_length; - uint16 engine_count; - uint16 wagon_count; - Vehicle **vehicle_list; - Vehicle **wagon_list; + VehicleList vehicle_list; + VehicleList wagon_list; DepotWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) { this->sel = INVALID_VEHICLE; - this->vehicle_list = NULL; - this->wagon_list = NULL; - this->engine_count = 0; - this->wagon_count = 0; this->generate_list = true; this->FindWindowPlacementAndResize(desc); @@ -262,8 +254,6 @@ struct DepotWindow : Window { ~DepotWindow() { DeleteWindowById(WC_BUILD_VEHICLE, this->window_number); - free((void*)this->vehicle_list); - free((void*)this->wagon_list); } /** Draw a vehicle in the depot window in the box with the top left corner at x,y @@ -314,11 +304,9 @@ struct DepotWindow : Window { void DrawDepotWindow(Window *w) { - Vehicle **vl = this->vehicle_list; TileIndex tile = this->window_number; int x, y, i, maxval; uint16 hnum; - uint16 num = this->engine_count; /* Set the row and number of boxes in each row based on the number of boxes drawn in the matrix */ uint16 rows_in_display = this->widget[DEPOT_WIDGET_MATRIX].data >> 8; @@ -339,15 +327,15 @@ struct DepotWindow : Window { /* determine amount of items for scroller */ if (this->type == VEH_TRAIN) { hnum = 8; - for (num = 0; num < this->engine_count; num++) { - const Vehicle *v = vl[num]; + for (uint num = 0; num < this->vehicle_list.Length(); num++) { + const Vehicle *v = this->vehicle_list[num]; hnum = max(hnum, v->u.rail.cached_total_length); } /* Always have 1 empty row, so people can change the setting of the train */ - SetVScrollCount(w, this->engine_count + this->wagon_count + 1); + SetVScrollCount(w, this->vehicle_list.Length() + this->wagon_list.Length() + 1); SetHScrollCount(w, WagonLengthToPixels(hnum)); } else { - SetVScrollCount(w, (num + this->hscroll.cap - 1) / this->hscroll.cap); + SetVScrollCount(w, (this->vehicle_list.Length() + this->hscroll.cap - 1) / this->hscroll.cap); } /* locate the depot struct */ @@ -362,24 +350,24 @@ struct DepotWindow : Window { w->DrawWidgets(); - num = this->vscroll.pos * boxes_in_each_row; - maxval = min(this->engine_count, num + (rows_in_display * boxes_in_each_row)); + uint16 num = this->vscroll.pos * boxes_in_each_row; + maxval = min(this->vehicle_list.Length(), num + (rows_in_display * boxes_in_each_row)); for (x = 2, y = 15; num < maxval; y += this->resize.step_height, x = 2) { // Draw the rows byte i; for (i = 0; i < boxes_in_each_row && num < maxval; i++, num++, x += this->resize.step_width) { /* Draw all vehicles in the current row */ - const Vehicle *v = vl[num]; + const Vehicle *v = this->vehicle_list[num]; DrawVehicleInDepot(w, v, x, y); } } - maxval = min(this->engine_count + this->wagon_count, (this->vscroll.pos * boxes_in_each_row) + (rows_in_display * boxes_in_each_row)); + maxval = min(this->vehicle_list.Length() + this->wagon_list.Length(), (this->vscroll.pos * boxes_in_each_row) + (rows_in_display * boxes_in_each_row)); /* draw the train wagons, that do not have an engine in front */ for (; num < maxval; num++, y += 14) { - const Vehicle *v = this->wagon_list[num - this->engine_count]; + const Vehicle *v = this->wagon_list[num - this->vehicle_list.Length()]; const Vehicle *u; DrawTrainImage(v, x + 50, y, this->sel, this->hscroll.cap - 29, 0); @@ -408,7 +396,6 @@ struct DepotWindow : Window { DepotGUIAction GetVehicleFromDepotWndPt(int x, int y, const Vehicle **veh, GetDepotVehiclePtData *d) const { - Vehicle **vl = this->vehicle_list; uint xt, row, xm = 0, ym = 0; int pos, skip = 0; uint16 boxes_in_each_row = this->widget[DEPOT_WIDGET_MATRIX].data & 0xFF; @@ -429,7 +416,7 @@ struct DepotWindow : Window { pos = ((row + this->vscroll.pos) * boxes_in_each_row) + xt; - if (this->engine_count + this->wagon_count <= pos) { + if ((int)(this->vehicle_list.Length() + this->wagon_list.Length()) <= pos) { if (this->type == VEH_TRAIN) { d->head = NULL; d->wagon = NULL; @@ -439,13 +426,12 @@ struct DepotWindow : Window { } } - if (this->engine_count > pos) { - *veh = vl[pos]; + if ((int)this->vehicle_list.Length() > pos) { + *veh = this->vehicle_list[pos]; skip = this->hscroll.pos; } else { - vl = this->wagon_list; - pos -= this->engine_count; - *veh = vl[pos]; + pos -= this->vehicle_list.Length(); + *veh = this->wagon_list[pos]; /* free wagons don't have an initial loco. */ x -= _traininfo_vehicle_width; } @@ -759,11 +745,9 @@ struct DepotWindow : Window { if (this->generate_list) { /* Generate the vehicle list * It's ok to use the wagon pointers for non-trains as they will be ignored */ - BuildDepotVehicleList(this->type, this->window_number, - &this->vehicle_list, &this->engine_list_length, &this->engine_count, - &this->wagon_list, &this->wagon_list_length, &this->wagon_count); + BuildDepotVehicleList(this->type, this->window_number, &this->vehicle_list, &this->wagon_list); this->generate_list = false; - DepotSortList(this->vehicle_list, this->engine_count); + DepotSortList(&this->vehicle_list); } DrawDepotWindow(this); } @@ -812,7 +796,7 @@ struct DepotWindow : Window { case DEPOT_WIDGET_SELL_ALL: /* Only open the confimation window if there are anything to sell */ - if (this->engine_count != 0 || this->wagon_count != 0) { + if (this->vehicle_list.Length() != 0 || this->wagon_list.Length() != 0) { static const StringID confirm_captions[] = { STR_8800_TRAIN_DEPOT, STR_9003_ROAD_VEHICLE_DEPOT, diff --git a/src/vehicle.cpp b/src/vehicle.cpp index ba79ef830..67eed7a0d 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1291,41 +1291,6 @@ CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) return total_cost; } - -/** Generates a list of vehicles inside a depot - * Will enlarge allocated space for the list if they are too small, so it's ok to call with (pointer to NULL array, pointer to uninitised uint16, pointer to 0) - * If one of the lists is not needed (say wagons when finding ships), all the pointers regarding that list should be set to NULL - * @param type Type of vehicle - * @param tile The tile the depot is located in - * @param ***engine_list Pointer to a pointer to an array of vehicles in the depot (old list is freed and a new one is malloced) - * @param *engine_list_length Allocated size of engine_list. Needs to be set to 0 when engine_list points to a NULL array - * @param *engine_count The number of engines stored in the list - * @param ***wagon_list Pointer to a pointer to an array of free wagons in the depot (old list is freed and a new one is malloced) - * @param *wagon_list_length Allocated size of wagon_list. Needs to be set to 0 when wagon_list points to a NULL array - * @param *wagon_count The number of engines stored in the list - */ -void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count) -{ - VehicleList engines; - VehicleList wagons; - - BuildDepotVehicleList(type, tile, &engines, &wagons); - - if (engines.Length() > 0) { - *engine_list = ReallocT(*engine_list, engines.Length()); - memcpy(*engine_list, engines.Begin(), sizeof(engines.Begin()) * engines.Length()); - } - if (engine_count != NULL) *engine_count = engines.Length(); - - if (wagon_list != NULL && wagon_list != engine_list) { - if (wagons.Length() > 0) { - *wagon_list = ReallocT(*wagon_list, wagons.Length()); - memcpy(*wagon_list, wagons.Begin(), sizeof(wagons.Begin()) * wagons.Length()); - } - if (wagon_count != NULL) *wagon_count = wagons.Length(); - } -} - /** * Generate a list of vehicles inside a depot. * @param type Type of vehicle diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 8178e0eda..05daf0fb8 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -70,7 +70,6 @@ Money GetTrainRunningCost(const Vehicle *v); /* Old style list kept for migration */ uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type); -void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count); void GenerateVehicleSortList(VehicleList *list, VehicleType type, PlayerID owner, uint32 index, uint16 window_type); void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine_list, VehicleList *wagon_list); CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 422fd9671..ac745309b 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -117,10 +117,11 @@ void SortVehicleList(VehicleListBase *vl) vl->vehicles.flags &= ~VL_RESORT; } -void DepotSortList(Vehicle **v, uint16 length) +void DepotSortList(VehicleList *list) { _internal_sort_order = 0; - qsort((void*)v, length, sizeof(v[0]), _vehicle_sorter[0]); + if (list->Length() < 2) return; + qsort((void*)list->Begin(), list->Length(), sizeof(list->Begin()), _vehicle_sorter[0]); } /** draw the vehicle profit button in the vehicle list window. */ -- cgit v1.2.3-54-g00ecf