(svn r21911) -Codechange: move tcache.last_speed to gcache.last_speed and make SetLastSpeed a function of GroundVehicle

This commit is contained in:
rubidium 2011-01-26 17:31:07 +00:00
parent 12b64a12b0
commit b11f1bd4ed
4 changed files with 26 additions and 19 deletions

View File

@ -13,7 +13,9 @@
#define GROUND_VEHICLE_HPP
#include "vehicle_base.h"
#include "vehicle_gui.h"
#include "landscape.h"
#include "window_func.h"
/** What is the status of our acceleration? */
enum AccelStatus {
@ -41,6 +43,9 @@ struct GroundVehicleCache {
uint16 cached_total_length; ///< Length of the whole vehicle (valid only for the first engine).
EngineID first_engine; ///< Cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
uint8 cached_veh_length; ///< Length of this vehicle in units of 1/8 of normal length. It is cached because this can be set by a callback.
/* Cached UI information. */
uint16 last_speed; ///< The last speed we did display, so we only have to redraw when this changes.
};
/** Ground vehicle flags. */
@ -357,6 +362,21 @@ struct GroundVehicle : public SpecializedVehicle<T, Type> {
* @return True if the engine has an articulated part.
*/
FORCEINLINE bool HasArticulatedPart() const { return this->Next() != NULL && this->Next()->IsArticulatedPart(); }
/**
* Update the GUI variant of the current speed of the vehicle.
* Also mark the widget dirty when that is needed, i.e. when
* the speed of this vehicle has changed.
*/
FORCEINLINE void SetLastSpeed()
{
if (this->cur_speed != this->gcache.last_speed) {
if (_settings_client.gui.vehicle_speed || (this->gcache.last_speed == 0) != (this->cur_speed == 0)) {
SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
}
this->gcache.last_speed = this->cur_speed;
}
}
};
#endif /* GROUND_VEHICLE_HPP */

View File

@ -336,7 +336,7 @@ void AfterLoadVehicles(bool part_of_load)
case VEH_TRAIN: {
Train *t = Train::From(v);
if (t->IsFrontEngine() || t->IsFreeWagon()) {
t->tcache.last_speed = t->cur_speed; // update displayed train speed
t->gcache.last_speed = t->cur_speed; // update displayed train speed
t->ConsistChanged(false);
}
break;

View File

@ -65,8 +65,6 @@ struct TrainCache {
/* Cached wagon override spritegroup */
const struct SpriteGroup *cached_override;
uint16 last_speed; // NOSAVE: only used in UI
/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
bool cached_tilt; ///< train can tilt; feature provides a bonus in curves
@ -110,7 +108,7 @@ struct Train : public GroundVehicle<Train, VEH_TRAIN> {
void PlayLeaveStationSound() const;
bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
SpriteID GetImage(Direction direction) const;
int GetDisplaySpeed() const { return this->tcache.last_speed; }
int GetDisplaySpeed() const { return this->gcache.last_speed; }
int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; }
Money GetRunningCost() const;
int GetDisplayImageWidth(Point *offset = NULL) const;

View File

@ -1395,17 +1395,6 @@ void Train::UpdateDeltaXY(Direction direction)
this->z_extent = 6;
}
static inline void SetLastSpeed(Train *v, int spd)
{
int old = v->tcache.last_speed;
if (spd != old) {
v->tcache.last_speed = spd;
if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) {
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
}
}
}
/** Mark a train as stuck and stop it if it isn't stopped right now. */
static void MarkTrainAsStuck(Train *v)
{
@ -1418,7 +1407,7 @@ static void MarkTrainAsStuck(Train *v)
/* Stop train */
v->cur_speed = 0;
v->subspeed = 0;
SetLastSpeed(v, 0);
v->SetLastSpeed();
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
}
@ -1843,7 +1832,7 @@ CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32
ToggleBit(v->flags, VRF_REVERSING);
} else {
v->cur_speed = 0;
SetLastSpeed(v, 0);
v->SetLastSpeed();
HideFillingPercent(&v->fill_percent_te_id);
ReverseTrainDirection(v);
}
@ -3659,7 +3648,7 @@ static bool TrainLocoHandler(Train *v, bool mode)
int adv_spd = v->GetAdvanceDistance();
if (j < adv_spd) {
/* if the vehicle has speed 0, update the last_speed field. */
if (v->cur_speed == 0) SetLastSpeed(v, v->cur_speed);
if (v->cur_speed == 0) v->SetLastSpeed();
} else {
TrainCheckIfLineEnds(v);
/* Loop until the train has finished moving. */
@ -3683,7 +3672,7 @@ static bool TrainLocoHandler(Train *v, bool mode)
ProcessOrders(v);
}
}
SetLastSpeed(v, v->cur_speed);
v->SetLastSpeed();
}
for (Train *u = v; u != NULL; u = u->Next()) {