mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-23 10:51:58 -05:00
Reduce use of get_sprite (#12382)
* Reduce use of get_sprite * Update src/openrct2/world/Sprite.cpp Co-authored-by: Michael Steenbeek <m.o.steenbeek@gmail.com> Co-authored-by: Michael Steenbeek <m.o.steenbeek@gmail.com>
This commit is contained in:
parent
b70716d5f6
commit
3de2de9814
4 changed files with 21 additions and 12 deletions
|
@ -31,7 +31,8 @@ struct GameStateSnapshot_t
|
|||
MemoryStream storedSprites;
|
||||
MemoryStream parkParameters;
|
||||
|
||||
void SerialiseSprites(rct_sprite* sprites, const size_t numSprites, bool saving)
|
||||
// Must pass a function that can access the sprite.
|
||||
void SerialiseSprites(std::function<rct_sprite*(const size_t)> getEntity, const size_t numSprites, bool saving)
|
||||
{
|
||||
const bool loading = !saving;
|
||||
|
||||
|
@ -47,7 +48,8 @@ struct GameStateSnapshot_t
|
|||
{
|
||||
for (size_t i = 0; i < numSprites; i++)
|
||||
{
|
||||
if (sprites[i].generic.sprite_identifier == SPRITE_IDENTIFIER_NULL)
|
||||
auto entity = getEntity(i);
|
||||
if (entity == nullptr || entity->generic.sprite_identifier == SPRITE_IDENTIFIER_NULL)
|
||||
continue;
|
||||
indexTable.push_back(static_cast<uint32_t>(i));
|
||||
}
|
||||
|
@ -66,7 +68,13 @@ struct GameStateSnapshot_t
|
|||
ds << indexTable[i];
|
||||
|
||||
const uint32_t spriteIdx = indexTable[i];
|
||||
rct_sprite& sprite = sprites[spriteIdx];
|
||||
rct_sprite* entity = getEntity(spriteIdx);
|
||||
if (entity == nullptr)
|
||||
{
|
||||
log_error("Entity index corrupted!");
|
||||
return;
|
||||
}
|
||||
auto& sprite = *entity;
|
||||
|
||||
ds << sprite.generic.sprite_identifier;
|
||||
|
||||
|
@ -132,8 +140,8 @@ struct GameStateSnapshots final : public IGameStateSnapshots
|
|||
|
||||
virtual void Capture(GameStateSnapshot_t& snapshot) override final
|
||||
{
|
||||
// TODO refactor to not use this as a proxy for getting a pointer to the sprite array
|
||||
snapshot.SerialiseSprites(get_sprite(0), MAX_SPRITES, true);
|
||||
snapshot.SerialiseSprites(
|
||||
[](const size_t index) { return reinterpret_cast<rct_sprite*>(GetEntity(index)); }, MAX_SPRITES, true);
|
||||
|
||||
// log_info("Snapshot size: %u bytes", static_cast<uint32_t>(snapshot.storedSprites.GetLength()));
|
||||
}
|
||||
|
@ -167,7 +175,7 @@ struct GameStateSnapshots final : public IGameStateSnapshots
|
|||
sprite.generic.sprite_identifier = SPRITE_IDENTIFIER_NULL;
|
||||
}
|
||||
|
||||
snapshot.SerialiseSprites(spriteList.data(), MAX_SPRITES, false);
|
||||
snapshot.SerialiseSprites([&spriteList](const size_t index) { return &spriteList[index]; }, MAX_SPRITES, false);
|
||||
|
||||
return spriteList;
|
||||
}
|
||||
|
|
|
@ -248,7 +248,7 @@ namespace OpenRCT2::Scripting
|
|||
private:
|
||||
Vehicle* GetVehicle() const
|
||||
{
|
||||
return get_sprite(_id)->generic.As<Vehicle>();
|
||||
return ::GetEntity<Vehicle>(_id);
|
||||
}
|
||||
|
||||
uint8_t rideObject_get() const
|
||||
|
|
|
@ -284,11 +284,12 @@ rct_sprite_checksum sprite_checksum()
|
|||
for (size_t i = 0; i < MAX_SPRITES; i++)
|
||||
{
|
||||
// TODO create a way to copy only the specific type
|
||||
auto sprite = get_sprite(i);
|
||||
if (sprite->generic.sprite_identifier != SPRITE_IDENTIFIER_NULL
|
||||
&& sprite->generic.sprite_identifier != SPRITE_IDENTIFIER_MISC)
|
||||
auto sprite = GetEntity(i);
|
||||
if (sprite != nullptr && sprite->sprite_identifier != SPRITE_IDENTIFIER_NULL
|
||||
&& sprite->sprite_identifier != SPRITE_IDENTIFIER_MISC)
|
||||
{
|
||||
auto copy = *sprite;
|
||||
// Upconvert it to rct_sprite so that the full size is copied.
|
||||
auto copy = *reinterpret_cast<rct_sprite*>(sprite);
|
||||
|
||||
// Only required for rendering/invalidation, has no meaning to the game state.
|
||||
copy.generic.sprite_left = copy.generic.sprite_right = copy.generic.sprite_top = copy.generic.sprite_bottom = 0;
|
||||
|
|
|
@ -108,7 +108,7 @@ static std::unique_ptr<GameState_t> GetGameState(std::unique_ptr<IContext>& cont
|
|||
std::unique_ptr<GameState_t> res = std::make_unique<GameState_t>();
|
||||
for (size_t spriteIdx = 0; spriteIdx < MAX_SPRITES; spriteIdx++)
|
||||
{
|
||||
rct_sprite* sprite = get_sprite(spriteIdx);
|
||||
rct_sprite* sprite = reinterpret_cast<rct_sprite*>(GetEntity(spriteIdx));
|
||||
if (sprite == nullptr)
|
||||
res->sprites[spriteIdx].generic.sprite_identifier = SPRITE_IDENTIFIER_NULL;
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue