Codechange: Store station layout tiles as std::span.

Using std::span provides both the start and end of the list, which allows validating that the requested layout is in range.
This commit is contained in:
Peter Nelson 2024-04-18 12:09:44 +01:00 committed by Peter Nelson
parent 70a2ed062d
commit d08636c841
3 changed files with 13 additions and 4 deletions

View File

@ -3008,9 +3008,17 @@ static CommandCost RemoveDock(TileIndex tile, DoCommandFlag flags)
#include "table/station_land.h"
/**
* Get station tile layout for a station type and its station gfx.
* @param st Station type to draw.
* @param gfx StationGfx of tile to draw.
* @return Tile layout to draw.
*/
const DrawTileSprites *GetStationTileLayout(StationType st, uint8_t gfx)
{
return &_station_display_datas[st][gfx];
const auto &layouts = _station_display_datas[st];
if (gfx >= layouts.size()) gfx &= 1;
return layouts.data() + gfx;
}
/**

View File

@ -28,7 +28,7 @@ static const StationID INVALID_STATION = 0xFFFF;
typedef SmallStack<StationID, StationID, INVALID_STATION, 8, 0xFFFD> StationIDStack;
/** Station types */
enum StationType {
enum StationType : uint8_t {
STATION_RAIL,
STATION_AIRPORT,
STATION_TRUCK,
@ -38,6 +38,7 @@ enum StationType {
STATION_BUOY,
STATION_WAYPOINT,
STATION_ROADWAYPOINT,
STATION_END,
};
/** Types of RoadStops */

View File

@ -1013,7 +1013,7 @@ static const DrawTileSprites _station_display_datas_waypoint[] = {
* As these are drawn/build like stations, they may use the same number of layouts. */
static_assert(lengthof(_station_display_datas_rail) == lengthof(_station_display_datas_waypoint));
static const DrawTileSprites * const _station_display_datas[] = {
static const std::array<std::span<const DrawTileSprites>, STATION_END> _station_display_datas = {{
_station_display_datas_rail,
_station_display_datas_airport,
_station_display_datas_truck,
@ -1023,4 +1023,4 @@ static const DrawTileSprites * const _station_display_datas[] = {
_station_display_datas_buoy,
_station_display_datas_waypoint,
_station_display_datas_road_waypoint,
};
}};