1
0
Fork 0

Codechange: use std::vector for NewGRFSpriteLayout registers

pull/13577/head
Rubidium 2025-01-12 21:28:42 +01:00 committed by rubidium42
parent e7595c6c85
commit faa845398f
3 changed files with 10 additions and 23 deletions

View File

@ -818,7 +818,7 @@ static void ReadSpriteLayoutRegisters(ByteReader &buf, TileLayoutFlags flags, bo
{
if (!(flags & TLF_DRAWING_FLAGS)) return;
if (dts->registers == nullptr) dts->AllocateRegisters();
if (dts->registers.empty()) dts->AllocateRegisters();
TileLayoutRegisters &regs = const_cast<TileLayoutRegisters&>(dts->registers[index]);
regs.flags = flags & TLF_DRAWING_FLAGS;
@ -943,9 +943,9 @@ static bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool us
/* When the Action1 sets are unknown, everything should be 0 (no spriteset usage) or UINT16_MAX (some spriteset usage) */
assert(use_cur_spritesets || (is_consistent && (dts->consistent_max_offset == 0 || dts->consistent_max_offset == UINT16_MAX)));
if (!is_consistent || dts->registers != nullptr) {
if (!is_consistent || !dts->registers.empty()) {
dts->consistent_max_offset = 0;
if (dts->registers == nullptr) dts->AllocateRegisters();
if (dts->registers.empty()) dts->AllocateRegisters();
for (uint i = 0; i < num_building_sprites + 1; i++) {
TileLayoutRegisters &regs = const_cast<TileLayoutRegisters&>(dts->registers[i]);

View File

@ -573,14 +573,7 @@ bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t
void NewGRFSpriteLayout::Clone(const NewGRFSpriteLayout *source)
{
this->Clone((const DrawTileSprites*)source);
if (source->registers != nullptr) {
size_t count = 1 + source->seq.size(); // 1 for the ground sprite
TileLayoutRegisters *regs = MallocT<TileLayoutRegisters>(count);
MemCpyT(regs, source->registers, count);
this->registers = regs;
}
this->registers = source->registers;
}
@ -601,10 +594,9 @@ void NewGRFSpriteLayout::Allocate(uint num_sprites)
void NewGRFSpriteLayout::AllocateRegisters()
{
assert(!this->seq.empty());
assert(this->registers == nullptr);
assert(this->registers.empty());
size_t count = 1 + this->seq.size(); // 1 for the ground sprite
this->registers = CallocT<TileLayoutRegisters>(count);
this->registers.resize(1 + this->seq.size(), {}); // 1 for the ground sprite
}
/**
@ -635,7 +627,7 @@ uint32_t NewGRFSpriteLayout::PrepareLayout(uint32_t orig_offset, uint32_t newgrf
}
/* Determine the var10 values the action-1-2-3 chains needs to be resolved for,
* and apply the default sprite offsets (unless disabled). */
const TileLayoutRegisters *regs = this->registers;
const TileLayoutRegisters *regs = this->registers.empty() ? nullptr : this->registers.data();
bool ground = true;
for (DrawTileSeqStruct result : result_seq) {
TileLayoutFlags flags = TLF_NOTHING;
@ -688,7 +680,7 @@ uint32_t NewGRFSpriteLayout::PrepareLayout(uint32_t orig_offset, uint32_t newgrf
*/
void NewGRFSpriteLayout::ProcessRegisters(uint8_t resolved_var10, uint32_t resolved_sprite, bool separate_ground) const
{
const TileLayoutRegisters *regs = this->registers;
const TileLayoutRegisters *regs = this->registers.empty() ? nullptr : this->registers.data();
bool ground = true;
for (DrawTileSeqStruct &result : result_seq) {
TileLayoutFlags flags = TLF_NOTHING;

View File

@ -111,7 +111,7 @@ static const uint TLR_MAX_VAR10 = 7; ///< Maximum value for var 10.
*/
struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
std::vector<DrawTileSeqStruct> seq;
const TileLayoutRegisters *registers;
std::vector<TileLayoutRegisters> registers;
/**
* Number of sprites in all referenced spritesets.
@ -137,11 +137,6 @@ struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
this->seq.insert(this->seq.end(), source_sequence.begin(), source_sequence.end());
}
virtual ~NewGRFSpriteLayout()
{
free(this->registers);
}
/**
* Tests whether this spritelayout needs preprocessing by
* #PrepareLayout() and #ProcessRegisters(), or whether it can be
@ -150,7 +145,7 @@ struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
*/
bool NeedsPreprocessing() const
{
return this->registers != nullptr;
return !this->registers.empty();
}
uint32_t PrepareLayout(uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground) const;