Codechange: Use std::vector for NewGRF station tile sprite layouts.

(cherry picked from commit a3e49178d1)
This commit is contained in:
Peter Nelson
2021-05-01 20:28:23 +01:00
committed by Jonathan G Rennison
parent 72bc8c7595
commit da571d6481
4 changed files with 26 additions and 27 deletions

View File

@@ -1939,13 +1939,13 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, cons
break; break;
} }
case 0x09: // Define sprite layout case 0x09: { // Define sprite layout
statspec->tiles = buf->ReadExtendedByte(); uint16 tiles = buf->ReadExtendedByte();
delete[] statspec->renderdata; // delete earlier loaded stuff statspec->renderdata.clear(); // delete earlier loaded stuff
statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles]; statspec->renderdata.reserve(tiles);
for (uint t = 0; t < statspec->tiles; t++) { for (uint t = 0; t < tiles; t++) {
NewGRFSpriteLayout *dts = &statspec->renderdata[t]; NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
dts->consistent_max_offset = UINT16_MAX; // Spritesets are unknown, so no limit. dts->consistent_max_offset = UINT16_MAX; // Spritesets are unknown, so no limit.
if (buf->HasData(4) && *(unaligned_uint32*)buf->Data() == 0) { if (buf->HasData(4) && *(unaligned_uint32*)buf->Data() == 0) {
@@ -1981,6 +1981,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, cons
dts->Clone(tmp_layout.data()); dts->Clone(tmp_layout.data());
} }
break; break;
}
case 0x0A: { // Copy sprite layout case 0x0A: { // Copy sprite layout
byte srcid = buf->ReadByte(); byte srcid = buf->ReadByte();
@@ -1991,12 +1992,12 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, cons
continue; continue;
} }
delete[] statspec->renderdata; // delete earlier loaded stuff statspec->renderdata.clear(); // delete earlier loaded stuff
statspec->renderdata.reserve(srcstatspec->renderdata.size());
statspec->tiles = srcstatspec->tiles; for (const auto &it : srcstatspec->renderdata) {
statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles]; NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
for (uint t = 0; t < statspec->tiles; t++) { dts->Clone(&it);
statspec->renderdata[t].Clone(&srcstatspec->renderdata[t]);
} }
break; break;
} }
@@ -2082,18 +2083,19 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, cons
statspec->animation.triggers = buf->ReadWord(); statspec->animation.triggers = buf->ReadWord();
break; break;
case 0x1A: // Advanced sprite layout case 0x1A: { // Advanced sprite layout
statspec->tiles = buf->ReadExtendedByte(); uint16 tiles = buf->ReadExtendedByte();
delete[] statspec->renderdata; // delete earlier loaded stuff statspec->renderdata.clear(); // delete earlier loaded stuff
statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles]; statspec->renderdata.reserve(tiles);
for (uint t = 0; t < statspec->tiles; t++) { for (uint t = 0; t < tiles; t++) {
NewGRFSpriteLayout *dts = &statspec->renderdata[t]; NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
uint num_building_sprites = buf->ReadByte(); uint num_building_sprites = buf->ReadByte();
/* On error, bail out immediately. Temporary GRF data was already freed */ /* On error, bail out immediately. Temporary GRF data was already freed */
if (ReadSpriteLayout(buf, num_building_sprites, false, GSF_STATIONS, true, false, dts)) return CIR_DISABLED; if (ReadSpriteLayout(buf, num_building_sprites, false, GSF_STATIONS, true, false, dts)) return CIR_DISABLED;
} }
break; break;
}
case A0RPI_STATION_MIN_BRIDGE_HEIGHT: case A0RPI_STATION_MIN_BRIDGE_HEIGHT:
if (MappedPropertyLengthMismatch(buf, 8, mapping_entry)) break; if (MappedPropertyLengthMismatch(buf, 8, mapping_entry)) break;
@@ -8993,8 +8995,6 @@ static void ResetCustomStations()
if (stations[i] == nullptr) continue; if (stations[i] == nullptr) continue;
StationSpec *statspec = stations[i]; StationSpec *statspec = stations[i];
delete[] statspec->renderdata;
/* Release this station */ /* Release this station */
delete statspec; delete statspec;
} }

View File

@@ -800,10 +800,10 @@ bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID
const NewGRFSpriteLayout *layout = nullptr; const NewGRFSpriteLayout *layout = nullptr;
DrawTileSprites tmp_rail_layout; DrawTileSprites tmp_rail_layout;
if (statspec->renderdata == nullptr) { if (statspec->renderdata.empty()) {
sprites = GetStationTileLayout(STATION_RAIL, tile + axis); sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
} else { } else {
layout = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis]; layout = &statspec->renderdata[(tile < statspec->renderdata.size()) ? tile + axis : (uint)axis];
if (!layout->NeedsPreprocessing()) { if (!layout->NeedsPreprocessing()) {
sprites = layout; sprites = layout;
layout = nullptr; layout = nullptr;

View File

@@ -119,8 +119,8 @@ enum StationSpecIntlFlags {
/** Station specification. */ /** Station specification. */
struct StationSpec { struct StationSpec {
StationSpec() : cls_id(STAT_CLASS_DFLT), name(0), StationSpec() : cls_id(STAT_CLASS_DFLT), name(0),
disallowed_platforms(0), disallowed_lengths(0), tiles(0), disallowed_platforms(0), disallowed_lengths(0),
renderdata(nullptr), cargo_threshold(0), cargo_triggers(0), cargo_threshold(0), cargo_triggers(0),
callback_mask(0), flags(0), pylons(0), wires(0), blocked(0), callback_mask(0), flags(0), pylons(0), wires(0), blocked(0),
animation({0, 0, 0, 0}), internal_flags(0) {} animation({0, 0, 0, 0}), internal_flags(0) {}
/** /**
@@ -152,8 +152,7 @@ struct StationSpec {
* 4-5 = platform with roof, left side * 4-5 = platform with roof, left side
* 6-7 = platform with roof, right side * 6-7 = platform with roof, right side
*/ */
uint tiles; std::vector<NewGRFSpriteLayout> renderdata; ///< Array of tile layouts.
NewGRFSpriteLayout *renderdata; ///< Array of tile layouts.
/** /**
* Cargo threshold for choosing between little and lots of cargo * Cargo threshold for choosing between little and lots of cargo

View File

@@ -3087,8 +3087,8 @@ static void DrawTile_Station(TileInfo *ti, DrawTileProcParams params)
} }
/* Ensure the chosen tile layout is valid for this custom station */ /* Ensure the chosen tile layout is valid for this custom station */
if (statspec->renderdata != nullptr) { if (!statspec->renderdata.empty()) {
layout = &statspec->renderdata[tile_layout < statspec->tiles ? tile_layout : (uint)GetRailStationAxis(ti->tile)]; layout = &statspec->renderdata[tile_layout < statspec->renderdata.size() ? tile_layout : (uint)GetRailStationAxis(ti->tile)];
if (!layout->NeedsPreprocessing()) { if (!layout->NeedsPreprocessing()) {
t = layout; t = layout;
layout = nullptr; layout = nullptr;