Changeset - r12428:f9fdea44ada5
[Not reviewed]
master
0 3 0
frosch - 15 years ago 2009-07-18 12:34:19
frosch@openttd.org
(svn r16872) -Codechange: Add RoadVehicle::GetDisplayImageWidth and simplify DrawRoadVehImage.
3 files changed with 27 insertions and 29 deletions:
0 comments (0 inline, 0 general)
src/roadveh.h
Show inline comments
 
@@ -114,12 +114,13 @@ struct RoadVehicle : public SpecializedV
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
 
	bool IsPrimaryVehicle() const { return this->IsRoadVehFront(); }
 
	SpriteID GetImage(Direction direction) const;
 
	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();
 
	void OnNewDay();
 
	Trackdir GetVehicleTrackdir() const;
 
	TileIndex GetOrderStationLocation(StationID station);
src/roadveh_cmd.cpp
Show inline comments
 
@@ -82,12 +82,28 @@ static const TrackdirBits _road_exit_dir
 

	
 
/** Converts the exit direction of a depot to trackdir the vehicle is going to drive to */
 
static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
 
	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;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
src/roadveh_gui.cpp
Show inline comments
 
@@ -3,13 +3,12 @@
 
/** @file roadveh_gui.cpp GUI for road vehicles. */
 

	
 
#include "stdafx.h"
 
#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"
 
#include "string_func.h"
 

	
 
#include "table/sprites.h"
 
@@ -112,53 +111,35 @@ void DrawRoadVehDetails(const Vehicle *v
 

	
 
	/* Draw Transfer credits text */
 
	SetDParam(0, feeder_share);
 
	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
 
 + @param x x Position to start at
 
 * @param y y Position to draw at
 
 * @param seletion Selected vehicle to draw a border around
 
 * @param max_width Number of pixels space for drawing
 
 */
 
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);
 
	}
 
}
 

	
 
void CcBuildRoadVeh(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	const Vehicle *v;
0 comments (0 inline, 0 general)