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