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