diff --git a/src/roadveh.h b/src/roadveh.h --- a/src/roadveh.h +++ b/src/roadveh.h @@ -117,6 +117,7 @@ struct RoadVehicle : public SpecializedV int GetDisplaySpeed() const { return this->cur_speed / 2; } int GetDisplayMaxSpeed() const { return this->max_speed / 2; } Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * GetPriceByIndex(RoadVehInfo(this->engine_type)->running_cost_class); } + int GetDisplayImageWidth(Point *offset = NULL) const; bool IsInDepot() const { return this->state == RVSB_IN_DEPOT; } bool IsStoppedInDepot() const; bool Tick(); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -85,6 +85,22 @@ static const Trackdir _roadveh_depot_exi TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW }; +/** + * Get the width of a road vehicle image in the GUI. + * @param offset Additional offset for positioning the sprite; set to NULL if not needed + * @return Width in pixels + */ +int RoadVehicle::GetDisplayImageWidth(Point *offset) const +{ + int reference_width = ROADVEHINFO_DEFAULT_VEHICLE_WIDTH; + + if (offset != NULL) { + offset->x = reference_width / 2; + offset->y = 0; + } + return this->rcache.cached_veh_length * reference_width / 8; +} + static SpriteID GetRoadVehIcon(EngineID engine) { uint8 spritenum = RoadVehInfo(engine)->image_index; diff --git a/src/roadveh_gui.cpp b/src/roadveh_gui.cpp --- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -6,7 +6,6 @@ #include "roadveh.h" #include "window_gui.h" #include "gfx_func.h" -#include "newgrf_engine.h" #include "vehicle_gui.h" #include "strings_func.h" #include "vehicle_func.h" @@ -115,12 +114,6 @@ void DrawRoadVehDetails(const Vehicle *v DrawString(left, right, y + 33 + y_offset, STR_FEEDER_CARGO_VALUE); } - -static inline int RoadVehLengthToPixels(int length) -{ - return (length * ROADVEHINFO_DEFAULT_VEHICLE_WIDTH) / 8; -} - /** * Draws an image of a road vehicle chain * @param v Front vehicle @@ -131,31 +124,19 @@ static inline int RoadVehLengthToPixels( */ void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection, int max_width) { - int max_length = max_width / ROADVEHINFO_DEFAULT_VEHICLE_WIDTH; - - /* Width of highlight box */ - int highlight_w = 0; - - for (int dx = 0; v != NULL && dx < max_length ; v = v->Next()) { - int width = RoadVehicle::From(v)->rcache.cached_veh_length; + const RoadVehicle *u = RoadVehicle::From(v); + int x_pos = 0; + for (; u != NULL && x_pos < max_width; u = u->Next()) { + Point offset; + int width = u->GetDisplayImageWidth(&offset); - if (dx + width > 0 && dx <= max_length) { - SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); - DrawSprite(v->GetImage(DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6); - - if (v->index == selection) { - /* Set the highlight position */ - highlight_w = RoadVehLengthToPixels(width); - } else if (_cursor.vehchain && highlight_w != 0) { - highlight_w += RoadVehLengthToPixels(width); - } - } - - dx += width; + SpriteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u); + DrawSprite(u->GetImage(DIR_W), pal, x + x_pos + offset.x, y + 6 + offset.y); + x_pos += width; } - if (highlight_w != 0) { - DrawFrameRect(x - 1, y - 1, x - 1 + highlight_w, y + 12, COLOUR_WHITE, FR_BORDERONLY); + if (v->index == selection) { + DrawFrameRect(x - 1, y - 1, x - 1 + x_pos, y + 12, COLOUR_WHITE, FR_BORDERONLY); } }