tron@2186: /* $Id$ */ tron@2186: rubidium@10268: /** @file animated_tile.cpp Everything related to animated tiles. */ belugas@6918: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@0: #include "saveload.h" rubidium@10268: #include "landscape.h" rubidium@8626: #include "core/alloc_func.hpp" rubidium@8627: #include "functions.h" truelight@0: rubidium@8190: /** The table/list with animated tiles. */ rubidium@8190: TileIndex *_animated_tile_list = NULL; rubidium@8190: /** The number of animated tiles in the current state. */ rubidium@8190: uint _animated_tile_count = 0; rubidium@8190: /** The number of slots for animated tiles allocated currently. */ rubidium@8190: static uint _animated_tile_allocated = 0; rubidium@8190: rubidium@8190: /** rubidium@8190: * Removes the given tile from the animated tile table. rubidium@8190: * @param tile the tile to remove rubidium@8190: */ tron@1977: void DeleteAnimatedTile(TileIndex tile) truelight@0: { rubidium@8190: for (TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) { tron@1977: if (tile == *ti) { rubidium@8245: /* Remove the hole rubidium@8245: * The order of the remaining elements must stay the same, otherwise the animation loop rubidium@8245: * may miss a tile; that's why we must use memmove instead of just moving the last element. rubidium@8245: */ rubidium@8245: memmove(ti, ti + 1, (_animated_tile_list + _animated_tile_count - (ti + 1)) * sizeof(*ti)); rubidium@8190: _animated_tile_count--; truelight@0: MarkTileDirtyByTile(tile); truelight@0: return; truelight@193: } truelight@0: } truelight@0: } truelight@0: rubidium@8190: /** rubidium@8190: * Add the given tile to the animated tile table (if it does not exist rubidium@8190: * on that table yet). Also increases the size of the table if necessary. rubidium@8190: * @param tile the tile to make animated rubidium@8190: */ rubidium@8190: void AddAnimatedTile(TileIndex tile) truelight@0: { rubidium@8190: MarkTileDirtyByTile(tile); truelight@0: rubidium@8190: for (const TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) { rubidium@8190: if (tile == *ti) return; truelight@193: } truelight@0: rubidium@8190: /* Table not large enough, so make it larger */ rubidium@8190: if (_animated_tile_count == _animated_tile_allocated) { rubidium@8190: _animated_tile_allocated *= 2; rubidium@8190: _animated_tile_list = ReallocT(_animated_tile_list, _animated_tile_allocated); rubidium@8190: } rubidium@8190: rubidium@8190: _animated_tile_list[_animated_tile_count] = tile; rubidium@8190: _animated_tile_count++; truelight@0: } truelight@0: rubidium@8190: /** rubidium@8190: * Animate all tiles in the animated tile list, i.e.\ call AnimateTile on them. rubidium@8190: */ rubidium@6573: void AnimateAnimatedTiles() truelight@0: { rubidium@8245: const TileIndex *ti = _animated_tile_list; rubidium@8245: while (ti < _animated_tile_list + _animated_tile_count) { rubidium@8245: const TileIndex curr = *ti; rubidium@8245: AnimateTile(curr); rubidium@8245: /* During the AnimateTile call, DeleteAnimatedTile could have been called, rubidium@8245: * deleting an element we've already processed and pushing the rest one rubidium@8245: * slot to the left. We can detect this by checking whether the index rubidium@8245: * in the current slot has changed - if it has, an element has been deleted, rubidium@8245: * and we should process the current slot again instead of going forward. rubidium@8245: * NOTE: this will still break if more than one animated tile is being rubidium@8245: * deleted during the same AnimateTile call, but no code seems to rubidium@8245: * be doing this anyway. rubidium@8245: */ rubidium@8245: if (*ti == curr) ++ti; truelight@0: } truelight@0: } truelight@0: rubidium@8190: /** rubidium@8190: * Initialize all animated tile variables to some known begin point rubidium@8190: */ rubidium@6573: void InitializeAnimatedTiles() truelight@0: { rubidium@8190: _animated_tile_list = ReallocT(_animated_tile_list, 256); rubidium@8190: _animated_tile_count = 0; rubidium@8190: _animated_tile_allocated = 256; truelight@0: } truelight@0: rubidium@8190: /** rubidium@8190: * Save the ANIT chunk. rubidium@8190: */ rubidium@8190: static void Save_ANIT() truelight@0: { rubidium@8190: SlSetLength(_animated_tile_count * sizeof(*_animated_tile_list)); rubidium@8190: SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32); truelight@0: } truelight@0: rubidium@8190: /** rubidium@8190: * Load the ANIT chunk; the chunk containing the animated tiles. rubidium@8190: */ rubidium@8190: static void Load_ANIT() rubidium@8190: { rubidium@8190: /* Before version 80 we did NOT have a variable length animated tile table */ rubidium@8190: if (CheckSavegameVersion(80)) { rubidium@8190: /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */ rubidium@8190: SlArray(_animated_tile_list, 256, CheckSavegameVersion(6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32); truelight@0: rubidium@8190: for (_animated_tile_count = 0; _animated_tile_count < 256; _animated_tile_count++) { rubidium@8190: if (_animated_tile_list[_animated_tile_count] == 0) break; rubidium@8190: } rubidium@8190: return; rubidium@8190: } rubidium@8190: rubidium@10751: _animated_tile_count = (uint)SlGetFieldLength() / sizeof(*_animated_tile_list); rubidium@8190: rubidium@8190: /* Determine a nice rounded size for the amount of allocated tiles */ rubidium@8190: _animated_tile_allocated = 256; rubidium@8190: while (_animated_tile_allocated < _animated_tile_count) _animated_tile_allocated *= 2; rubidium@8190: rubidium@8190: _animated_tile_list = ReallocT(_animated_tile_list, _animated_tile_allocated); rubidium@8190: SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32); rubidium@8190: } rubidium@8190: rubidium@8190: /** rubidium@8190: * "Definition" imported by the saveload code to be able to load and save rubidium@8190: * the animated tile table. rubidium@8190: */ rubidium@5838: extern const ChunkHandler _animated_tile_chunk_handlers[] = { rubidium@8190: { 'ANIT', Save_ANIT, Load_ANIT, CH_RIFF | CH_LAST}, truelight@0: };