summaryrefslogtreecommitdiff
path: root/src/vehicle_gui.cpp
diff options
context:
space:
mode:
authorterkhen <terkhen@openttd.org>2010-12-21 13:54:57 +0000
committerterkhen <terkhen@openttd.org>2010-12-21 13:54:57 +0000
commit759a596e0b390752bf59b8b0c4a757635de89f36 (patch)
treeefe7c67ada7b409d10f6a6c7bfc7477f024bb612 /src/vehicle_gui.cpp
parent8fc48a79dae5d60a8b1c3db7d663f1188e2aba2c (diff)
downloadopenttd-759a596e0b390752bf59b8b0c4a757635de89f36.tar.xz
(svn r21563) -Codechange: Add function to get the width of a vehicle.
Diffstat (limited to 'src/vehicle_gui.cpp')
-rw-r--r--src/vehicle_gui.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp
index 7ca9f6fc1..5393a5a71 100644
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -2397,3 +2397,37 @@ void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1,
const Vehicle *v = Vehicle::Get(_new_vehicle_id);
ShowVehicleViewWindow(v);
}
+
+/**
+ * Get the width of a vehicle (including all parts of the consist) in pixels.
+ * @param v Vehicle to get the width for.
+ * @return Width of the vehicle.
+ */
+int GetVehicleWidth(Vehicle *v)
+{
+ int vehicle_width = 0;
+
+ switch (v->type) {
+ case VEH_TRAIN:
+ for (const Train *u = Train::From(v); u != NULL; u = u->Next()) {
+ vehicle_width += u->GetDisplayImageWidth();
+ }
+ break;
+
+ case VEH_ROAD:
+ for (const RoadVehicle *u = RoadVehicle::From(v); u != NULL; u = u->Next()) {
+ vehicle_width += u->GetDisplayImageWidth();
+ }
+ break;
+
+ default:
+ bool rtl = _current_text_dir == TD_RTL;
+ SpriteID sprite = v->GetImage(rtl ? DIR_E : DIR_W);
+ const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
+ vehicle_width = real_sprite->width;
+
+ break;
+ }
+
+ return vehicle_width;
+}