Changeset - r9022:5be8b7703ae9
[Not reviewed]
master
0 9 0
peter1138 - 17 years ago 2008-04-21 20:50:58
peter1138@openttd.org
(svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
9 files changed with 116 insertions and 128 deletions:
0 comments (0 inline, 0 general)
src/aircraft.h
Show inline comments
 
@@ -116,13 +116,13 @@ struct Aircraft : public Vehicle {
 
	const char *GetTypeString() const { return "aircraft"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_AIRCRAFT_INC : EXPENSES_AIRCRAFT_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_AIRCRAFT_LIST; }
 
	bool IsPrimaryVehicle() const { return IsNormalAircraft(this); }
 
	int GetImage(Direction direction) const;
 
	SpriteID GetImage(Direction direction) const;
 
	int GetDisplaySpeed() const { return this->cur_speed * 10 / 16; }
 
	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 16; }
 
	Money GetRunningCost() const { return AircraftVehInfo(this->engine_type)->running_cost * _price.aircraft_running; }
 
	bool IsInDepot() const { return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile); }
 
	void Tick();
 
	void OnNewDay();
src/aircraft_cmd.cpp
Show inline comments
 
@@ -157,58 +157,59 @@ static bool HaveHangarInOrderList(Vehicl
 
	}
 

	
 
	return false;
 
}
 
#endif
 

	
 
int Aircraft::GetImage(Direction direction) const
 
SpriteID Aircraft::GetImage(Direction direction) const
 
{
 
	int spritenum = this->spritenum;
 
	uint8 spritenum = this->spritenum;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		int sprite = GetCustomVehicleSprite(this, direction);
 
		SpriteID sprite = GetCustomVehicleSprite(this, direction);
 
		if (sprite != 0) return sprite;
 

	
 
		if (sprite != 0) return sprite;
 
		spritenum = _orig_aircraft_vehicle_info[this->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
 
	}
 

	
 
	return direction + _aircraft_sprite[spritenum];
 
}
 

	
 
SpriteID GetRotorImage(const Vehicle *v)
 
{
 
	assert(v->subtype == AIR_HELICOPTER);
 

	
 
	const Vehicle *w = v->Next()->Next();
 
	if (is_custom_sprite(v->spritenum)) {
 
		SpriteID spritenum = GetCustomRotorSprite(v, false);
 
		if (spritenum != 0) return spritenum;
 
		SpriteID sprite = GetCustomRotorSprite(v, false);
 
		if (sprite != 0) return sprite;
 
	}
 

	
 
	/* Return standard rotor sprites if there are no custom sprites for this helicopter */
 
	return SPR_ROTOR_STOPPED + w->u.air.state;
 
}
 

	
 
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
 
static SpriteID GetAircraftIcon(EngineID engine)
 
{
 
	const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
 
	int spritenum = avi->image_index;
 
	SpriteID sprite = 0;
 
	uint8 spritenum = AircraftVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite == 0) {
 
			spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
 
		}
 
	}
 
	if (sprite == 0) {
 
		sprite = 6 + _aircraft_sprite[spritenum];
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
 
	}
 

	
 
	DrawSprite(sprite, pal, x, y);
 
	return 6 + _aircraft_sprite[spritenum];
 
}
 

	
 
	if (!(avi->subtype & AIR_CTOL)) {
 
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
 
{
 
	DrawSprite(GetAircraftIcon(engine), pal, x, y);
 

	
 
	if (!(AircraftVehInfo(engine)->subtype & AIR_CTOL)) {
 
		SpriteID rotor_sprite = GetCustomRotorIcon(engine);
 
		if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
 
		DrawSprite(rotor_sprite, PAL_NONE, x, y - 5);
 
	}
 
}
 

	
 
@@ -216,27 +217,15 @@ void DrawAircraftEngine(int x, int y, En
 
 * @param engine The engine to get the sprite from
 
 * @param width The width of the sprite
 
 * @param height The height of the sprite
 
 */
 
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height)
 
{
 
	const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
 
	int spritenum = avi->image_index;
 
	SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
 
	const Sprite *spr = GetSprite(GetAircraftIcon(engine));
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite == 0) {
 
			spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
 
			sprite = (6 + _aircraft_sprite[spritenum]);
 
		}
 
	}
 

	
 
	const Sprite *spr = GetSprite(sprite);
 

	
 
	width  = spr->width ;
 
	width  = spr->width;
 
	height = spr->height;
 
}
 

	
 
static CommandCost EstimateAircraftCost(EngineID engine, const AircraftVehicleInfo *avi)
 
{
 
	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x0B, avi->base_cost) * (_price.aircraft_base >> 3) >> 5);
src/roadveh.h
Show inline comments
 
@@ -70,13 +70,13 @@ struct RoadVehicle : public Vehicle {
 
	const char *GetTypeString() const { return "road vehicle"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_ROADVEH_LIST; }
 
	bool IsPrimaryVehicle() const { return IsRoadVehFront(this); }
 
	int GetImage(Direction direction) const;
 
	SpriteID GetImage(Direction direction) const;
 
	int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
 
	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
 
	Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * GetPriceByIndex(RoadVehInfo(this->engine_type)->running_cost_class); }
 
	bool IsInDepot() const { return this->u.road.state == RVSB_IN_DEPOT; }
 
	void Tick();
 
	void OnNewDay();
src/roadveh_cmd.cpp
Show inline comments
 
@@ -91,42 +91,48 @@ 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
 
};
 

	
 
int RoadVehicle::GetImage(Direction direction) const
 
static SpriteID GetRoadVehIcon(EngineID engine)
 
{
 
	int img = this->spritenum;
 
	int image;
 
	uint8 spritenum = RoadVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(img)) {
 
		image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
 
		if (image != 0) return image;
 
		img = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
 
	}
 

	
 
	image = direction + _roadveh_images[img];
 
	if (this->cargo.Count() >= this->cargo_cap / 2U) image += _roadveh_full_adder[img];
 
	return image;
 
	return 6 + _roadveh_images[spritenum];
 
}
 

	
 
SpriteID RoadVehicle::GetImage(Direction direction) const
 
{
 
	uint8 spritenum = this->spritenum;
 
	SpriteID sprite;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
 
	}
 

	
 
	sprite = direction + _roadveh_images[spritenum];
 

	
 
	if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
 

	
 
	return sprite;
 
}
 

	
 
void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
 
{
 
	int spritenum = RoadVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		int sprite = GetCustomVehicleIcon(engine, DIR_W);
 

	
 
		if (sprite != 0) {
 
			DrawSprite(sprite, pal, x, y);
 
			return;
 
		}
 
		spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
 
	}
 
	DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
 
	DrawSprite(GetRoadVehIcon(engine), pal, x, y);
 
}
 

	
 
static CommandCost EstimateRoadVehCost(EngineID engine_type)
 
{
 
	return CommandCost(EXPENSES_NEW_VEHICLES, ((_price.roadveh_base >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->base_cost)) >> 5);
 
}
src/ship.h
Show inline comments
 
@@ -32,13 +32,13 @@ struct Ship: public Vehicle {
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_SHIP_INC : EXPENSES_SHIP_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_SHIPS_LIST; }
 
	void PlayLeaveStationSound() const;
 
	bool IsPrimaryVehicle() const { return true; }
 
	int GetImage(Direction direction) const;
 
	SpriteID GetImage(Direction direction) const;
 
	int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
 
	int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
 
	Money GetRunningCost() const { return ShipVehInfo(this->engine_type)->running_cost * _price.ship_running; }
 
	bool IsInDepot() const { return this->u.ship.state == 0x80; }
 
	void Tick();
 
	void OnNewDay();
src/ship_cmd.cpp
Show inline comments
 
@@ -55,64 +55,55 @@ static const TrackBits _ship_sometracks[
 

	
 
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
 
{
 
	return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
 
}
 

	
 
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
 
static SpriteID GetShipIcon(EngineID engine)
 
{
 
	int spritenum = ShipVehInfo(engine)->image_index;
 
	uint8 spritenum = ShipVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		int sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		if (sprite != 0) {
 
			DrawSprite(sprite, pal, x, y);
 
			return;
 
		}
 
		spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
 
	}
 
	DrawSprite(6 + _ship_sprites[spritenum], pal, x, y);
 

	
 
	return 6 + _ship_sprites[spritenum];
 
}
 

	
 
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
 
{
 
	DrawSprite(GetShipIcon(engine), pal, x, y);
 
}
 

	
 
/** Get the size of the sprite of a ship sprite heading west (used for lists)
 
 * @param engine The engine to get the sprite from
 
 * @param width The width of the sprite
 
 * @param height The height of the sprite
 
 */
 
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
 
{
 
	SpriteID spritenum = ShipVehInfo(engine)->image_index;
 
	SpriteID custom_sprite = 0;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		custom_sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
 
	}
 
	if (custom_sprite == 0) {
 
		spritenum = 6 + _ship_sprites[spritenum];
 
	} else {
 
		spritenum = custom_sprite;
 
	}
 

	
 
	const Sprite *spr = GetSprite(spritenum);
 
	const Sprite *spr = GetSprite(GetShipIcon(engine));
 

	
 
	width  = spr->width;
 
	height = spr->height;
 
}
 

	
 
int Ship::GetImage(Direction direction) const
 
SpriteID Ship::GetImage(Direction direction) const
 
{
 
	int spritenum = this->spritenum;
 
	uint8 spritenum = this->spritenum;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		int sprite = GetCustomVehicleSprite(this, direction);
 
		SpriteID sprite = GetCustomVehicleSprite(this, direction);
 
		if (sprite != 0) return sprite;
 

	
 
		if (sprite != 0) return sprite;
 
		spritenum = _orig_ship_vehicle_info[this->engine_type - SHIP_ENGINES_INDEX].image_index;
 
	}
 

	
 
	return _ship_sprites[spritenum] + direction;
 
}
 

	
 
static const Depot* FindClosestShipDepot(const Vehicle* v)
 
{
 
	if (_patches.pathfinder_for_ships == VPF_NPF) { /* NPF is used */
src/train.h
Show inline comments
 
@@ -293,13 +293,13 @@ struct Train : public Vehicle {
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; }
 
	void PlayLeaveStationSound() const;
 
	bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
 
	int GetImage(Direction direction) const;
 
	SpriteID GetImage(Direction direction) const;
 
	int GetDisplaySpeed() const { return this->u.rail.last_speed * 10 / 16; }
 
	int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; }
 
	Money GetRunningCost() const;
 
	bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
 
	bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
 
	void Tick();
src/train_cmd.cpp
Show inline comments
 
@@ -464,65 +464,67 @@ void UpdateTrainAcceleration(Vehicle *v)
 
	uint power = v->u.rail.cached_power;
 
	uint weight = v->u.rail.cached_weight;
 
	assert(weight != 0);
 
	v->acceleration = Clamp(power / weight * 4, 1, 255);
 
}
 

	
 
int Train::GetImage(Direction direction) const
 
SpriteID Train::GetImage(Direction direction) const
 
{
 
	int img = this->spritenum;
 
	int base;
 
	uint8 spritenum = this->spritenum;
 
	SpriteID sprite;
 

	
 
	if (HasBit(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
 

	
 
	if (is_custom_sprite(img)) {
 
		base = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
 
		if (base != 0) return base;
 
		img = _orig_rail_vehicle_info[this->engine_type].image_index;
 
	if (is_custom_sprite(spritenum)) {
 
		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = _orig_rail_vehicle_info[this->engine_type].image_index;
 
	}
 

	
 
	base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
 

	
 
	if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
 
	return base;
 
	sprite = _engine_sprite_base[spritenum] + ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]);
 

	
 
	if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
 

	
 
	return sprite;
 
}
 

	
 
static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
 
{
 
	Direction dir = rear_head ? DIR_E : DIR_W;
 
	uint8 spritenum = RailVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, dir);
 
		if (sprite != 0) {
 
			y += _traininfo_vehicle_pitch; // TODO Make this per-GRF
 
			return sprite;
 
		}
 

	
 
		spritenum = _orig_rail_vehicle_info[engine].image_index;
 
	}
 

	
 
	if (rear_head) spritenum++;
 

	
 
	return ((6 + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
 
}
 

	
 
void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal)
 
{
 
	const RailVehicleInfo *rvi = RailVehInfo(engine);
 

	
 
	int img = rvi->image_index;
 
	SpriteID image = 0;
 

	
 
	if (is_custom_sprite(img)) {
 
		image = GetCustomVehicleIcon(engine, DIR_W);
 
		if (image == 0) {
 
			img = _orig_rail_vehicle_info[engine].image_index;
 
		} else {
 
			y += _traininfo_vehicle_pitch;
 
		}
 
	}
 
	if (image == 0) {
 
		image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
 
	if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
 
		int yf = y;
 
		int yr = y;
 

	
 
		SpriteID spritef = GetRailIcon(engine, false, yf);
 
		SpriteID spriter = GetRailIcon(engine, true, yr);
 
		DrawSprite(spritef, pal, x - 14, yf);
 
		DrawSprite(spriter, pal, x + 15, yr);
 
	} else {
 
		SpriteID sprite = GetRailIcon(engine, false, y);
 
		DrawSprite(sprite, pal, x, y);
 
	}
 

	
 
	if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
 
		DrawSprite(image, pal, x - 14, y);
 
		x += 15;
 
		image = 0;
 
		if (is_custom_sprite(img)) {
 
			image = GetCustomVehicleIcon(engine, DIR_E);
 
			if (image == 0) img = _orig_rail_vehicle_info[engine].image_index;
 
		}
 
		if (image == 0) {
 
			image =
 
				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
 
				_engine_sprite_base[img + 1];
 
		}
 
	}
 
	DrawSprite(image, pal, x, y);
 
}
 

	
 
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
 
{
 
	const RailVehicleInfo *rvi = RailVehInfo(engine);
 
	CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
src/vehicle_base.h
Show inline comments
 
@@ -387,13 +387,13 @@ public:
 

	
 
	/**
 
	 * Gets the sprite to show for the given direction
 
	 * @param direction the direction the vehicle is facing
 
	 * @return the sprite for the given vehicle in the given direction
 
	 */
 
	virtual int GetImage(Direction direction) const { return 0; }
 
	virtual SpriteID GetImage(Direction direction) const { return 0; }
 

	
 
	/**
 
	 * Gets the speed in mph that can be sent into SetDParam for string processing.
 
	 * @return the vehicle's speed
 
	 */
 
	virtual int GetDisplaySpeed() const { return 0; }
0 comments (0 inline, 0 general)