Codechange: Use a SmallVec for the animated tile list instead of replicating most of the logic.

This commit is contained in:
Michael Lutz 2018-04-15 01:15:26 +02:00
parent 4851feb102
commit 7dd6027194
4 changed files with 39 additions and 70 deletions

View File

@ -11,17 +11,14 @@
#include "stdafx.h" #include "stdafx.h"
#include "core/alloc_func.hpp" #include "core/alloc_func.hpp"
#include "core/smallvec_type.hpp"
#include "tile_cmd.h" #include "tile_cmd.h"
#include "viewport_func.h" #include "viewport_func.h"
#include "safeguards.h" #include "safeguards.h"
/** The table/list with animated tiles. */ /** The table/list with animated tiles. */
TileIndex *_animated_tile_list = NULL; SmallVector<TileIndex, 256> _animated_tiles;
/** The number of animated tiles in the current state. */
uint _animated_tile_count = 0;
/** The number of slots for animated tiles allocated currently. */
uint _animated_tile_allocated = 0;
/** /**
* Removes the given tile from the animated tile table. * Removes the given tile from the animated tile table.
@ -29,17 +26,11 @@ uint _animated_tile_allocated = 0;
*/ */
void DeleteAnimatedTile(TileIndex tile) void DeleteAnimatedTile(TileIndex tile)
{ {
for (TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) { TileIndex *to_remove = _animated_tiles.Find(tile);
if (tile == *ti) { if (to_remove != _animated_tiles.End()) {
/* Remove the hole /* The order of the remaining elements must stay the same, otherwise the animation loop may miss a tile. */
* The order of the remaining elements must stay the same, otherwise the animation loop _animated_tiles.ErasePreservingOrder(to_remove);
* may miss a tile; that's why we must use memmove instead of just moving the last element. MarkTileDirtyByTile(tile);
*/
memmove(ti, ti + 1, (_animated_tile_list + _animated_tile_count - (ti + 1)) * sizeof(*ti));
_animated_tile_count--;
MarkTileDirtyByTile(tile);
return;
}
} }
} }
@ -51,19 +42,7 @@ void DeleteAnimatedTile(TileIndex tile)
void AddAnimatedTile(TileIndex tile) void AddAnimatedTile(TileIndex tile)
{ {
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
_animated_tiles.Include(tile);
for (const TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) {
if (tile == *ti) return;
}
/* Table not large enough, so make it larger */
if (_animated_tile_count == _animated_tile_allocated) {
_animated_tile_allocated *= 2;
_animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, _animated_tile_allocated);
}
_animated_tile_list[_animated_tile_count] = tile;
_animated_tile_count++;
} }
/** /**
@ -71,8 +50,8 @@ void AddAnimatedTile(TileIndex tile)
*/ */
void AnimateAnimatedTiles() void AnimateAnimatedTiles()
{ {
const TileIndex *ti = _animated_tile_list; const TileIndex *ti = _animated_tiles.Begin();
while (ti < _animated_tile_list + _animated_tile_count) { while (ti < _animated_tiles.End()) {
const TileIndex curr = *ti; const TileIndex curr = *ti;
AnimateTile(curr); AnimateTile(curr);
/* During the AnimateTile call, DeleteAnimatedTile could have been called, /* During the AnimateTile call, DeleteAnimatedTile could have been called,
@ -93,7 +72,5 @@ void AnimateAnimatedTiles()
*/ */
void InitializeAnimatedTiles() void InitializeAnimatedTiles()
{ {
_animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, 256); _animated_tiles.Clear();
_animated_tile_count = 0;
_animated_tile_allocated = 256;
} }

View File

@ -2157,22 +2157,21 @@ bool AfterLoadGame()
/* Animated tiles would sometimes not be actually animated or /* Animated tiles would sometimes not be actually animated or
* in case of old savegames duplicate. */ * in case of old savegames duplicate. */
extern TileIndex *_animated_tile_list; extern SmallVector<TileIndex, 256> _animated_tiles;
extern uint _animated_tile_count;
for (uint i = 0; i < _animated_tile_count; /* Nothing */) { for (TileIndex *tile = _animated_tiles.Begin(); tile < _animated_tiles.End(); /* Nothing */) {
/* Remove if tile is not animated */ /* Remove if tile is not animated */
bool remove = _tile_type_procs[GetTileType(_animated_tile_list[i])]->animate_tile_proc == NULL; bool remove = _tile_type_procs[GetTileType(*tile)]->animate_tile_proc == NULL;
/* and remove if duplicate */ /* and remove if duplicate */
for (uint j = 0; !remove && j < i; j++) { for (TileIndex *j = _animated_tiles.Begin(); !remove && j < tile; j++) {
remove = _animated_tile_list[i] == _animated_tile_list[j]; remove = *tile == *j;
} }
if (remove) { if (remove) {
DeleteAnimatedTile(_animated_tile_list[i]); DeleteAnimatedTile(*tile);
} else { } else {
i++; tile++;
} }
} }
} }

View File

@ -12,22 +12,21 @@
#include "../stdafx.h" #include "../stdafx.h"
#include "../tile_type.h" #include "../tile_type.h"
#include "../core/alloc_func.hpp" #include "../core/alloc_func.hpp"
#include "../core/smallvec_type.hpp"
#include "saveload.h" #include "saveload.h"
#include "../safeguards.h" #include "../safeguards.h"
extern TileIndex *_animated_tile_list; extern SmallVector<TileIndex, 256> _animated_tiles;
extern uint _animated_tile_count;
extern uint _animated_tile_allocated;
/** /**
* Save the ANIT chunk. * Save the ANIT chunk.
*/ */
static void Save_ANIT() static void Save_ANIT()
{ {
SlSetLength(_animated_tile_count * sizeof(*_animated_tile_list)); SlSetLength(_animated_tiles.Length() * sizeof(*_animated_tiles.Begin()));
SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32); SlArray(_animated_tiles.Begin(), _animated_tiles.Length(), SLE_UINT32);
} }
/** /**
@ -38,22 +37,20 @@ static void Load_ANIT()
/* Before version 80 we did NOT have a variable length animated tile table */ /* Before version 80 we did NOT have a variable length animated tile table */
if (IsSavegameVersionBefore(80)) { if (IsSavegameVersionBefore(80)) {
/* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */ /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */
SlArray(_animated_tile_list, 256, IsSavegameVersionBefore(6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32); TileIndex anim_list[256];
SlArray(anim_list, 256, IsSavegameVersionBefore(6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32);
for (_animated_tile_count = 0; _animated_tile_count < 256; _animated_tile_count++) { for (int i = 0; i < 256; i++) {
if (_animated_tile_list[_animated_tile_count] == 0) break; if (anim_list[i] == 0) break;
*_animated_tiles.Append() = anim_list[i];
} }
return; return;
} }
_animated_tile_count = (uint)SlGetFieldLength() / sizeof(*_animated_tile_list); uint count = (uint)SlGetFieldLength() / sizeof(*_animated_tiles.Begin());
_animated_tiles.Clear();
/* Determine a nice rounded size for the amount of allocated tiles */ _animated_tiles.Append(count);
_animated_tile_allocated = 256; SlArray(_animated_tiles.Begin(), count, SLE_UINT32);
while (_animated_tile_allocated < _animated_tile_count) _animated_tile_allocated *= 2;
_animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, _animated_tile_allocated);
SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32);
} }
/** /**

View File

@ -28,6 +28,7 @@
#include "../engine_func.h" #include "../engine_func.h"
#include "../company_base.h" #include "../company_base.h"
#include "../disaster_vehicle.h" #include "../disaster_vehicle.h"
#include "../core/smallvec_type.hpp"
#include "saveload_internal.h" #include "saveload_internal.h"
#include "oldloader.h" #include "oldloader.h"
@ -490,8 +491,7 @@ static inline uint RemapOrderIndex(uint x)
return _savegame_type == SGT_TTO ? (x - 0x1AC4) / 2 : (x - 0x1C18) / 2; return _savegame_type == SGT_TTO ? (x - 0x1AC4) / 2 : (x - 0x1C18) / 2;
} }
extern TileIndex *_animated_tile_list; extern SmallVector<TileIndex, 256> _animated_tiles;
extern uint _animated_tile_count;
extern char *_old_name_array; extern char *_old_name_array;
static uint32 _old_town_index; static uint32 _old_town_index;
@ -640,22 +640,18 @@ static bool LoadOldOrder(LoadgameState *ls, int num)
static bool LoadOldAnimTileList(LoadgameState *ls, int num) static bool LoadOldAnimTileList(LoadgameState *ls, int num)
{ {
/* This is slightly hackish - we must load a chunk into an array whose TileIndex anim_list[256];
* address isn't static, but instead pointed to by _animated_tile_list.
* To achieve that, create an OldChunks list on the stack on the fly.
* The list cannot be static because the value of _animated_tile_list
* can change between calls. */
const OldChunks anim_chunk[] = { const OldChunks anim_chunk[] = {
OCL_VAR ( OC_TILE, 256, _animated_tile_list ), OCL_VAR ( OC_TILE, 256, anim_list ),
OCL_END () OCL_END ()
}; };
if (!LoadChunk(ls, NULL, anim_chunk)) return false; if (!LoadChunk(ls, NULL, anim_chunk)) return false;
/* Update the animated tile counter by counting till the first zero in the array */ /* The first zero in the loaded array indicates the end of the list. */
for (_animated_tile_count = 0; _animated_tile_count < 256; _animated_tile_count++) { for (int i = 0; i < 256; i++) {
if (_animated_tile_list[_animated_tile_count] == 0) break; if (anim_list[i] == 0) break;
*_animated_tiles.Append() = anim_list[i];
} }
return true; return true;