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