summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2008-04-21 20:50:58 +0000
committerpeter1138 <peter1138@openttd.org>2008-04-21 20:50:58 +0000
commit5dcd689f5662d93ade95e3fd04332b8456605add (patch)
treeb307e9b6916fb53727cd6a80f2c5478a0b4ab819
parent173ac52da5c933f35e5cae799679bed8c580078f (diff)
downloadopenttd-5dcd689f5662d93ade95e3fd04332b8456605add.tar.xz
(svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
-rw-r--r--src/aircraft.h2
-rw-r--r--src/aircraft_cmd.cpp55
-rw-r--r--src/roadveh.h2
-rw-r--r--src/roadveh_cmd.cpp44
-rw-r--r--src/ship.h2
-rw-r--r--src/ship_cmd.cpp43
-rw-r--r--src/train.h2
-rw-r--r--src/train_cmd.cpp78
-rw-r--r--src/vehicle_base.h2
9 files changed, 109 insertions, 121 deletions
diff --git a/src/aircraft.h b/src/aircraft.h
index 50611803c..671463b69 100644
--- a/src/aircraft.h
+++ b/src/aircraft.h
@@ -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; }
diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp
index a2da629ac..c0795b6fb 100644
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -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]);
-
- 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);
+ const Sprite *spr = GetSprite(GetAircraftIcon(engine));
- width = spr->width ;
+ width = spr->width;
height = spr->height;
}
diff --git a/src/roadveh.h b/src/roadveh.h
index 9159a8784..a89b73197 100644
--- a/src/roadveh.h
+++ b/src/roadveh.h
@@ -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); }
diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp
index 19578162f..28d515330 100644
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -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];
}
-void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
+SpriteID RoadVehicle::GetImage(Direction direction) const
{
- int spritenum = RoadVehInfo(engine)->image_index;
+ uint8 spritenum = this->spritenum;
+ SpriteID sprite;
if (is_custom_sprite(spritenum)) {
- int sprite = GetCustomVehicleIcon(engine, DIR_W);
+ sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
+ if (sprite != 0) return sprite;
- if (sprite != 0) {
- DrawSprite(sprite, pal, x, y);
- return;
- }
- spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
+ spritenum = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
}
- DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
+
+ 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)
+{
+ DrawSprite(GetRoadVehIcon(engine), pal, x, y);
}
static CommandCost EstimateRoadVehCost(EngineID engine_type)
diff --git a/src/ship.h b/src/ship.h
index b0a0400ba..3d2e3cc02 100644
--- a/src/ship.h
+++ b/src/ship.h
@@ -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; }
diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp
index 2a0e7222b..de4926999 100644
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -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;
}
diff --git a/src/train.h b/src/train.h
index 6aa2ad763..cc1882919 100644
--- a/src/train.h
+++ b/src/train.h
@@ -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;
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index a1a378602..84ad087c9 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -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) sprite += _wagon_full_adder[spritenum];
- if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
- return base;
+ return sprite;
}
-void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal)
+static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
{
- const RailVehicleInfo *rvi = RailVehInfo(engine);
-
- int img = rvi->image_index;
- SpriteID image = 0;
+ Direction dir = rear_head ? DIR_E : DIR_W;
+ uint8 spritenum = RailVehInfo(engine)->image_index;
- 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;
+ if (is_custom_sprite(spritenum)) {
+ SpriteID sprite = GetCustomVehicleIcon(engine, dir);
+ if (sprite != 0) {
+ y += _traininfo_vehicle_pitch; // TODO Make this per-GRF
+ return sprite;
}
- }
- if (image == 0) {
- image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
+
+ spritenum = _orig_rail_vehicle_info[engine].image_index;
}
- 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];
- }
+ 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)
+{
+ if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
+ int yf = y;
+ int yr = y;
+
+ 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);
}
- DrawSprite(image, pal, x, y);
}
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
diff --git a/src/vehicle_base.h b/src/vehicle_base.h
index c27fd5aaa..283b743c9 100644
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -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.