changeset 7134:e1c4f52f82cd draft

(svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
author rubidium <rubidium@openttd.org>
date Sun, 01 Jul 2007 19:11:47 +0000
parents 148c6c10ffa7
children 45123abbc930
files src/aircraft.h src/aircraft_cmd.cpp src/aircraft_gui.cpp src/depot_gui.cpp src/group_gui.cpp src/roadveh.h src/roadveh_cmd.cpp src/roadveh_gui.cpp src/ship.h src/ship_cmd.cpp src/ship_gui.cpp src/train.h src/train_cmd.cpp src/train_gui.cpp src/vehicle.cpp src/vehicle.h
diffstat 16 files changed, 61 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft.h
+++ b/src/aircraft.h
@@ -136,6 +136,7 @@
 	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;
 };
 
 #endif /* AIRCRAFT_H */
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -153,15 +153,15 @@
 }
 #endif
 
-int GetAircraftImage(const Vehicle* v, Direction direction)
+int Aircraft::GetImage(Direction direction) const
 {
-	int spritenum = v->spritenum;
+	int spritenum = this->spritenum;
 
 	if (is_custom_sprite(spritenum)) {
-		int sprite = GetCustomVehicleSprite(v, direction);
+		int sprite = GetCustomVehicleSprite(this, direction);
 
 		if (sprite != 0) return sprite;
-		spritenum = orig_aircraft_vehicle_info[v->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
+		spritenum = orig_aircraft_vehicle_info[this->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
 	}
 	return direction + _aircraft_sprite[spritenum];
 }
@@ -827,7 +827,7 @@
 	v->y_pos = y;
 	v->z_pos = z;
 
-	v->cur_image = GetAircraftImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	if (v->subtype == AIR_HELICOPTER) v->next->next->cur_image = GetRotorImage(v);
 
 	BeginVehicleMove(v);
@@ -1409,7 +1409,7 @@
 
 void Aircraft::MarkDirty()
 {
-		this->cur_image = GetAircraftImage(this, this->direction);
+		this->cur_image = this->GetImage(this->direction);
 		if (this->subtype == AIR_HELICOPTER) this->next->next->cur_image = GetRotorImage(this);
 		MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
 }
--- a/src/aircraft_gui.cpp
+++ b/src/aircraft_gui.cpp
@@ -25,7 +25,7 @@
 void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
 	SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-	DrawSprite(GetAircraftImage(v, DIR_W), pal, x + 25, y + 10);
+	DrawSprite(v->GetImage(DIR_W), pal, x + 25, y + 10);
 	if (v->subtype == AIR_HELICOPTER) {
 		SpriteID rotor_sprite = GetCustomRotorSprite(v, true);
 		if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -187,7 +187,7 @@
 		case VEH_ROAD:     DrawRoadVehImage( v, x + 24, sprite_y, 1, WP(w, depot_d).sel); break;
 		case VEH_SHIP:     DrawShipImage(    v, x + 19, sprite_y - 1, WP(w, depot_d).sel); break;
 		case VEH_AIRCRAFT: {
-			const Sprite *spr = GetSprite(GetAircraftImage(v, DIR_W));
+			const Sprite *spr = GetSprite(v->GetImage(DIR_W));
 			DrawAircraftImage(v, x + 12,
 							  y + max(spr->height + spr->y_offs - 14, 0), // tall sprites needs an y offset
 							  WP(w, depot_d).sel);
@@ -438,15 +438,7 @@
 				WP(w,depot_d).sel = INVALID_VEHICLE;
 				TrainDepotMoveVehicle(v, sel, gdvp.head);
 			} else if (v != NULL) {
-				int image;
-
-				switch (WP(w, depot_d).type) {
-					case VEH_TRAIN:    image = GetTrainImage(v, DIR_W);    break;
-					case VEH_ROAD:     image = GetRoadVehImage(v, DIR_W);  break;
-					case VEH_SHIP:     image = GetShipImage(v, DIR_W);     break;
-					case VEH_AIRCRAFT: image = GetAircraftImage(v, DIR_W); break;
-					default: NOT_REACHED(); image = 0;
-				}
+				int image = v->GetImage(DIR_W);
 
 				WP(w, depot_d).sel = v->index;
 				SetWindowDirty(w);
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -531,17 +531,7 @@
 					gv->vehicle_sel = v->index;
 
 					if (IsValidVehicle(v)) {
-						CursorID image;
-
-						switch (gv->vehicle_type) {
-							case VEH_TRAIN:    image = GetTrainImage(v, DIR_W);    break;
-							case VEH_ROAD:     image = GetRoadVehImage(v, DIR_W);  break;
-							case VEH_SHIP:     image = GetShipImage(v, DIR_W);     break;
-							case VEH_AIRCRAFT: image = GetAircraftImage(v, DIR_W); break;
-							default: NOT_REACHED(); break;
-						}
-
-						SetObjectToPlaceWnd(image, GetVehiclePalette(v), 4, w);
+						SetObjectToPlaceWnd(v->GetImage(DIR_W), GetVehiclePalette(v), 4, w);
 					}
 
 					SetWindowDirty(w);
--- a/src/roadveh.h
+++ b/src/roadveh.h
@@ -81,6 +81,7 @@
 	WindowClass GetVehicleListWindowClass() const { return WC_ROADVEH_LIST; }
 	bool IsPrimaryVehicle() const { return IsRoadVehFront(this); }
 	bool HasFront() const { return true; }
+	int GetImage(Direction direction) const;
 };
 
 byte GetRoadVehLength(const Vehicle *v);
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -85,19 +85,19 @@
 	TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
 };
 
-int GetRoadVehImage(const Vehicle* v, Direction direction)
+int RoadVehicle::GetImage(Direction direction) const
 {
-	int img = v->spritenum;
+	int img = this->spritenum;
 	int image;
 
 	if (is_custom_sprite(img)) {
-		image = GetCustomVehicleSprite(v, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
+		image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
 		if (image != 0) return image;
-		img = orig_road_vehicle_info[v->engine_type - ROAD_ENGINES_INDEX].image_index;
+		img = orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
 	}
 
 	image = direction + _roadveh_images[img];
-	if (v->cargo.Count() >= v->cargo_cap / 2U) image += _roadveh_full_adder[img];
+	if (this->cargo.Count() >= this->cargo_cap / 2U) image += _roadveh_full_adder[img];
 	return image;
 }
 
@@ -555,7 +555,7 @@
 
 void RoadVehicle::MarkDirty()
 {
-	this->cur_image = GetRoadVehImage(this, this->direction);
+	this->cur_image = this->GetImage(this->direction);
 	MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
 }
 
@@ -641,7 +641,7 @@
 		v->direction = ChangeDir(v->direction, delta[r & 3]);
 		BeginVehicleMove(v);
 		v->UpdateDeltaXY(v->direction);
-		v->cur_image = GetRoadVehImage(v, v->direction);
+		v->cur_image = v->GetImage(v->direction);
 		SetRoadVehPosition(v, v->x_pos, v->y_pos);
 	} while ((v = v->next) != NULL);
 }
@@ -1366,7 +1366,7 @@
 	v->u.road.state = tdir;
 	v->u.road.frame = RVC_DEPOT_START_FRAME;
 
-	v->cur_image = GetRoadVehImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	v->UpdateDeltaXY(v->direction);
 	SetRoadVehPosition(v,x,y);
 
@@ -1459,7 +1459,7 @@
 
 		if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && HASBIT(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
 			/* Vehicle has just entered a bridge or tunnel */
-			v->cur_image = GetRoadVehImage(v, v->direction);
+			v->cur_image = v->GetImage(v->direction);
 			v->UpdateDeltaXY(v->direction);
 			SetRoadVehPosition(v,gp.x,gp.y);
 			return true;
@@ -1574,7 +1574,7 @@
 			v->cur_speed -= v->cur_speed >> 2;
 		}
 
-		v->cur_image = GetRoadVehImage(v, newdir);
+		v->cur_image = v->GetImage(newdir);
 		v->UpdateDeltaXY(v->direction);
 		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 		return true;
@@ -1614,7 +1614,7 @@
 			v->cur_speed -= v->cur_speed >> 2;
 		}
 
-		v->cur_image = GetRoadVehImage(v, newdir);
+		v->cur_image = v->GetImage(newdir);
 		v->UpdateDeltaXY(v->direction);
 		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 		return true;
@@ -1656,7 +1656,7 @@
 		v->cur_speed -= (v->cur_speed >> 2);
 		if (old_dir != v->u.road.state) {
 			/* The vehicle is in a road stop */
-			v->cur_image = GetRoadVehImage(v, new_dir);
+			v->cur_image = v->GetImage(new_dir);
 			v->UpdateDeltaXY(v->direction);
 			SetRoadVehPosition(v, v->x_pos, v->y_pos);
 			/* Note, return here means that the frame counter is not incremented
@@ -1776,7 +1776,7 @@
 	 * in a depot or entered a tunnel/bridge */
 	if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) v->u.road.frame++;
 
-	v->cur_image = GetRoadVehImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	v->UpdateDeltaXY(v->direction);
 	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 	return true;
--- a/src/roadveh_gui.cpp
+++ b/src/roadveh_gui.cpp
@@ -38,7 +38,7 @@
 
 		if (dx + length > 0 && dx <= max_length) {
 			SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-			DrawSprite(GetRoadVehImage(v, DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6);
+			DrawSprite(v->GetImage(DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6);
 
 			if (v->index == selection) {
 				DrawFrameRect(x - 1, y - 1, x + 28, y + 12, 15, FR_BORDERONLY);
--- a/src/ship.h
+++ b/src/ship.h
@@ -46,6 +46,7 @@
 	WindowClass GetVehicleListWindowClass() const { return WC_SHIPS_LIST; }
 	void PlayLeaveStationSound() const;
 	bool IsPrimaryVehicle() const { return true; }
+	int GetImage(Direction direction) const;
 };
 
 #endif /* SHIP_H */
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -90,15 +90,15 @@
 	height = spr->height;
 }
 
-int GetShipImage(const Vehicle* v, Direction direction)
+int Ship::GetImage(Direction direction) const
 {
-	int spritenum = v->spritenum;
+	int spritenum = this->spritenum;
 
 	if (is_custom_sprite(spritenum)) {
-		int sprite = GetCustomVehicleSprite(v, direction);
+		int sprite = GetCustomVehicleSprite(this, direction);
 
 		if (sprite != 0) return sprite;
-		spritenum = orig_ship_vehicle_info[v->engine_type - SHIP_ENGINES_INDEX].image_index;
+		spritenum = orig_ship_vehicle_info[this->engine_type - SHIP_ENGINES_INDEX].image_index;
 	}
 	return _ship_sprites[spritenum] + direction;
 }
@@ -233,7 +233,7 @@
 
 void Ship::MarkDirty()
 {
-	this->cur_image = GetShipImage(this, this->direction);
+	this->cur_image = this->GetImage(this->direction);
 	MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
 }
 
@@ -337,7 +337,7 @@
 void RecalcShipStuff(Vehicle *v)
 {
 	v->UpdateDeltaXY(v->direction);
-	v->cur_image = GetShipImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	v->MarkDirty();
 	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 }
@@ -770,7 +770,7 @@
 
 getout:
 	v->UpdateDeltaXY(dir);
-	v->cur_image = GetShipImage(v, dir);
+	v->cur_image = v->GetImage(dir);
 	VehiclePositionChanged(v);
 	EndVehicleMove(v);
 	return;
--- a/src/ship_gui.cpp
+++ b/src/ship_gui.cpp
@@ -19,7 +19,7 @@
 
 void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection)
 {
-	DrawSprite(GetShipImage(v, DIR_W), GetVehiclePalette(v), x + 32, y + 10);
+	DrawSprite(v->GetImage(DIR_W), GetVehiclePalette(v), x + 32, y + 10);
 
 	if (v->index == selection) {
 		DrawFrameRect(x - 5, y - 1, x + 67, y + 21, 15, FR_BORDERONLY);
--- a/src/train.h
+++ b/src/train.h
@@ -272,6 +272,7 @@
 	void PlayLeaveStationSound() const;
 	bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
 	bool HasFront() const { return true; }
+	int GetImage(Direction direction) const;
 };
 
 #endif /* TRAIN_H */
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -448,22 +448,22 @@
 	v->acceleration = clamp(power / weight * 4, 1, 255);
 }
 
-int GetTrainImage(const Vehicle* v, Direction direction)
+int Train::GetImage(Direction direction) const
 {
-	int img = v->spritenum;
+	int img = this->spritenum;
 	int base;
 
-	if (HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
+	if (HASBIT(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
 
 	if (is_custom_sprite(img)) {
-		base = GetCustomVehicleSprite(v, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
+		base = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
 		if (base != 0) return base;
-		img = orig_rail_vehicle_info[v->engine_type].image_index;
+		img = orig_rail_vehicle_info[this->engine_type].image_index;
 	}
 
 	base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
 
-	if (v->cargo.Count() >= v->cargo_cap / 2U) base += _wagon_full_adder[img];
+	if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
 	return base;
 }
 
@@ -1424,7 +1424,7 @@
 static void UpdateVarsAfterSwap(Vehicle *v)
 {
 	v->UpdateDeltaXY(v->direction);
-	v->cur_image = GetTrainImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	BeginVehicleMove(v);
 	VehiclePositionChanged(v);
 	EndVehicleMove(v);
@@ -2081,7 +2081,7 @@
 	v->cur_speed = 0;
 
 	v->UpdateDeltaXY(v->direction);
-	v->cur_image = GetTrainImage(v, v->direction);
+	v->cur_image = v->GetImage(v->direction);
 	VehiclePositionChanged(v);
 	UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction));
 	UpdateTrainAcceleration(v);
@@ -2467,7 +2467,7 @@
 {
 	Vehicle *v = this;
 	do {
-		v->cur_image = GetTrainImage(v, v->direction);
+		v->cur_image = v->GetImage(v->direction);
 		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
 	} while ((v = v->next) != NULL);
 
@@ -2969,7 +2969,7 @@
 		/* update image of train, as well as delta XY */
 		Direction newdir = GetNewVehicleDirection(v, gp.x, gp.y);
 		v->UpdateDeltaXY(newdir);
-		if (update_image) v->cur_image = GetTrainImage(v, newdir);
+		if (update_image) v->cur_image = v->GetImage(newdir);
 
 		v->x_pos = gp.x;
 		v->y_pos = gp.y;
@@ -3076,7 +3076,7 @@
 			v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
 			BeginVehicleMove(v);
 			v->UpdateDeltaXY(v->direction);
-			v->cur_image = GetTrainImage(v, v->direction);
+			v->cur_image = v->GetImage(v->direction);
 			/* Refrain from updating the z position of the vehicle when on
 			   a bridge, because AfterSetTrainPos will put the vehicle under
 			   the bridge in that case */
--- a/src/train_gui.cpp
+++ b/src/train_gui.cpp
@@ -93,7 +93,7 @@
 		if (dx + width > 0) {
 			if (dx <= count) {
 				SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-				DrawSprite(GetTrainImage(v, DIR_W), pal, 16 + WagonLengthToPixels(dx), 7 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
+				DrawSprite(v->GetImage(DIR_W), pal, 16 + WagonLengthToPixels(dx), 7 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
 				if (v->index == selection) {
 					/* Set the highlight position */
 					highlight_l = WagonLengthToPixels(dx) + 1;
@@ -459,7 +459,7 @@
 				u = v;
 				do {
 					SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
-					DrawSprite(GetTrainImage(u, DIR_W), pal, x + WagonLengthToPixels(4 + dx), y + 6 + (is_custom_sprite(RailVehInfo(u->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
+					DrawSprite(u->GetImage(DIR_W), pal, x + WagonLengthToPixels(4 + dx), y + 6 + (is_custom_sprite(RailVehInfo(u->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
 					dx += u->u.rail.cached_veh_length;
 					u = u->next;
 				} while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0);
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -249,16 +249,17 @@
 	FOR_ALL_VEHICLES(v) {
 		switch (v->type) {
 			case VEH_ROAD:
-				v->cur_image = GetRoadVehImage(v, v->direction);
 				v->u.road.roadtype = HASBIT(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
 				v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
+				/* FALL THROUGH */
+			case VEH_TRAIN:
+			case VEH_SHIP:
+				v->cur_image = v->GetImage(v->direction);
 				break;
 
-			case VEH_TRAIN: v->cur_image = GetTrainImage(v, v->direction); break;
-			case VEH_SHIP: v->cur_image = GetShipImage(v, v->direction); break;
 			case VEH_AIRCRAFT:
 				if (IsNormalAircraft(v)) {
-					v->cur_image = GetAircraftImage(v, v->direction);
+					v->cur_image = v->GetImage(v->direction);
 
 					/* The plane's shadow will have the same image as the plane */
 					Vehicle *shadow = v->next;
--- a/src/vehicle.h
+++ b/src/vehicle.h
@@ -420,6 +420,13 @@
 	 * basically, if GetFirstVehicleInChain() can be called for it.
 	 */
 	virtual bool HasFront() const { return false; }
+
+	/**
+	 * 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; }
 };
 
 /**
@@ -520,11 +527,6 @@
 
 void ViewportAddVehicles(DrawPixelInfo *dpi);
 
-/* train_cmd.h */
-int GetTrainImage(const Vehicle* v, Direction direction);
-int GetAircraftImage(const Vehicle* v, Direction direction);
-int GetRoadVehImage(const Vehicle* v, Direction direction);
-int GetShipImage(const Vehicle* v, Direction direction);
 SpriteID GetRotorImage(const Vehicle *v);
 
 Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type);