# HG changeset patch # User smatz # Date 2010-05-03 23:36:17 # Node ID 4610e022866de291efa9f677abecf41b1a6a06a0 # Parent ab250200d5be664dc439c3fc9014d3eb1926af47 (svn r19756) -Codechange: move UpdateViewport() from Vehicle to SpecializedVehicle in order to improve performance diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -437,7 +437,7 @@ CommandCost CmdTurnRoadVeh(TileIndex til void RoadVehicle::MarkDirty() { - for (Vehicle *v = this; v != NULL; v = v->Next()) { + for (RoadVehicle *v = this; v != NULL; v = v->Next()) { v->UpdateViewport(false, false); } this->CargoChanged(); diff --git a/src/ship.h b/src/ship.h --- a/src/ship.h +++ b/src/ship.h @@ -14,7 +14,7 @@ #include "vehicle_base.h" -void RecalcShipStuff(Vehicle *v); +void RecalcShipStuff(Ship *v); void GetShipSpriteSize(EngineID engine, uint &width, uint &height); /** diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -292,7 +292,7 @@ void Ship::UpdateDeltaXY(Direction direc this->z_extent = 6; } -void RecalcShipStuff(Vehicle *v) +void RecalcShipStuff(Ship *v) { v->UpdateViewport(false, true); SetWindowDirty(WC_VEHICLE_DEPOT, v->tile); diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1771,7 +1771,7 @@ static void ReverseTrainDirection(Train v->ConsistChanged(true); /* update all images */ - for (Vehicle *u = v; u != NULL; u = u->Next()) u->UpdateViewport(false, false); + for (Train *u = v; u != NULL; u = u->Next()) u->UpdateViewport(false, false); /* update crossing we were approaching */ if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing); @@ -2806,7 +2806,7 @@ TileIndex Train::GetOrderStationLocation void Train::MarkDirty() { - Vehicle *v = this; + Train *v = this; do { v->UpdateViewport(false, false); } while ((v = v->Next()) != NULL); diff --git a/src/vehicle.cpp b/src/vehicle.cpp --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1069,7 +1069,7 @@ void VehicleEnterDepot(Vehicle *v) case VEH_SHIP: SetWindowClassesDirty(WC_SHIPS_LIST); Ship::From(v)->state = TRACK_BIT_DEPOT; - RecalcShipStuff(v); + RecalcShipStuff(Ship::From(v)); break; case VEH_AIRCRAFT: diff --git a/src/vehicle_base.h b/src/vehicle_base.h --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -318,21 +318,6 @@ public: virtual uint Crash(bool flooded = false); /** - * Update vehicle sprite- and position caches - * @param moved Was the vehicle moved? - * @param turned Did the vehicle direction change? - */ - inline void UpdateViewport(bool moved, bool turned) - { - extern void VehicleMove(Vehicle *v, bool update_viewport); - - if (turned) this->UpdateDeltaXY(this->direction); - SpriteID old_image = this->cur_image; - this->cur_image = this->GetImage(this->direction); - if (moved || this->cur_image != old_image) VehicleMove(this, true); - } - - /** * Returns the Trackdir on which the vehicle is currently located. * Works for trains and ships. * Currently works only sortof for road vehicles, since they have a fuzzy @@ -661,6 +646,23 @@ struct SpecializedVehicle : public Vehic assert(v->type == Type); return (const T *)v; } + + /** + * Update vehicle sprite- and position caches + * @param moved Was the vehicle moved? + * @param turned Did the vehicle direction change? + */ + FORCEINLINE void UpdateViewport(bool moved, bool turned) + { + extern void VehicleMove(Vehicle *v, bool update_viewport); + + /* Explicitly choose method to call to prevent vtable dereference - + * it gives ~3% runtime improvements in games with many vehicles */ + if (turned) ((T *)this)->T::UpdateDeltaXY(this->direction); + SpriteID old_image = this->cur_image; + this->cur_image = ((T *)this)->T::GetImage(this->direction); + if (moved || this->cur_image != old_image) VehicleMove(this, true); + } }; #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)