Skip to content

Commit b4ebb26

Browse files
authored
Codechange: Remove SpriteGroupType from SpriteGroup. (OpenTTD#14511)
1 parent f596498 commit b4ebb26

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

src/newgrf_profiling.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ void NewGRFProfiler::EndResolve(const ResolverResult &result)
7474
{
7575
return cb_result;
7676
}
77-
uint32_t operator()(const SpriteGroup *group)
77+
uint32_t operator()(const ResultSpriteGroup *group)
7878
{
79-
if (group == nullptr) return 0;
80-
if (group->type != SGT_RESULT) return group->nfo_line;
81-
return GetSpriteLocalID(static_cast<const ResultSpriteGroup *>(group)->sprite);
79+
return group == nullptr ? 0 : GetSpriteLocalID(group->sprite);
80+
}
81+
uint32_t operator()(const TileLayoutSpriteGroup *group)
82+
{
83+
return group == nullptr ? 0 : group->nfo_line;
84+
}
85+
uint32_t operator()(const IndustryProductionSpriteGroup *group)
86+
{
87+
return group == nullptr ? 0 : group->nfo_line;
8288
}
8389
};
8490
this->cur_call.result = std::visit(visitor{}, result);

src/newgrf_spritegroup.h

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@
2121
#include "newgrf_storage.h"
2222
#include "newgrf_commons.h"
2323

24-
/* List of different sprite group types */
25-
enum SpriteGroupType : uint8_t {
26-
SGT_REAL,
27-
SGT_DETERMINISTIC,
28-
SGT_RANDOMIZED,
29-
SGT_CALLBACK,
30-
SGT_RESULT,
31-
SGT_TILELAYOUT,
32-
SGT_INDUSTRY_PRODUCTION,
33-
};
34-
3524
struct SpriteGroup;
25+
struct ResultSpriteGroup;
26+
struct TileLayoutSpriteGroup;
27+
struct IndustryProductionSpriteGroup;
3628
struct ResolverObject;
3729
using CallbackResult = uint16_t;
3830

@@ -42,7 +34,7 @@ using CallbackResult = uint16_t;
4234
* - CallbackResult: Callback result.
4335
* - SpriteGroup: ResultSpriteGroup, TileLayoutSpriteGroup, IndustryProductionSpriteGroup
4436
*/
45-
using ResolverResult = std::variant<std::monostate, CallbackResult, const SpriteGroup *>;
37+
using ResolverResult = std::variant<std::monostate, CallbackResult, const ResultSpriteGroup *, const TileLayoutSpriteGroup *, const IndustryProductionSpriteGroup *>;
4638

4739
/* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
4840
* Adding an 'extra' margin would be assuming 64 sprite groups per real
@@ -54,15 +46,14 @@ extern SpriteGroupPool _spritegroup_pool;
5446
/* Common wrapper for all the different sprite group types */
5547
struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
5648
protected:
57-
SpriteGroup(SpriteGroupType type) : type(type) {}
49+
SpriteGroup() {} // Not `= default` as that resets PoolItem->index.
5850
/** Base sprite group resolver */
59-
virtual ResolverResult Resolve([[maybe_unused]] ResolverObject &object) const { return this; };
51+
virtual ResolverResult Resolve(ResolverObject &object) const = 0;
6052

6153
public:
6254
virtual ~SpriteGroup() = default;
6355

6456
uint32_t nfo_line = 0;
65-
SpriteGroupType type{};
6657

6758
static ResolverResult Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
6859
};
@@ -71,7 +62,7 @@ struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
7162
/* 'Real' sprite groups contain a list of other result or callback sprite
7263
* groups. */
7364
struct RealSpriteGroup : SpriteGroup {
74-
RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
65+
RealSpriteGroup() : SpriteGroup() {}
7566

7667
/* Loaded = in motion, loading = not moving
7768
* Each group contains several spritesets, for various loading stages */
@@ -166,7 +157,7 @@ struct DeterministicSpriteGroupRange {
166157

167158

168159
struct DeterministicSpriteGroup : SpriteGroup {
169-
DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
160+
DeterministicSpriteGroup() : SpriteGroup() {}
170161

171162
VarSpriteGroupScope var_scope{};
172163
DeterministicSpriteGroupSize size{};
@@ -188,7 +179,7 @@ enum RandomizedSpriteGroupCompareMode : uint8_t {
188179
};
189180

190181
struct RandomizedSpriteGroup : SpriteGroup {
191-
RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
182+
RandomizedSpriteGroup() : SpriteGroup() {}
192183

193184
VarSpriteGroupScope var_scope{}; ///< Take this object:
194185

@@ -212,7 +203,7 @@ struct CallbackResultSpriteGroup : SpriteGroup {
212203
* Creates a spritegroup representing a callback result
213204
* @param value The value that was used to represent this callback result
214205
*/
215-
explicit CallbackResultSpriteGroup(CallbackResult value) : SpriteGroup(SGT_CALLBACK), result(value) {}
206+
explicit CallbackResultSpriteGroup(CallbackResult value) : SpriteGroup(), result(value) {}
216207

217208
CallbackResult result = 0;
218209

@@ -224,43 +215,37 @@ struct CallbackResultSpriteGroup : SpriteGroup {
224215
/* A result sprite group returns the first SpriteID and the number of
225216
* sprites in the set */
226217
struct ResultSpriteGroup : SpriteGroup {
227-
static constexpr SpriteGroupType TYPE = SGT_RESULT;
228-
229218
/**
230219
* Creates a spritegroup representing a sprite number result.
231220
* @param sprite The sprite number.
232221
* @param num_sprites The number of sprites per set.
233222
* @return A spritegroup representing the sprite number result.
234223
*/
235-
ResultSpriteGroup(SpriteID sprite, uint8_t num_sprites) :
236-
SpriteGroup(TYPE),
237-
num_sprites(num_sprites),
238-
sprite(sprite)
239-
{
240-
}
224+
ResultSpriteGroup(SpriteID sprite, uint8_t num_sprites) : SpriteGroup(), num_sprites(num_sprites), sprite(sprite) {}
241225

242226
uint8_t num_sprites = 0;
243227
SpriteID sprite = 0;
228+
229+
protected:
230+
ResolverResult Resolve(ResolverObject &) const override { return this; }
244231
};
245232

246233
/**
247234
* Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
248235
*/
249236
struct TileLayoutSpriteGroup : SpriteGroup {
250-
static constexpr SpriteGroupType TYPE = SGT_TILELAYOUT;
251-
252-
TileLayoutSpriteGroup() : SpriteGroup(TYPE) {}
253-
~TileLayoutSpriteGroup() {}
237+
TileLayoutSpriteGroup() : SpriteGroup() {}
254238

255239
NewGRFSpriteLayout dts{};
256240

257241
SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const;
242+
243+
protected:
244+
ResolverResult Resolve(ResolverObject &) const override { return this; }
258245
};
259246

260247
struct IndustryProductionSpriteGroup : SpriteGroup {
261-
static constexpr SpriteGroupType TYPE = SGT_INDUSTRY_PRODUCTION;
262-
263-
IndustryProductionSpriteGroup() : SpriteGroup(TYPE) {}
248+
IndustryProductionSpriteGroup() : SpriteGroup() {}
264249

265250
uint8_t version = 0; ///< Production callback version used, or 0xFF if marked invalid
266251
uint8_t num_input = 0; ///< How many subtract_input values are valid
@@ -271,6 +256,8 @@ struct IndustryProductionSpriteGroup : SpriteGroup {
271256
std::array<CargoType, INDUSTRY_NUM_OUTPUTS> cargo_output{}; ///< Which output cargoes to add to (only cb version 2)
272257
uint8_t again = 0;
273258

259+
protected:
260+
ResolverResult Resolve(ResolverObject &) const override { return this; }
274261
};
275262

276263
/**
@@ -374,9 +361,9 @@ struct ResolverObject {
374361
inline const TSpriteGroup *Resolve()
375362
{
376363
auto result = this->DoResolve();
377-
const auto *group = std::get_if<const SpriteGroup *>(&result);
378-
if (group == nullptr || *group == nullptr || (*group)->type != TSpriteGroup::TYPE) return nullptr;
379-
return static_cast<const TSpriteGroup *>(*group);
364+
const auto *group = std::get_if<const TSpriteGroup *>(&result);
365+
if (group == nullptr) return nullptr;
366+
return *group;
380367
}
381368

382369
/**

0 commit comments

Comments
 (0)