OpenTTD Source
12.0-beta2
|
#include "stdafx.h"
#include "random_access_file_type.h"
#include "spriteloader/grf.hpp"
#include "gfx_func.h"
#include "error.h"
#include "zoom_func.h"
#include "settings_type.h"
#include "blitter/factory.hpp"
#include "core/math_func.hpp"
#include "core/mem_func.hpp"
#include "video/video_driver.hpp"
#include "table/sprites.h"
#include "table/strings.h"
#include "table/palette_convert.h"
#include "safeguards.h"
Go to the source code of this file.
Data Structures | |
struct | SpriteCache |
struct | MemBlock |
Functions | |
static SpriteCache * | GetSpriteCache (uint index) |
static bool | IsMapgenSpriteID (SpriteID sprite) |
static SpriteCache * | AllocateSpriteCache (uint index) |
static SpriteFile * | GetCachedSpriteFileByName (const std::string &filename) |
Get the cached SpriteFile given the name of the file. More... | |
SpriteFile & | OpenCachedSpriteFile (const std::string &filename, Subdirectory subdir, bool palette_remap) |
Open/get the SpriteFile that is cached for use in the sprite cache. More... | |
static void | CompactSpriteCache () |
Called when holes in the sprite cache should be removed. More... | |
static void * | AllocSprite (size_t mem_req) |
bool | SkipSpriteData (SpriteFile &file, byte type, uint16 num) |
Skip the given amount of sprite graphics data. More... | |
bool | SpriteExists (SpriteID id) |
SpriteType | GetSpriteType (SpriteID sprite) |
Get the sprite type of a given sprite. More... | |
SpriteFile * | GetOriginFile (SpriteID sprite) |
Get the SpriteFile of a given sprite. More... | |
uint32 | GetSpriteLocalID (SpriteID sprite) |
Get the GRF-local sprite id of a given sprite. More... | |
uint | GetSpriteCountForFile (const std::string &filename, SpriteID begin, SpriteID end) |
Count the sprites which originate from a specific file in a range of SpriteIDs. More... | |
uint | GetMaxSpriteID () |
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no sprites with a higher SpriteID, although there might be up to roughly a thousand unused SpriteIDs below this number. More... | |
static bool | ResizeSpriteIn (SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt) |
static void | ResizeSpriteOut (SpriteLoader::Sprite *sprite, ZoomLevel zoom) |
static bool | PadSingleSprite (SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom) |
static bool | PadSprites (SpriteLoader::Sprite *sprite, uint8 sprite_avail, SpriteEncoder *encoder) |
static bool | ResizeSprites (SpriteLoader::Sprite *sprite, uint8 sprite_avail, SpriteEncoder *encoder) |
static void * | ReadRecolourSprite (SpriteFile &file, uint num) |
Load a recolour sprite into memory. More... | |
static void * | ReadSprite (const SpriteCache *sc, SpriteID id, SpriteType sprite_type, AllocatorProc *allocator, SpriteEncoder *encoder) |
Read a sprite from disk. More... | |
size_t | GetGRFSpriteOffset (uint32 id) |
Get the file offset for a specific sprite in the sprite section of a GRF. More... | |
void | ReadGRFSpriteOffsets (SpriteFile &file) |
Parse the sprite section of GRFs. More... | |
bool | LoadNextSprite (int load_index, SpriteFile &file, uint file_sprite_id) |
Load a real or recolour sprite. More... | |
void | DupSprite (SpriteID old_spr, SpriteID new_spr) |
static MemBlock * | NextBlock (MemBlock *block) |
static size_t | GetSpriteCacheUsage () |
void | IncreaseSpriteLRU () |
static void | DeleteEntryFromSpriteCache (uint item) |
Delete a single entry from the sprite cache. More... | |
static void | DeleteEntryFromSpriteCache () |
void * | SimpleSpriteAlloc (size_t size) |
Sprite allocator simply using malloc. | |
static void * | HandleInvalidSpriteRequest (SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator) |
Handles the case when a sprite of different type is requested than is present in the SpriteCache. More... | |
void * | GetRawSprite (SpriteID sprite, SpriteType type, AllocatorProc *allocator, SpriteEncoder *encoder) |
Reads a sprite (from disk or sprite cache). More... | |
static void | GfxInitSpriteCache () |
void | GfxInitSpriteMem () |
void | GfxClearSpriteCache () |
Remove all encoded sprites from the sprite cache without discarding sprite location information. | |
Variables | |
uint | _sprite_cache_size = 4 |
static uint | _spritecache_items = 0 |
static SpriteCache * | _spritecache = nullptr |
static std::vector< std::unique_ptr< SpriteFile > > | _sprite_files |
static uint | _sprite_lru_counter |
static MemBlock * | _spritecache_ptr |
static uint | _allocated_sprite_cache_size = 0 |
static int | _compact_cache_counter |
static std::map< uint32, size_t > | _grf_sprite_offsets |
Map from sprite numbers to position in the GRF file. | |
static const size_t | S_FREE_MASK = sizeof(size_t) - 1 |
S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero, the block is free. More... | |
Caching of sprites.
Definition in file spritecache.cpp.
|
static |
Called when holes in the sprite cache should be removed.
That is accomplished by moving the cached data.
Definition at line 721 of file spritecache.cpp.
References Debug.
|
static |
Delete a single entry from the sprite cache.
item | Entry to delete. |
Definition at line 765 of file spritecache.cpp.
|
static |
Get the cached SpriteFile given the name of the file.
filename | The name of the file at the disk. |
null
. Definition at line 79 of file spritecache.cpp.
Referenced by GetSpriteCountForFile(), and OpenCachedSpriteFile().
size_t GetGRFSpriteOffset | ( | uint32 | id | ) |
Get the file offset for a specific sprite in the sprite section of a GRF.
id | ID of the sprite to look up. |
Definition at line 541 of file spritecache.cpp.
References _grf_sprite_offsets.
Referenced by LoadNextSprite().
uint GetMaxSpriteID | ( | ) |
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no sprites with a higher SpriteID, although there might be up to roughly a thousand unused SpriteIDs below this number.
Definition at line 220 of file spritecache.cpp.
Referenced by SpriteAlignerWindow::OnClick(), SpriteAlignerWindow::OnQueryTextFinished(), and UpdateCursorSize().
SpriteFile* GetOriginFile | ( | SpriteID | sprite | ) |
Get the SpriteFile of a given sprite.
sprite | The sprite to look at. |
Definition at line 173 of file spritecache.cpp.
void* GetRawSprite | ( | SpriteID | sprite, |
SpriteType | type, | ||
AllocatorProc * | allocator, | ||
SpriteEncoder * | encoder | ||
) |
Reads a sprite (from disk or sprite cache).
If the sprite is not available or of wrong type, a fallback sprite is returned.
sprite | Sprite to read. |
type | Expected sprite type. |
allocator | Allocator function to use. Set to nullptr to use the usual sprite cache. |
encoder | Sprite encoder to use. Set to nullptr to use the currently active blitter. |
Definition at line 905 of file spritecache.cpp.
Count the sprites which originate from a specific file in a range of SpriteIDs.
file | The loaded SpriteFile. |
begin | First sprite in range. |
end | First sprite not in range. |
Definition at line 197 of file spritecache.cpp.
References GetCachedSpriteFileByName().
uint32 GetSpriteLocalID | ( | SpriteID | sprite | ) |
Get the GRF-local sprite id of a given sprite.
sprite | The sprite to look at. |
Definition at line 184 of file spritecache.cpp.
SpriteType GetSpriteType | ( | SpriteID | sprite | ) |
Get the sprite type of a given sprite.
sprite | The sprite to look at. |
Definition at line 162 of file spritecache.cpp.
Referenced by SpriteAlignerWindow::OnClick(), and SpriteAlignerWindow::OnQueryTextFinished().
|
static |
Handles the case when a sprite of different type is requested than is present in the SpriteCache.
For ST_FONT sprites, it is normal. In other cases, default sprite is loaded instead.
sprite | ID of loaded sprite |
requested | requested sprite type |
sc | the currently known sprite cache for the requested sprite |
Definition at line 860 of file spritecache.cpp.
bool LoadNextSprite | ( | int | load_index, |
SpriteFile & | file, | ||
uint | file_sprite_id | ||
) |
Load a real or recolour sprite.
load_index | Global sprite index. |
file | GRF to load from. |
file_sprite_id | Sprite number in the GRF. |
container_version | Container version of the GRF. |
Definition at line 583 of file spritecache.cpp.
References SpriteFile::GetContainerVersion(), GetGRFSpriteOffset(), RandomAccessFile::GetPos(), MAX_SPRITES, RandomAccessFile::ReadByte(), RandomAccessFile::ReadDword(), ReadRecolourSprite(), RandomAccessFile::ReadWord(), RandomAccessFile::SkipBytes(), SkipSpriteData(), ST_INVALID, ST_NORMAL, ST_RECOLOUR, and usererror().
Referenced by LoadGrfFile().
SpriteFile& OpenCachedSpriteFile | ( | const std::string & | filename, |
Subdirectory | subdir, | ||
bool | palette_remap | ||
) |
Open/get the SpriteFile that is cached for use in the sprite cache.
filename | Name of the file at the disk. |
subdir | The sub directory to search this file in. |
palette_remap | Whether a palette remap needs to be performed for this file. |
Definition at line 95 of file spritecache.cpp.
References GetCachedSpriteFileByName().
Referenced by LoadGrfFile(), and LoadGrfFileIndexed().
void ReadGRFSpriteOffsets | ( | SpriteFile & | file | ) |
Parse the sprite section of GRFs.
container_version | Container version of the GRF we're currently processing. |
Definition at line 550 of file spritecache.cpp.
References _grf_sprite_offsets, SpriteFile::GetContainerVersion(), RandomAccessFile::GetPos(), RandomAccessFile::ReadDword(), RandomAccessFile::SeekTo(), and RandomAccessFile::SkipBytes().
Referenced by LoadGrfFile(), and LoadGrfFileIndexed().
|
static |
Load a recolour sprite into memory.
file | GRF we're reading from. |
num | Size of the sprite in the GRF. |
Definition at line 417 of file spritecache.cpp.
Referenced by LoadNextSprite().
|
static |
Read a sprite from disk.
sc | Location of sprite. |
id | Sprite number. |
sprite_type | Type of sprite. |
allocator | Allocator function to use. |
encoder | Sprite encoder to use. |
Definition at line 453 of file spritecache.cpp.
bool SkipSpriteData | ( | SpriteFile & | file, |
byte | type, | ||
uint16 | num | ||
) |
Skip the given amount of sprite graphics data.
type | the type of sprite (compressed etc) |
num | the amount of sprites to skip |
Definition at line 125 of file spritecache.cpp.
References RandomAccessFile::ReadByte(), and RandomAccessFile::SkipBytes().
Referenced by LoadNextSprite().
|
static |
S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero, the block is free.
S_FREE_MASK has to ensure MemBlock is correctly aligned - it means 8B (S_FREE_MASK == 7) on 64bit systems!
Definition at line 664 of file spritecache.cpp.