Mercurial > hg > openttd
changeset 9022:db43607c4798 draft
(svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
author | peter1138 <peter1138@openttd.org> |
---|---|
date | Mon, 21 Apr 2008 20:50:58 +0000 |
parents | e5aba9a61144 |
children | 8358b67f9780 |
files | src/aircraft.h src/aircraft_cmd.cpp src/roadveh.h src/roadveh_cmd.cpp src/ship.h src/ship_cmd.cpp src/train.h src/train_cmd.cpp src/vehicle_base.h |
diffstat | 9 files changed, 116 insertions(+), 128 deletions(-) [+] |
line wrap: on
line diff
--- a/src/aircraft.h +++ b/src/aircraft.h @@ -119,7 +119,7 @@ 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; }
--- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -160,16 +160,17 @@ } #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; - 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 @@ 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 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; }
--- a/src/roadveh.h +++ b/src/roadveh.h @@ -73,7 +73,7 @@ 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); }
--- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -94,36 +94,42 @@ 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)
--- a/src/ship.h +++ b/src/ship.h @@ -35,7 +35,7 @@ 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; }
--- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -58,20 +58,23 @@ 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 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; - if (sprite != 0) return sprite; spritenum = _orig_ship_vehicle_info[this->engine_type - SHIP_ENGINES_INDEX].image_index; } + return _ship_sprites[spritenum] + direction; }
--- a/src/train.h +++ b/src/train.h @@ -296,7 +296,7 @@ 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;
--- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -467,59 +467,61 @@ 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]); - - if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img]; - return base; + 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]; + + 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); - - 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; - } - } - if (image == 0) { - image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; + 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); } - - 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)
--- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -390,7 +390,7 @@ * @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.