summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Nelson <peter1138@openttd.org>2021-05-02 00:00:06 +0100
committerPeterN <peter@fuzzle.org>2021-05-02 09:41:01 +0100
commit1aeaf399541c229973b576f2f2423fa8c4b49f65 (patch)
tree489af8206117a928b5e4efb113bacc844fe44778 /src
parentf785a70a2bc08eaefcb1e2a43b8e6d36a154a00e (diff)
downloadopenttd-1aeaf399541c229973b576f2f2423fa8c4b49f65.tar.xz
Cleanup: Use std::vector in DeterministicSpriteGroup.
Diffstat (limited to 'src')
-rw-r--r--src/newgrf.cpp20
-rw-r--r--src/newgrf_spritegroup.cpp22
-rw-r--r--src/newgrf_spritegroup.h7
3 files changed, 13 insertions, 36 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index 1d30cb2d7..ba5c098a2 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -5024,16 +5024,13 @@ static void NewSpriteGroup(ByteReader *buf)
case 2: group->size = DSG_SIZE_DWORD; varsize = 4; break;
}
- static std::vector<DeterministicSpriteGroupAdjust> adjusts;
- adjusts.clear();
-
/* Loop through the var adjusts. Unfortunately we don't know how many we have
* from the outset, so we shall have to keep reallocing. */
do {
- DeterministicSpriteGroupAdjust &adjust = adjusts.emplace_back();
+ DeterministicSpriteGroupAdjust &adjust = group->adjusts.emplace_back();
/* The first var adjust doesn't have an operation specified, so we set it to add. */
- adjust.operation = adjusts.size() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
+ adjust.operation = group->adjusts.size() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
adjust.variable = buf->ReadByte();
if (adjust.variable == 0x7E) {
/* Link subroutine group */
@@ -5058,10 +5055,6 @@ static void NewSpriteGroup(ByteReader *buf)
/* Continue reading var adjusts while bit 5 is set. */
} while (HasBit(varadjust, 5));
- group->num_adjusts = (uint)adjusts.size();
- group->adjusts = MallocT<DeterministicSpriteGroupAdjust>(group->num_adjusts);
- MemCpyT(group->adjusts, adjusts.data(), group->num_adjusts);
-
std::vector<DeterministicSpriteGroupRange> ranges;
ranges.resize(buf->ReadByte());
for (uint i = 0; i < ranges.size(); i++) {
@@ -5098,27 +5091,20 @@ static void NewSpriteGroup(ByteReader *buf)
}
assert(target.size() == bounds.size());
- std::vector<DeterministicSpriteGroupRange> optimised;
for (uint j = 0; j < bounds.size(); ) {
if (target[j] != group->default_group) {
- DeterministicSpriteGroupRange r;
+ DeterministicSpriteGroupRange &r = group->ranges.emplace_back();
r.group = target[j];
r.low = bounds[j];
while (j < bounds.size() && target[j] == r.group) {
j++;
}
r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX;
- optimised.push_back(r);
} else {
j++;
}
}
- group->num_ranges = (uint)optimised.size(); // cast is safe, there should never be 2**31 elements here
- if (group->num_ranges > 0) {
- group->ranges = MallocT<DeterministicSpriteGroupRange>(group->num_ranges);
- MemCpyT(group->ranges, &optimised.front(), group->num_ranges);
- }
break;
}
diff --git a/src/newgrf_spritegroup.cpp b/src/newgrf_spritegroup.cpp
index fb0d65c31..058baf334 100644
--- a/src/newgrf_spritegroup.cpp
+++ b/src/newgrf_spritegroup.cpp
@@ -53,12 +53,6 @@ TemporaryStorageArray<int32, 0x110> _temp_store;
}
}
-DeterministicSpriteGroup::~DeterministicSpriteGroup()
-{
- free(this->adjusts);
- free(this->ranges);
-}
-
RandomizedSpriteGroup::~RandomizedSpriteGroup()
{
free(this->groups);
@@ -205,8 +199,8 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) con
ScopeResolver *scope = object.GetScope(this->var_scope);
- for (i = 0; i < this->num_adjusts; i++) {
- DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
+ for (i = 0; i < this->adjusts.size(); i++) {
+ const DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
/* Try to get the variable. We shall assume it is available, unless told otherwise. */
bool available = true;
@@ -250,16 +244,16 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) con
return &nvarzero;
}
- if (this->num_ranges > 4) {
- DeterministicSpriteGroupRange *lower = std::lower_bound(this->ranges + 0, this->ranges + this->num_ranges, value, RangeHighComparator);
- if (lower != this->ranges + this->num_ranges && lower->low <= value) {
+ if (this->ranges.size() > 4) {
+ const auto &lower = std::lower_bound(this->ranges.begin(), this->ranges.end(), value, RangeHighComparator);
+ if (lower != this->ranges.end() && lower->low <= value) {
assert(lower->low <= value && value <= lower->high);
return SpriteGroup::Resolve(lower->group, object, false);
}
} else {
- for (i = 0; i < this->num_ranges; i++) {
- if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
- return SpriteGroup::Resolve(this->ranges[i].group, object, false);
+ for (const auto &range : this->ranges) {
+ if (range.low <= value && value <= range.high) {
+ return SpriteGroup::Resolve(range.group, object, false);
}
}
}
diff --git a/src/newgrf_spritegroup.h b/src/newgrf_spritegroup.h
index a931597a0..e91987dbb 100644
--- a/src/newgrf_spritegroup.h
+++ b/src/newgrf_spritegroup.h
@@ -166,15 +166,12 @@ struct DeterministicSpriteGroupRange {
struct DeterministicSpriteGroup : SpriteGroup {
DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
- ~DeterministicSpriteGroup();
VarSpriteGroupScope var_scope;
DeterministicSpriteGroupSize size;
- uint num_adjusts;
- uint num_ranges;
bool calculated_result;
- DeterministicSpriteGroupAdjust *adjusts;
- DeterministicSpriteGroupRange *ranges; // Dynamically allocated
+ std::vector<DeterministicSpriteGroupAdjust> adjusts;
+ std::vector<DeterministicSpriteGroupRange> ranges; // Dynamically allocated
/* Dynamically allocated, this is the sole owner */
const SpriteGroup *default_group;