(svn r14606) -Codechange: Unify usage of PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR in spritelayout drawing.
authorfrosch
Sat, 22 Nov 2008 16:04:11 +0000
changeset 10355 8a930759b457
parent 10354 bb146a933ca8
child 10356 2ec5dcfd8554
(svn r14606) -Codechange: Unify usage of PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR in spritelayout drawing.
-Fix [FS#2419]: The modifiers were not applied in all cases.
src/newgrf_commons.h
src/newgrf_house.cpp
src/newgrf_industrytiles.cpp
src/newgrf_station.cpp
src/rail_cmd.cpp
src/station_cmd.cpp
--- a/src/newgrf_commons.h	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/newgrf_commons.h	Sat Nov 22 16:04:11 2008 +0000
@@ -7,6 +7,10 @@
 #ifndef NEWGRF_COMMONS_H
 #define NEWGRF_COMMONS_H
 
+#include "core/bitmath_func.hpp"
+
+#include "table/sprites.h"
+
 /**
  * Maps an entity id stored on the map to a GRF file.
  * Entities are objects used ingame (houses, industries, industry tiles) for
@@ -96,4 +100,43 @@
 TileIndex GetNearbyTile(byte parameter, TileIndex tile);
 uint32 GetNearbyTileInformation(TileIndex tile);
 
+/**
+ * Applies PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR to a palette entry of a sprite layout entry
+ * @Note for ground sprites use #GroundSpritePaletteTransform
+ * @Note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set
+ *       when to use the default palette.
+ *
+ * @param image The sprite to draw
+ * @param pal The palette from the sprite layout
+ * @param default_pal The default recolour sprite to use (typically company color resp. random industry/house color)
+ * @return The palette to use
+ */
+static inline SpriteID SpriteLayoutPaletteTransform(SpriteID image, SpriteID pal, SpriteID default_pal)
+{
+	if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
+		return (pal != 0 ? pal : default_pal);
+	} else {
+		return PAL_NONE;
+	}
+}
+
+/**
+ * Applies PALETTE_MODIFIER_COLOR to a palette entry of a ground sprite
+ * @Note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set
+ *       when to use the default palette.
+ *
+ * @param image The sprite to draw
+ * @param pal The palette from the sprite layout
+ * @param default_pal The default recolour sprite to use (typically company color resp. random industry/house color)
+ * @return The palette to use
+ */
+static inline SpriteID GroundSpritePaletteTransform(SpriteID image, SpriteID pal, SpriteID default_pal)
+{
+	if (HasBit(image, PALETTE_MODIFIER_COLOR)) {
+		return (pal != 0 ? pal : default_pal);
+	} else {
+		return PAL_NONE;
+	}
+}
+
 #endif /* NEWGRF_COMMONS_H */
--- a/src/newgrf_house.cpp	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/newgrf_house.cpp	Sat Nov 22 16:04:11 2008 +0000
@@ -426,12 +426,24 @@
 	const DrawTileSprites *dts = group->g.layout.dts;
 	const DrawTileSeqStruct *dtss;
 
+	const HouseSpec *hs = GetHouseSpecs(house_id);
+	SpriteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOR_START;
+	if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
+		uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
+		if (callback != CALLBACK_FAILED) {
+			/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
+			palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
+		}
+	}
+
 	SpriteID image = dts->ground.sprite;
 	SpriteID pal   = dts->ground.pal;
 
 	if (IS_CUSTOM_SPRITE(image)) image += stage;
 
-	if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal);
+	if (GB(image, 0, SPRITE_WIDTH) != 0) {
+		DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
+	}
 
 	foreach_draw_tile_seq(dtss, dts->seq) {
 		if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
@@ -444,21 +456,7 @@
 
 		if (IS_CUSTOM_SPRITE(image)) image += stage;
 
-		if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
-			if (pal == 0) {
-				const HouseSpec *hs = GetHouseSpecs(house_id);
-				pal = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOR_START;
-				if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
-					uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
-					if (callback != CALLBACK_FAILED) {
-						/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
-						pal = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
-					}
-				}
-			}
-		} else {
-			pal = PAL_NONE;
-		}
+		pal = SpriteLayoutPaletteTransform(image, pal, palette);
 
 		if ((byte)dtss->delta_z != 0x80) {
 			AddSortableSpriteToDraw(
--- a/src/newgrf_industrytiles.cpp	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/newgrf_industrytiles.cpp	Sat Nov 22 16:04:11 2008 +0000
@@ -183,7 +183,7 @@
 		if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
 			DrawWaterClassGround(ti);
 		} else {
-			DrawGroundSprite(image, pal);
+			DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOR(rnd_color)));
 		}
 	}
 
@@ -198,13 +198,7 @@
 
 		if (IS_CUSTOM_SPRITE(image)) image += stage;
 
-		if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
-			if (pal == 0) {
-				pal = GENERAL_SPRITE_COLOR(rnd_color);
-			}
-		} else {
-			pal = PAL_NONE;
-		}
+		pal = SpriteLayoutPaletteTransform(image, pal, GENERAL_SPRITE_COLOR(rnd_color));
 
 		if ((byte)dtss->delta_z != 0x80) {
 			AddSortableSpriteToDraw(
--- a/src/newgrf_station.cpp	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/newgrf_station.cpp	Sat Nov 22 16:04:11 2008 +0000
@@ -803,6 +803,7 @@
 	}
 
 	image = sprites->ground.sprite;
+	SpriteID pal = sprites->ground.pal;
 	if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
 		image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE);
 		image += rti->custom_ground_offset;
@@ -810,7 +811,7 @@
 		image += rti->total_offset;
 	}
 
-	DrawSprite(image, PAL_NONE, x, y);
+	DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
 
 	Point child_offset = {0, 0};
 
@@ -822,16 +823,7 @@
 			image += relocation;
 		}
 
-		SpriteID pal;
-		if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
-			if (seq->image.pal > 0) {
-				pal = seq->image.pal;
-			} else {
-				pal = palette;
-			}
-		} else {
-			pal = PAL_NONE;
-		}
+		pal = SpriteLayoutPaletteTransform(image, seq->image.pal, palette);
 
 		if ((byte)seq->delta_z != 0x80) {
 			Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
--- a/src/rail_cmd.cpp	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/rail_cmd.cpp	Sat Nov 22 16:04:11 2008 +0000
@@ -30,6 +30,7 @@
 #include "newgrf_engine.h"
 #include "newgrf_callbacks.h"
 #include "newgrf_station.h"
+#include "newgrf_commons.h"
 #include "train.h"
 #include "variables.h"
 #include "autoslope.h"
@@ -1920,6 +1921,7 @@
 		const DrawTileSprites* dts;
 		const DrawTileSeqStruct* dtss;
 		uint32 relocation;
+		SpriteID pal = PAL_NONE;
 
 		if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
 
@@ -1976,6 +1978,8 @@
 					} else {
 						image += rti->total_offset;
 					}
+
+					pal = dts->ground.pal;
 				} else {
 					goto default_waypoint;
 				}
@@ -1989,7 +1993,7 @@
 			}
 		}
 
-		DrawGroundSprite(image, PAL_NONE);
+		DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, _drawtile_track_palette));
 
 		/* PBS debugging, draw reserved tracks darker */
 		if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && GetDepotWaypointReservation(ti->tile) &&
@@ -2001,7 +2005,7 @@
 
 		foreach_draw_tile_seq(dtss, dts->seq) {
 			SpriteID image = dtss->image.sprite;
-			SpriteID pal;
+			SpriteID pal   = dtss->image.pal;
 
 			/* Stop drawing sprite sequence once we meet a sprite that doesn't have to be opaque */
 			if (IsInvisibilitySet(TO_BUILDINGS) && !HasBit(image, SPRITE_MODIFIER_OPAQUE)) return;
@@ -2015,15 +2019,7 @@
 				image += relocation;
 			}
 
-			if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
-				if (dtss->image.pal != 0) {
-					pal = dtss->image.pal;
-				} else {
-					pal = _drawtile_track_palette;
-				}
-			} else {
-				pal = PAL_NONE;
-			}
+			pal = SpriteLayoutPaletteTransform(image, pal, _drawtile_track_palette);
 
 			if ((byte)dtss->delta_z != 0x80) {
 				AddSortableSpriteToDraw(
--- a/src/station_cmd.cpp	Sat Nov 22 15:57:31 2008 +0000
+++ b/src/station_cmd.cpp	Sat Nov 22 16:04:11 2008 +0000
@@ -24,6 +24,7 @@
 #include "industry_map.h"
 #include "newgrf_callbacks.h"
 #include "newgrf_station.h"
+#include "newgrf_commons.h"
 #include "yapf/yapf.h"
 #include "road_type.h"
 #include "road_internal.h" /* For drawing catenary/checking road removal */
@@ -2325,13 +2326,14 @@
 		}
 	} else {
 		SpriteID image = t->ground.sprite;
+		SpriteID pal   = t->ground.pal;
 		if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
 			image += GetCustomStationGroundRelocation(statspec, st, ti->tile);
 			image += custom_ground_offset;
 		} else {
 			image += total_offset;
 		}
-		DrawGroundSprite(image, HasBit(image, PALETTE_MODIFIER_COLOR) ? palette : PAL_NONE);
+		DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
 
 		/* PBS debugging, draw reserved tracks darker */
 		if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && IsRailwayStation(ti->tile) && GetRailwayStationReservation(ti->tile)) {
@@ -2361,16 +2363,7 @@
 			image += relocation;
 		}
 
-		SpriteID pal;
-		if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) {
-			if (dtss->image.pal > 0) {
-				pal = dtss->image.pal;
-			} else {
-				pal = palette;
-			}
-		} else {
-			pal = PAL_NONE;
-		}
+		SpriteID pal = SpriteLayoutPaletteTransform(image, dtss->image.pal, palette);
 
 		if ((byte)dtss->delta_z != 0x80) {
 			AddSortableSpriteToDraw(