Changeset - r24100:c2d839fb3157
[Not reviewed]
master
0 1 0
Niels Martin Hansen - 4 years ago 2020-02-09 19:10:32
nielsm@indvikleren.dk
Codechange: Refactor road bridge catenary drawing
1 file changed with 28 insertions and 21 deletions:
0 comments (0 inline, 0 general)
src/tunnelbridge_cmd.cpp
Show inline comments
 
@@ -1096,48 +1096,71 @@ static void DrawBridgePillars(const PalS
 
	if (z_front_south < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
 

	
 
	/* Draw back pillars, skip top two parts, which are hidden by the bridge */
 
	int z_bridge_back = z_bridge - 2 * (int)TILE_HEIGHT;
 
	if (drawfarpillar && (z_back_north <= z_bridge_back || z_back_south <= z_bridge_back)) {
 
		bottom_z = DrawPillarColumn(z_back, z_bridge_back, psid, x_back, y_back, w, h);
 
		if (z_back_north < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
 
		if (z_back_south < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
 
	}
 
}
 

	
 
/**
 
 * Retrieve the sprites required for catenary on a road/tram bridge.
 
 * @param rti              RoadTypeInfo for the road or tram type to get catenary for
 
 * @param head_tile        Bridge head tile with roadtype information
 
 * @param offset           Sprite offset identifying flat to sloped bridge tiles
 
 * @param head             Are we drawing bridge head?
 
 * @param[out] spr_back    Back catenary sprite to use
 
 * @param[out] spr_front   Front catenary sprite to use
 
 */
 
static void GetBridgeRoadCatenary(const RoadTypeInfo *rti, TileIndex head_tile, int offset, bool head, SpriteID &spr_back, SpriteID &spr_front)
 
{
 
	static const SpriteID back_offsets[6]  = { 95,  96,  99, 102, 100, 101 };
 
	static const SpriteID front_offsets[6] = { 97,  98, 103, 106, 104, 105 };
 

	
 
	/* Simplified from DrawRoadTypeCatenary() to remove all the special cases required for regular ground road */
 
	spr_back = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_BACK, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
	spr_front = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_FRONT, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
	if (spr_back == 0 || spr_front == 0) {
 
		spr_back = SPR_TRAMWAY_BASE + back_offsets[offset];
 
		spr_front = SPR_TRAMWAY_BASE + front_offsets[offset];
 
	} else {
 
		spr_back += 23 + offset;
 
		spr_front += 23 + offset;
 
	}
 
}
 

	
 
/**
 
 * Draws the road and trambits over an already drawn (lower end) of a bridge.
 
 * @param head_tile    bridge head tile with roadtype information
 
 * @param x            the x of the bridge
 
 * @param y            the y of the bridge
 
 * @param z            the z of the bridge
 
 * @param offset       sprite offset identifying flat to sloped bridge tiles
 
 * @param head         are we drawing bridge head?
 
 */
 
static void DrawBridgeRoadBits(TileIndex head_tile, int x, int y, int z, int offset, bool head)
 
{
 
	RoadType road_rt = GetRoadTypeRoad(head_tile);
 
	RoadType tram_rt = GetRoadTypeTram(head_tile);
 
	const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
 
	const RoadTypeInfo *tram_rti = tram_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(tram_rt);
 

	
 
	SpriteID seq_back[4] = { 0 };
 
	bool trans_back[4] = { false };
 
	SpriteID seq_front[4] = { 0 };
 
	bool trans_front[4] = { false };
 

	
 
	static const SpriteID overlay_offsets[6] = {   0,   1,  11,  12,  13,  14 };
 
	static const SpriteID back_offsets[6]    = {  95,  96,  99, 102, 100, 101 };
 
	static const SpriteID front_offsets[6]   = {  97,  98, 103, 106, 104, 105 };
 

	
 
	if (head || !IsInvisibilitySet(TO_BRIDGES)) {
 
		/* Road underlay takes precedence over tram */
 
		trans_back[0] = !head && IsTransparencySet(TO_BRIDGES);
 
		if (road_rti != nullptr) {
 
			if (road_rti->UsesOverlay()) {
 
				seq_back[0] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
 
			}
 
		} else if (tram_rti != nullptr) {
 
			if (tram_rti->UsesOverlay()) {
 
				seq_back[0] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
 
			} else {
 
				seq_back[0] = SPR_TRAMWAY_BRIDGE + offset;
 
@@ -1159,43 +1182,27 @@ static void DrawBridgeRoadBits(TileIndex
 
			if (tram_rti->UsesOverlay()) {
 
				seq_back[2] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_OVERLAY, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
				if (seq_back[2] != 0) seq_back[2] += overlay_offsets[offset];
 
			} else if (road_rti != nullptr) {
 
				seq_back[2] = SPR_TRAMWAY_OVERLAY + overlay_offsets[offset];
 
			}
 
		}
 

	
 
		/* Road catenary takes precedence over tram */
 
		trans_back[3] = IsTransparencySet(TO_CATENARY);
 
		trans_front[0] = IsTransparencySet(TO_CATENARY);
 
		if (road_rti != nullptr && HasRoadCatenaryDrawn(road_rt)) {
 
			seq_back[3] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_CATENARY_BACK, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
			seq_front[0] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_CATENARY_FRONT, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
			if (seq_back[3] == 0 || seq_front[0] == 0) {
 
				seq_back[3] = SPR_TRAMWAY_BASE + back_offsets[offset];
 
				seq_front[0] = SPR_TRAMWAY_BASE + front_offsets[offset];
 
			} else {
 
				seq_back[3] += 23 + offset;
 
				seq_front[0] += 23 + offset;
 
			}
 
			GetBridgeRoadCatenary(road_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
 
		} else if (tram_rti != nullptr && HasRoadCatenaryDrawn(tram_rt)) {
 
			seq_back[3] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_CATENARY_BACK, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
			seq_front[0] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_CATENARY_FRONT, head ? TCX_NORMAL : TCX_ON_BRIDGE);
 
			if (seq_back[3] == 0 || seq_front[0] == 0) {
 
				seq_back[3] = SPR_TRAMWAY_BASE + back_offsets[offset];
 
				seq_front[0] = SPR_TRAMWAY_BASE + front_offsets[offset];
 
			} else {
 
				seq_back[3] += 23 + offset;
 
				seq_front[0] += 23 + offset;
 
			}
 
			GetBridgeRoadCatenary(tram_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
 
		}
 
	}
 

	
 
	static const uint size_x[6] = {  1, 16, 16,  1, 16,  1 };
 
	static const uint size_y[6] = { 16,  1,  1, 16,  1, 16 };
 
	static const uint front_bb_offset_x[6] = { 15,  0,  0, 15,  0, 15 };
 
	static const uint front_bb_offset_y[6] = {  0, 15, 15,  0, 15,  0 };
 

	
 
	/* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called
 
	 * The bounding boxes here are the same as for bridge front/roof */
 
	for (uint i = 0; i < lengthof(seq_back); ++i) {
 
		if (seq_back[i] != 0) {
0 comments (0 inline, 0 general)