mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 06:15:04 +00:00
(svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
This commit is contained in:
parent
173ac52da5
commit
5dcd689f56
@ -119,7 +119,7 @@ struct Aircraft : public Vehicle {
|
||||
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;
|
||||
SpriteID GetImage(Direction direction) const;
|
||||
int GetDisplaySpeed() const { return this->cur_speed * 10 / 16; }
|
||||
int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 16; }
|
||||
Money GetRunningCost() const { return AircraftVehInfo(this->engine_type)->running_cost * _price.aircraft_running; }
|
||||
|
@ -160,16 +160,17 @@ static bool HaveHangarInOrderList(Vehicle *v)
|
||||
}
|
||||
#endif
|
||||
|
||||
int Aircraft::GetImage(Direction direction) const
|
||||
SpriteID Aircraft::GetImage(Direction direction) const
|
||||
{
|
||||
int spritenum = this->spritenum;
|
||||
uint8 spritenum = this->spritenum;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
int sprite = GetCustomVehicleSprite(this, direction);
|
||||
|
||||
SpriteID sprite = GetCustomVehicleSprite(this, direction);
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_aircraft_vehicle_info[this->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
|
||||
}
|
||||
|
||||
return direction + _aircraft_sprite[spritenum];
|
||||
}
|
||||
|
||||
@ -179,33 +180,33 @@ SpriteID GetRotorImage(const Vehicle *v)
|
||||
|
||||
const Vehicle *w = v->Next()->Next();
|
||||
if (is_custom_sprite(v->spritenum)) {
|
||||
SpriteID spritenum = GetCustomRotorSprite(v, false);
|
||||
if (spritenum != 0) return spritenum;
|
||||
SpriteID sprite = GetCustomRotorSprite(v, false);
|
||||
if (sprite != 0) return sprite;
|
||||
}
|
||||
|
||||
/* Return standard rotor sprites if there are no custom sprites for this helicopter */
|
||||
return SPR_ROTOR_STOPPED + w->u.air.state;
|
||||
}
|
||||
|
||||
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
static SpriteID GetAircraftIcon(EngineID engine)
|
||||
{
|
||||
const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
|
||||
int spritenum = avi->image_index;
|
||||
SpriteID sprite = 0;
|
||||
uint8 spritenum = AircraftVehInfo(engine)->image_index;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (sprite == 0) {
|
||||
spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
||||
}
|
||||
}
|
||||
if (sprite == 0) {
|
||||
sprite = 6 + _aircraft_sprite[spritenum];
|
||||
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
||||
}
|
||||
|
||||
DrawSprite(sprite, pal, x, y);
|
||||
return 6 + _aircraft_sprite[spritenum];
|
||||
}
|
||||
|
||||
if (!(avi->subtype & AIR_CTOL)) {
|
||||
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
{
|
||||
DrawSprite(GetAircraftIcon(engine), pal, x, y);
|
||||
|
||||
if (!(AircraftVehInfo(engine)->subtype & AIR_CTOL)) {
|
||||
SpriteID rotor_sprite = GetCustomRotorIcon(engine);
|
||||
if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
|
||||
DrawSprite(rotor_sprite, PAL_NONE, x, y - 5);
|
||||
@ -219,21 +220,9 @@ void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
*/
|
||||
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height)
|
||||
{
|
||||
const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
|
||||
int spritenum = avi->image_index;
|
||||
SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
|
||||
const Sprite *spr = GetSprite(GetAircraftIcon(engine));
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (sprite == 0) {
|
||||
spritenum = _orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
||||
sprite = (6 + _aircraft_sprite[spritenum]);
|
||||
}
|
||||
}
|
||||
|
||||
const Sprite *spr = GetSprite(sprite);
|
||||
|
||||
width = spr->width ;
|
||||
width = spr->width;
|
||||
height = spr->height;
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ struct RoadVehicle : public Vehicle {
|
||||
ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
|
||||
WindowClass GetVehicleListWindowClass() const { return WC_ROADVEH_LIST; }
|
||||
bool IsPrimaryVehicle() const { return IsRoadVehFront(this); }
|
||||
int GetImage(Direction direction) const;
|
||||
SpriteID GetImage(Direction direction) const;
|
||||
int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
|
||||
int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
|
||||
Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * GetPriceByIndex(RoadVehInfo(this->engine_type)->running_cost_class); }
|
||||
|
@ -94,36 +94,42 @@ static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
|
||||
TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
|
||||
};
|
||||
|
||||
int RoadVehicle::GetImage(Direction direction) const
|
||||
static SpriteID GetRoadVehIcon(EngineID engine)
|
||||
{
|
||||
int img = this->spritenum;
|
||||
int image;
|
||||
uint8 spritenum = RoadVehInfo(engine)->image_index;
|
||||
|
||||
if (is_custom_sprite(img)) {
|
||||
image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
|
||||
if (image != 0) return image;
|
||||
img = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
|
||||
}
|
||||
|
||||
image = direction + _roadveh_images[img];
|
||||
if (this->cargo.Count() >= this->cargo_cap / 2U) image += _roadveh_full_adder[img];
|
||||
return image;
|
||||
return 6 + _roadveh_images[spritenum];
|
||||
}
|
||||
|
||||
SpriteID RoadVehicle::GetImage(Direction direction) const
|
||||
{
|
||||
uint8 spritenum = this->spritenum;
|
||||
SpriteID sprite;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
|
||||
}
|
||||
|
||||
sprite = direction + _roadveh_images[spritenum];
|
||||
|
||||
if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
{
|
||||
int spritenum = RoadVehInfo(engine)->image_index;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
|
||||
if (sprite != 0) {
|
||||
DrawSprite(sprite, pal, x, y);
|
||||
return;
|
||||
}
|
||||
spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
|
||||
}
|
||||
DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
|
||||
DrawSprite(GetRoadVehIcon(engine), pal, x, y);
|
||||
}
|
||||
|
||||
static CommandCost EstimateRoadVehCost(EngineID engine_type)
|
||||
|
@ -35,7 +35,7 @@ struct Ship: public Vehicle {
|
||||
WindowClass GetVehicleListWindowClass() const { return WC_SHIPS_LIST; }
|
||||
void PlayLeaveStationSound() const;
|
||||
bool IsPrimaryVehicle() const { return true; }
|
||||
int GetImage(Direction direction) const;
|
||||
SpriteID GetImage(Direction direction) const;
|
||||
int GetDisplaySpeed() const { return this->cur_speed * 10 / 32; }
|
||||
int GetDisplayMaxSpeed() const { return this->max_speed * 10 / 32; }
|
||||
Money GetRunningCost() const { return ShipVehInfo(this->engine_type)->running_cost * _price.ship_running; }
|
||||
|
@ -58,20 +58,23 @@ static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
|
||||
return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
|
||||
}
|
||||
|
||||
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
static SpriteID GetShipIcon(EngineID engine)
|
||||
{
|
||||
int spritenum = ShipVehInfo(engine)->image_index;
|
||||
uint8 spritenum = ShipVehInfo(engine)->image_index;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
if (sprite != 0) {
|
||||
DrawSprite(sprite, pal, x, y);
|
||||
return;
|
||||
}
|
||||
spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
||||
}
|
||||
DrawSprite(6 + _ship_sprites[spritenum], pal, x, y);
|
||||
|
||||
return 6 + _ship_sprites[spritenum];
|
||||
}
|
||||
|
||||
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
{
|
||||
DrawSprite(GetShipIcon(engine), pal, x, y);
|
||||
}
|
||||
|
||||
/** Get the size of the sprite of a ship sprite heading west (used for lists)
|
||||
@ -81,35 +84,23 @@ void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
*/
|
||||
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
|
||||
{
|
||||
SpriteID spritenum = ShipVehInfo(engine)->image_index;
|
||||
SpriteID custom_sprite = 0;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
custom_sprite = GetCustomVehicleIcon(engine, DIR_W);
|
||||
spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
||||
}
|
||||
if (custom_sprite == 0) {
|
||||
spritenum = 6 + _ship_sprites[spritenum];
|
||||
} else {
|
||||
spritenum = custom_sprite;
|
||||
}
|
||||
|
||||
const Sprite *spr = GetSprite(spritenum);
|
||||
const Sprite *spr = GetSprite(GetShipIcon(engine));
|
||||
|
||||
width = spr->width;
|
||||
height = spr->height;
|
||||
}
|
||||
|
||||
int Ship::GetImage(Direction direction) const
|
||||
SpriteID Ship::GetImage(Direction direction) const
|
||||
{
|
||||
int spritenum = this->spritenum;
|
||||
uint8 spritenum = this->spritenum;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
int sprite = GetCustomVehicleSprite(this, direction);
|
||||
|
||||
SpriteID sprite = GetCustomVehicleSprite(this, direction);
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_ship_vehicle_info[this->engine_type - SHIP_ENGINES_INDEX].image_index;
|
||||
}
|
||||
|
||||
return _ship_sprites[spritenum] + direction;
|
||||
}
|
||||
|
||||
|
@ -296,7 +296,7 @@ struct Train : public Vehicle {
|
||||
WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; }
|
||||
void PlayLeaveStationSound() const;
|
||||
bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
|
||||
int GetImage(Direction direction) const;
|
||||
SpriteID GetImage(Direction direction) const;
|
||||
int GetDisplaySpeed() const { return this->u.rail.last_speed * 10 / 16; }
|
||||
int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; }
|
||||
Money GetRunningCost() const;
|
||||
|
@ -467,59 +467,61 @@ void UpdateTrainAcceleration(Vehicle *v)
|
||||
v->acceleration = Clamp(power / weight * 4, 1, 255);
|
||||
}
|
||||
|
||||
int Train::GetImage(Direction direction) const
|
||||
SpriteID Train::GetImage(Direction direction) const
|
||||
{
|
||||
int img = this->spritenum;
|
||||
int base;
|
||||
uint8 spritenum = this->spritenum;
|
||||
SpriteID sprite;
|
||||
|
||||
if (HasBit(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
|
||||
|
||||
if (is_custom_sprite(img)) {
|
||||
base = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
|
||||
if (base != 0) return base;
|
||||
img = _orig_rail_vehicle_info[this->engine_type].image_index;
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
|
||||
if (sprite != 0) return sprite;
|
||||
|
||||
spritenum = _orig_rail_vehicle_info[this->engine_type].image_index;
|
||||
}
|
||||
|
||||
base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
|
||||
sprite = _engine_sprite_base[spritenum] + ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]);
|
||||
|
||||
if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
|
||||
return base;
|
||||
if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
|
||||
{
|
||||
Direction dir = rear_head ? DIR_E : DIR_W;
|
||||
uint8 spritenum = RailVehInfo(engine)->image_index;
|
||||
|
||||
if (is_custom_sprite(spritenum)) {
|
||||
SpriteID sprite = GetCustomVehicleIcon(engine, dir);
|
||||
if (sprite != 0) {
|
||||
y += _traininfo_vehicle_pitch; // TODO Make this per-GRF
|
||||
return sprite;
|
||||
}
|
||||
|
||||
spritenum = _orig_rail_vehicle_info[engine].image_index;
|
||||
}
|
||||
|
||||
if (rear_head) spritenum++;
|
||||
|
||||
return ((6 + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
|
||||
}
|
||||
|
||||
void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal)
|
||||
{
|
||||
const RailVehicleInfo *rvi = RailVehInfo(engine);
|
||||
if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
|
||||
int yf = y;
|
||||
int yr = y;
|
||||
|
||||
int img = rvi->image_index;
|
||||
SpriteID image = 0;
|
||||
|
||||
if (is_custom_sprite(img)) {
|
||||
image = GetCustomVehicleIcon(engine, DIR_W);
|
||||
if (image == 0) {
|
||||
img = _orig_rail_vehicle_info[engine].image_index;
|
||||
} else {
|
||||
y += _traininfo_vehicle_pitch;
|
||||
}
|
||||
SpriteID spritef = GetRailIcon(engine, false, yf);
|
||||
SpriteID spriter = GetRailIcon(engine, true, yr);
|
||||
DrawSprite(spritef, pal, x - 14, yf);
|
||||
DrawSprite(spriter, pal, x + 15, yr);
|
||||
} else {
|
||||
SpriteID sprite = GetRailIcon(engine, false, y);
|
||||
DrawSprite(sprite, pal, x, y);
|
||||
}
|
||||
if (image == 0) {
|
||||
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
|
||||
}
|
||||
|
||||
if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
|
||||
DrawSprite(image, pal, x - 14, y);
|
||||
x += 15;
|
||||
image = 0;
|
||||
if (is_custom_sprite(img)) {
|
||||
image = GetCustomVehicleIcon(engine, DIR_E);
|
||||
if (image == 0) img = _orig_rail_vehicle_info[engine].image_index;
|
||||
}
|
||||
if (image == 0) {
|
||||
image =
|
||||
((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
|
||||
_engine_sprite_base[img + 1];
|
||||
}
|
||||
}
|
||||
DrawSprite(image, pal, x, y);
|
||||
}
|
||||
|
||||
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
|
||||
|
@ -390,7 +390,7 @@ public:
|
||||
* @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; }
|
||||
virtual SpriteID GetImage(Direction direction) const { return 0; }
|
||||
|
||||
/**
|
||||
* Gets the speed in mph that can be sent into SetDParam for string processing.
|
||||
|
Loading…
Reference in New Issue
Block a user