Changeset - r7802:503a69fff2c1
[Not reviewed]
master
0 4 0
glx - 17 years ago 2007-10-28 15:40:18
glx@openttd.org
(svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
4 files changed with 26 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/misc_cmd.cpp
Show inline comments
 
@@ -110,12 +110,13 @@ CommandCost CmdSetPlayerColor(TileIndex 
 
				}
 
				break;
 

	
 
			default:
 
				break;
 
		}
 
		ResetVehicleColorMap();
 
		MarkWholeScreenDirty();
 
	}
 
	return CommandCost();
 
}
 

	
 
/** Increase the loan of your company.
src/train_cmd.cpp
Show inline comments
 
@@ -38,12 +38,13 @@
 
#include "newgrf_text.h"
 
#include "direction.h"
 
#include "yapf/yapf.h"
 
#include "date.h"
 
#include "cargotype.h"
 
#include "group.h"
 
#include "table/sprites.h"
 

	
 
static bool TrainCheckIfLineEnds(Vehicle *v);
 
static void TrainController(Vehicle *v, bool update_image);
 

	
 
static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
 
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
 
@@ -173,12 +174,15 @@ void TrainConsistChanged(Vehicle* v)
 

	
 
		if (IsTrainEngine(u)) first_engine = u->engine_type;
 

	
 
		/* Cache wagon override sprite group. NULL is returned if there is none */
 
		u->u.rail.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->u.rail.first_engine);
 

	
 
		/* Reset color map */
 
		u->colormap = PAL_NONE;
 

	
 
		if (rvi_u->visual_effect != 0) {
 
			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
 
		} else {
 
			if (IsTrainWagon(u) || IsArticulatedPart(u)) {
 
				/* Wagons and articulated parts have no effect by default */
 
				u->u.rail.cached_vis_effect = 0x40;
src/vehicle.cpp
Show inline comments
 
@@ -271,12 +271,13 @@ Vehicle::Vehicle()
 
{
 
	this->type               = VEH_INVALID;
 
	this->left_coord         = INVALID_COORD;
 
	this->group_id           = DEFAULT_GROUP;
 
	this->fill_percent_te_id = INVALID_TE_ID;
 
	this->first              = this;
 
	this->colormap           = PAL_NONE;
 
}
 

	
 
/**
 
 * Get a value for a vehicle's random_bits.
 
 * @return A random value from 0 to 255.
 
 */
 
@@ -454,12 +455,18 @@ void ResetVehiclePosHash()
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; }
 
	memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
 
	memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash));
 
}
 

	
 
void ResetVehicleColorMap()
 
{
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) { v->colormap = PAL_NONE; }
 
}
 

	
 
void InitializeVehicles()
 
{
 
	_Vehicle_pool.CleanPool();
 
	_Vehicle_pool.AddBlockToPool();
 

	
 
	ResetVehiclePosHash();
 
@@ -2604,36 +2611,45 @@ const Livery *GetEngineLivery(EngineID e
 
	return &p->livery[scheme];
 
}
 

	
 

	
 
static SpriteID GetEngineColourMap(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v)
 
{
 
	SpriteID map = PAL_NONE;
 
	SpriteID map = (v != NULL) ? v->colormap : PAL_NONE;
 

	
 
	/* Return cached value if any */
 
	if (map != PAL_NONE) return map;
 

	
 
	/* Check if we should use the colour map callback */
 
	if (HASBIT(EngInfo(engine_type)->callbackmask, CBM_VEHICLE_COLOUR_REMAP)) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_COLOUR_MAPPING, 0, 0, engine_type, v);
 
		/* A return value of 0xC000 is stated to "use the default two-color
 
		 * maps" which happens to be the failure action too... */
 
		if (callback != CALLBACK_FAILED && callback != 0xC000) {
 
			map = GB(callback, 0, 14);
 
			/* If bit 14 is set, then the company colours are applied to the
 
			 * map else it's returned as-is. */
 
			if (!HASBIT(callback, 14)) return map;
 
			if (!HASBIT(callback, 14)) {
 
				/* Update cache */
 
				if (v != NULL) ((Vehicle*)v)->colormap = map;
 
				return map;
 
			}
 
		}
 
	}
 

	
 
	bool twocc = HASBIT(EngInfo(engine_type)->misc_flags, EF_USES_2CC);
 

	
 
	if (map == PAL_NONE) map = twocc ? (SpriteID)SPR_2CCMAP_BASE : (SpriteID)PALETTE_RECOLOR_START;
 

	
 
	const Livery *livery = GetEngineLivery(engine_type, player, parent_engine_type, v);
 

	
 
	map += livery->colour1;
 
	if (twocc) map += livery->colour2 * 16;
 

	
 
	/* Update cache */
 
	if (v != NULL) ((Vehicle*)v)->colormap = map;
 
	return map;
 
}
 

	
 
SpriteID GetEnginePalette(EngineID engine_type, PlayerID player)
 
{
 
	return GetEngineColourMap(engine_type, player, INVALID_ENGINE, NULL);
src/vehicle.h
Show inline comments
 
@@ -337,12 +337,14 @@ public:
 
	GroupID group_id;              ///< Index of group Pool array
 

	
 
	/* Used for timetabling. */
 
	uint32 current_order_time;     ///< How many ticks have passed since this order started.
 
	int32 lateness_counter;        ///< How many ticks late (or early if negative) this vehicle is.
 

	
 
	SpriteID colormap; // NOSAVE: cached color mapping
 

	
 
	union {
 
		VehicleRail rail;
 
		VehicleAir air;
 
		VehicleRoad road;
 
		VehicleSpecial special;
 
		VehicleDisaster disaster;
 
@@ -585,12 +587,13 @@ void CallVehicleTicks();
 
Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z);
 
uint8 CalcPercentVehicleFilled(Vehicle *v, StringID *color);
 

	
 
void InitializeTrains();
 
byte VehicleRandomBits();
 
void ResetVehiclePosHash();
 
void ResetVehicleColorMap();
 

	
 
bool CanRefitTo(EngineID engine_type, CargoID cid_to);
 
CargoID FindFirstRefittableCargo(EngineID engine_type);
 
CommandCost GetRefitCost(EngineID engine_type);
 

	
 
void ViewportAddVehicles(DrawPixelInfo *dpi);
0 comments (0 inline, 0 general)