summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Nelson <peter1138@openttd.org>2021-05-01 20:28:23 +0100
committerPeterN <peter@fuzzle.org>2021-05-02 17:15:27 +0100
commita3e49178d1d5d65e3ce91328d743d1642f324961 (patch)
tree26305a3efe359650389fc620bedf58ef1b752a24
parentbd1a20f6eee6758f6a812af18fbe5b41b491b5c4 (diff)
downloadopenttd-a3e49178d1d5d65e3ce91328d743d1642f324961.tar.xz
Codechange: Use std::vector for NewGRF station tile sprite layouts.
-rw-r--r--src/newgrf.cpp38
-rw-r--r--src/newgrf_station.cpp4
-rw-r--r--src/newgrf_station.h7
-rw-r--r--src/station_cmd.cpp4
4 files changed, 26 insertions, 27 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index 9e8bbda19..a2671eff2 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -1904,13 +1904,13 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte
break;
}
- case 0x09: // Define sprite layout
- statspec->tiles = buf->ReadExtendedByte();
- delete[] statspec->renderdata; // delete earlier loaded stuff
- statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles];
+ case 0x09: { // Define sprite layout
+ uint16 tiles = buf->ReadExtendedByte();
+ statspec->renderdata.clear(); // delete earlier loaded stuff
+ statspec->renderdata.reserve(tiles);
- for (uint t = 0; t < statspec->tiles; t++) {
- NewGRFSpriteLayout *dts = &statspec->renderdata[t];
+ for (uint t = 0; t < tiles; t++) {
+ NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
dts->consistent_max_offset = UINT16_MAX; // Spritesets are unknown, so no limit.
if (buf->HasData(4) && *(uint32*)buf->Data() == 0) {
@@ -1946,6 +1946,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte
dts->Clone(tmp_layout.data());
}
break;
+ }
case 0x0A: { // Copy sprite layout
byte srcid = buf->ReadByte();
@@ -1956,12 +1957,12 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte
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;
- statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles];
- for (uint t = 0; t < statspec->tiles; t++) {
- statspec->renderdata[t].Clone(&srcstatspec->renderdata[t]);
+ for (const auto &it : srcstatspec->renderdata) {
+ NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
+ dts->Clone(&it);
}
break;
}
@@ -2047,18 +2048,19 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte
statspec->animation.triggers = buf->ReadWord();
break;
- case 0x1A: // Advanced sprite layout
- statspec->tiles = buf->ReadExtendedByte();
- delete[] statspec->renderdata; // delete earlier loaded stuff
- statspec->renderdata = new NewGRFSpriteLayout[statspec->tiles];
+ case 0x1A: { // Advanced sprite layout
+ uint16 tiles = buf->ReadExtendedByte();
+ statspec->renderdata.clear(); // delete earlier loaded stuff
+ statspec->renderdata.reserve(tiles);
- for (uint t = 0; t < statspec->tiles; t++) {
- NewGRFSpriteLayout *dts = &statspec->renderdata[t];
+ for (uint t = 0; t < tiles; t++) {
+ NewGRFSpriteLayout *dts = &statspec->renderdata.emplace_back();
uint num_building_sprites = buf->ReadByte();
/* 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;
}
break;
+ }
default:
ret = CIR_UNKNOWN;
@@ -8370,8 +8372,6 @@ static void ResetCustomStations()
if (stations[i] == nullptr) continue;
StationSpec *statspec = stations[i];
- delete[] statspec->renderdata;
-
/* Release this station */
delete statspec;
}
diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp
index 9aa3ad43b..eff5ef2b2 100644
--- a/src/newgrf_station.cpp
+++ b/src/newgrf_station.cpp
@@ -798,10 +798,10 @@ bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID
const NewGRFSpriteLayout *layout = nullptr;
DrawTileSprites tmp_rail_layout;
- if (statspec->renderdata == nullptr) {
+ if (statspec->renderdata.empty()) {
sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
} 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()) {
sprites = layout;
layout = nullptr;
diff --git a/src/newgrf_station.h b/src/newgrf_station.h
index 4c4a5831b..5273625ae 100644
--- a/src/newgrf_station.h
+++ b/src/newgrf_station.h
@@ -112,8 +112,8 @@ enum StationRandomTrigger {
/** Station specification. */
struct StationSpec {
StationSpec() : cls_id(STAT_CLASS_DFLT), name(0),
- disallowed_platforms(0), disallowed_lengths(0), tiles(0),
- renderdata(nullptr), cargo_threshold(0), cargo_triggers(0),
+ disallowed_platforms(0), disallowed_lengths(0),
+ cargo_threshold(0), cargo_triggers(0),
callback_mask(0), flags(0), pylons(0), wires(0), blocked(0),
animation({0, 0, 0, 0}) {}
/**
@@ -145,8 +145,7 @@ struct StationSpec {
* 4-5 = platform with roof, left side
* 6-7 = platform with roof, right side
*/
- uint tiles;
- NewGRFSpriteLayout *renderdata; ///< Array of tile layouts.
+ std::vector<NewGRFSpriteLayout> renderdata; ///< Array of tile layouts.
/**
* Cargo threshold for choosing between little and lots of cargo
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index ac78064d4..f642a914f 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -2844,8 +2844,8 @@ static void DrawTile_Station(TileInfo *ti)
}
/* Ensure the chosen tile layout is valid for this custom station */
- if (statspec->renderdata != nullptr) {
- layout = &statspec->renderdata[tile_layout < statspec->tiles ? tile_layout : (uint)GetRailStationAxis(ti->tile)];
+ if (!statspec->renderdata.empty()) {
+ layout = &statspec->renderdata[tile_layout < statspec->renderdata.size() ? tile_layout : (uint)GetRailStationAxis(ti->tile)];
if (!layout->NeedsPreprocessing()) {
t = layout;
layout = nullptr;