Update to 14.0-beta1
This commit is contained in:
@@ -12,12 +12,15 @@
|
||||
#include "spriteloader/grf.hpp"
|
||||
#include "gfx_func.h"
|
||||
#include "error.h"
|
||||
#include "error_func.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 "spritecache.h"
|
||||
#include "spritecache_internal.h"
|
||||
|
||||
#include "table/sprites.h"
|
||||
#include "table/strings.h"
|
||||
@@ -28,17 +31,6 @@
|
||||
/* Default of 4MB spritecache */
|
||||
uint _sprite_cache_size = 4;
|
||||
|
||||
struct SpriteCache {
|
||||
void *ptr;
|
||||
size_t file_pos;
|
||||
SpriteFile *file; ///< The file the sprite in this entry can be found in.
|
||||
uint32 id;
|
||||
int16 lru;
|
||||
SpriteType type; ///< In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour sprite. If the recolour sprite gets into the cache it might be drawn as real sprite which causes enormous trouble.
|
||||
bool warned; ///< True iff the user has been warned about incorrect use of this sprite
|
||||
byte control_flags; ///< Control flags, see SpriteCacheCtrlFlags
|
||||
};
|
||||
|
||||
|
||||
static uint _spritecache_items = 0;
|
||||
static SpriteCache *_spritecache = nullptr;
|
||||
@@ -49,12 +41,7 @@ static inline SpriteCache *GetSpriteCache(uint index)
|
||||
return &_spritecache[index];
|
||||
}
|
||||
|
||||
static inline bool IsMapgenSpriteID(SpriteID sprite)
|
||||
{
|
||||
return IsInsideMM(sprite, 4845, 4882);
|
||||
}
|
||||
|
||||
static SpriteCache *AllocateSpriteCache(uint index)
|
||||
SpriteCache *AllocateSpriteCache(uint index)
|
||||
{
|
||||
if (index >= _spritecache_items) {
|
||||
/* Add another 1024 items to the 'pool' */
|
||||
@@ -77,7 +64,8 @@ static SpriteCache *AllocateSpriteCache(uint index)
|
||||
* @param filename The name of the file at the disk.
|
||||
* @return The SpriteFile or \c null.
|
||||
*/
|
||||
static SpriteFile *GetCachedSpriteFileByName(const std::string &filename) {
|
||||
static SpriteFile *GetCachedSpriteFileByName(const std::string &filename)
|
||||
{
|
||||
for (auto &f : _sprite_files) {
|
||||
if (f->GetFilename() == filename) {
|
||||
return f.get();
|
||||
@@ -97,7 +85,7 @@ SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdi
|
||||
{
|
||||
SpriteFile *file = GetCachedSpriteFileByName(filename);
|
||||
if (file == nullptr) {
|
||||
file = _sprite_files.emplace_back(new SpriteFile(filename, subdir, palette_remap)).get();
|
||||
file = _sprite_files.insert(std::end(_sprite_files), std::make_unique<SpriteFile>(filename, subdir, palette_remap))->get();
|
||||
} else {
|
||||
file->SeekToBegin();
|
||||
}
|
||||
@@ -115,7 +103,6 @@ static uint _allocated_sprite_cache_size = 0;
|
||||
static int _compact_cache_counter;
|
||||
|
||||
static void CompactSpriteCache();
|
||||
static void *AllocSprite(size_t mem_req);
|
||||
|
||||
/**
|
||||
* Skip the given amount of sprite graphics data.
|
||||
@@ -123,13 +110,13 @@ static void *AllocSprite(size_t mem_req);
|
||||
* @param num the amount of sprites to skip
|
||||
* @return true if the data could be correctly skipped.
|
||||
*/
|
||||
bool SkipSpriteData(SpriteFile &file, byte type, uint16 num)
|
||||
bool SkipSpriteData(SpriteFile &file, byte type, uint16_t num)
|
||||
{
|
||||
if (type & 2) {
|
||||
file.SkipBytes(num);
|
||||
} else {
|
||||
while (num > 0) {
|
||||
int8 i = file.ReadByte();
|
||||
int8_t i = file.ReadByte();
|
||||
if (i >= 0) {
|
||||
int size = (i == 0) ? 0x80 : i;
|
||||
if (size > num) return false;
|
||||
@@ -162,7 +149,7 @@ bool SpriteExists(SpriteID id)
|
||||
*/
|
||||
SpriteType GetSpriteType(SpriteID sprite)
|
||||
{
|
||||
if (!SpriteExists(sprite)) return ST_INVALID;
|
||||
if (!SpriteExists(sprite)) return SpriteType::Invalid;
|
||||
return GetSpriteCache(sprite)->type;
|
||||
}
|
||||
|
||||
@@ -182,7 +169,7 @@ SpriteFile *GetOriginFile(SpriteID sprite)
|
||||
* @param sprite The sprite to look at.
|
||||
* @return The GRF-local sprite id.
|
||||
*/
|
||||
uint32 GetSpriteLocalID(SpriteID sprite)
|
||||
uint32_t GetSpriteLocalID(SpriteID sprite)
|
||||
{
|
||||
if (!SpriteExists(sprite)) return 0;
|
||||
return GetSpriteCache(sprite)->id;
|
||||
@@ -204,7 +191,10 @@ uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID
|
||||
for (SpriteID i = begin; i != end; i++) {
|
||||
if (SpriteExists(i)) {
|
||||
SpriteCache *sc = GetSpriteCache(i);
|
||||
if (sc->file == file) count++;
|
||||
if (sc->file == file) {
|
||||
count++;
|
||||
Debug(sprite, 4, "Sprite: {}", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
@@ -223,9 +213,9 @@ uint GetMaxSpriteID()
|
||||
return _spritecache_items;
|
||||
}
|
||||
|
||||
static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLevel tgt)
|
||||
static bool ResizeSpriteIn(SpriteLoader::SpriteCollection &sprite, ZoomLevel src, ZoomLevel tgt)
|
||||
{
|
||||
uint8 scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
|
||||
uint8_t scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
|
||||
|
||||
/* Check for possible memory overflow. */
|
||||
if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
|
||||
@@ -236,7 +226,7 @@ static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLeve
|
||||
sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
|
||||
sprite[tgt].colours = sprite[src].colours;
|
||||
|
||||
sprite[tgt].AllocateData(tgt, sprite[tgt].width * sprite[tgt].height);
|
||||
sprite[tgt].AllocateData(tgt, static_cast<size_t>(sprite[tgt].width) * sprite[tgt].height);
|
||||
|
||||
SpriteLoader::CommonPixel *dst = sprite[tgt].data;
|
||||
for (int y = 0; y < sprite[tgt].height; y++) {
|
||||
@@ -250,7 +240,7 @@ static bool ResizeSpriteIn(SpriteLoader::Sprite *sprite, ZoomLevel src, ZoomLeve
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
|
||||
static void ResizeSpriteOut(SpriteLoader::SpriteCollection &sprite, ZoomLevel zoom)
|
||||
{
|
||||
/* Algorithm based on 32bpp_Optimized::ResizeSprite() */
|
||||
sprite[zoom].width = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].width, zoom);
|
||||
@@ -259,7 +249,7 @@ static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom)
|
||||
sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_NORMAL].y_offs, zoom);
|
||||
sprite[zoom].colours = sprite[ZOOM_LVL_NORMAL].colours;
|
||||
|
||||
sprite[zoom].AllocateData(zoom, sprite[zoom].height * sprite[zoom].width);
|
||||
sprite[zoom].AllocateData(zoom, static_cast<size_t>(sprite[zoom].height) * sprite[zoom].width);
|
||||
|
||||
SpriteLoader::CommonPixel *dst = sprite[zoom].data;
|
||||
const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
|
||||
@@ -290,9 +280,10 @@ static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint p
|
||||
if (width > UINT16_MAX || height > UINT16_MAX) return false;
|
||||
|
||||
/* Copy source data and reallocate sprite memory. */
|
||||
SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
|
||||
MemCpyT(src_data, sprite->data, sprite->width * sprite->height);
|
||||
sprite->AllocateData(zoom, width * height);
|
||||
size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
|
||||
SpriteLoader::CommonPixel *src_data = MallocT<SpriteLoader::CommonPixel>(sprite_size);
|
||||
MemCpyT(src_data, sprite->data, sprite_size);
|
||||
sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
|
||||
|
||||
/* Copy with padding to destination. */
|
||||
SpriteLoader::CommonPixel *src = src_data;
|
||||
@@ -332,7 +323,7 @@ static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint p
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, SpriteEncoder *encoder)
|
||||
static bool PadSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
|
||||
{
|
||||
/* Get minimum top left corner coordinates. */
|
||||
int min_xoffs = INT32_MAX;
|
||||
@@ -380,10 +371,10 @@ static bool PadSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, SpriteE
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, SpriteEncoder *encoder)
|
||||
static bool ResizeSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
|
||||
{
|
||||
/* Create a fully zoomed image if it does not exist */
|
||||
ZoomLevel first_avail = static_cast<ZoomLevel>(FIND_FIRST_BIT(sprite_avail));
|
||||
ZoomLevel first_avail = static_cast<ZoomLevel>(FindFirstBit(sprite_avail));
|
||||
if (first_avail != ZOOM_LVL_NORMAL) {
|
||||
if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_NORMAL)) return false;
|
||||
SetBit(sprite_avail, ZOOM_LVL_NORMAL);
|
||||
@@ -406,6 +397,12 @@ static bool ResizeSprites(SpriteLoader::Sprite *sprite, uint8 sprite_avail, Spri
|
||||
if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
|
||||
}
|
||||
|
||||
/* Upscale to desired sprite_min_zoom if provided sprite only had zoomed in versions. */
|
||||
if (first_avail < _settings_client.gui.sprite_zoom_min) {
|
||||
if (_settings_client.gui.sprite_zoom_min >= ZOOM_LVL_OUT_4X) ResizeSpriteIn(sprite, ZOOM_LVL_OUT_4X, ZOOM_LVL_OUT_2X);
|
||||
if (_settings_client.gui.sprite_zoom_min >= ZOOM_LVL_OUT_2X) ResizeSpriteIn(sprite, ZOOM_LVL_OUT_2X, ZOOM_LVL_NORMAL);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -425,7 +422,7 @@ static void *ReadRecolourSprite(SpriteFile &file, uint num)
|
||||
byte *dest = (byte *)AllocSprite(std::max(RECOLOUR_SPRITE_SIZE, num));
|
||||
|
||||
if (file.NeedsPaletteRemap()) {
|
||||
byte *dest_tmp = AllocaM(byte, std::max(RECOLOUR_SPRITE_SIZE, num));
|
||||
byte *dest_tmp = new byte[std::max(RECOLOUR_SPRITE_SIZE, num)];
|
||||
|
||||
/* Only a few recolour sprites are less than 257 bytes */
|
||||
if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
|
||||
@@ -435,6 +432,7 @@ static void *ReadRecolourSprite(SpriteFile &file, uint num)
|
||||
for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
|
||||
dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
|
||||
}
|
||||
delete[] dest_tmp;
|
||||
} else {
|
||||
file.ReadBlock(dest, num);
|
||||
}
|
||||
@@ -459,18 +457,18 @@ static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_ty
|
||||
SpriteFile &file = *sc->file;
|
||||
size_t file_pos = sc->file_pos;
|
||||
|
||||
assert(sprite_type != ST_RECOLOUR);
|
||||
assert(IsMapgenSpriteID(id) == (sprite_type == ST_MAPGEN));
|
||||
assert(sprite_type != SpriteType::Recolour);
|
||||
assert(IsMapgenSpriteID(id) == (sprite_type == SpriteType::MapGen));
|
||||
assert(sc->type == sprite_type);
|
||||
|
||||
Debug(sprite, 9, "Load sprite {}", id);
|
||||
|
||||
SpriteLoader::Sprite sprite[ZOOM_LVL_COUNT];
|
||||
uint8 sprite_avail = 0;
|
||||
SpriteLoader::SpriteCollection sprite;
|
||||
uint8_t sprite_avail = 0;
|
||||
sprite[ZOOM_LVL_NORMAL].type = sprite_type;
|
||||
|
||||
SpriteLoaderGrf sprite_loader(file.GetContainerVersion());
|
||||
if (sprite_type != ST_MAPGEN && encoder->Is32BppSupported()) {
|
||||
if (sprite_type != SpriteType::MapGen && encoder->Is32BppSupported()) {
|
||||
/* Try for 32bpp sprites first. */
|
||||
sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags);
|
||||
}
|
||||
@@ -479,12 +477,12 @@ static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_ty
|
||||
}
|
||||
|
||||
if (sprite_avail == 0) {
|
||||
if (sprite_type == ST_MAPGEN) return nullptr;
|
||||
if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
|
||||
return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator, encoder);
|
||||
if (sprite_type == SpriteType::MapGen) return nullptr;
|
||||
if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
|
||||
return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator, encoder);
|
||||
}
|
||||
|
||||
if (sprite_type == ST_MAPGEN) {
|
||||
if (sprite_type == SpriteType::MapGen) {
|
||||
/* Ugly hack to work around the problem that the old landscape
|
||||
* generator assumes that those sprites are stored uncompressed in
|
||||
* the memory, and they are only read directly by the code, never
|
||||
@@ -513,18 +511,18 @@ static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_ty
|
||||
}
|
||||
|
||||
if (!ResizeSprites(sprite, sprite_avail, encoder)) {
|
||||
if (id == SPR_IMG_QUERY) usererror("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
|
||||
return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator, encoder);
|
||||
if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
|
||||
return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator, encoder);
|
||||
}
|
||||
|
||||
if (sprite->type == ST_FONT && ZOOM_LVL_GUI != ZOOM_LVL_NORMAL) {
|
||||
if (sprite[ZOOM_LVL_NORMAL].type == SpriteType::Font && _font_zoom != ZOOM_LVL_NORMAL) {
|
||||
/* Make ZOOM_LVL_NORMAL be ZOOM_LVL_GUI */
|
||||
sprite[ZOOM_LVL_NORMAL].width = sprite[ZOOM_LVL_GUI].width;
|
||||
sprite[ZOOM_LVL_NORMAL].height = sprite[ZOOM_LVL_GUI].height;
|
||||
sprite[ZOOM_LVL_NORMAL].x_offs = sprite[ZOOM_LVL_GUI].x_offs;
|
||||
sprite[ZOOM_LVL_NORMAL].y_offs = sprite[ZOOM_LVL_GUI].y_offs;
|
||||
sprite[ZOOM_LVL_NORMAL].data = sprite[ZOOM_LVL_GUI].data;
|
||||
sprite[ZOOM_LVL_NORMAL].colours = sprite[ZOOM_LVL_GUI].colours;
|
||||
sprite[ZOOM_LVL_NORMAL].width = sprite[_font_zoom].width;
|
||||
sprite[ZOOM_LVL_NORMAL].height = sprite[_font_zoom].height;
|
||||
sprite[ZOOM_LVL_NORMAL].x_offs = sprite[_font_zoom].x_offs;
|
||||
sprite[ZOOM_LVL_NORMAL].y_offs = sprite[_font_zoom].y_offs;
|
||||
sprite[ZOOM_LVL_NORMAL].data = sprite[_font_zoom].data;
|
||||
sprite[ZOOM_LVL_NORMAL].colours = sprite[_font_zoom].colours;
|
||||
}
|
||||
|
||||
return encoder->Encode(sprite, allocator);
|
||||
@@ -536,14 +534,14 @@ struct GrfSpriteOffset {
|
||||
};
|
||||
|
||||
/** Map from sprite numbers to position in the GRF file. */
|
||||
static std::map<uint32, GrfSpriteOffset> _grf_sprite_offsets;
|
||||
static std::map<uint32_t, GrfSpriteOffset> _grf_sprite_offsets;
|
||||
|
||||
/**
|
||||
* Get the file offset for a specific sprite in the sprite section of a GRF.
|
||||
* @param id ID of the sprite to look up.
|
||||
* @return Position of the sprite in the sprite section or SIZE_MAX if no such sprite is present.
|
||||
*/
|
||||
size_t GetGRFSpriteOffset(uint32 id)
|
||||
size_t GetGRFSpriteOffset(uint32_t id)
|
||||
{
|
||||
return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id].file_pos : SIZE_MAX;
|
||||
}
|
||||
@@ -566,7 +564,7 @@ void ReadGRFSpriteOffsets(SpriteFile &file)
|
||||
|
||||
/* Loop over all sprite section entries and store the file
|
||||
* offset for each newly encountered ID. */
|
||||
uint32 id, prev_id = 0;
|
||||
uint32_t id, prev_id = 0;
|
||||
while ((id = file.ReadDword()) != 0) {
|
||||
if (id != prev_id) {
|
||||
_grf_sprite_offsets[prev_id] = offset;
|
||||
@@ -613,7 +611,7 @@ bool LoadNextSprite(int load_index, SpriteFile &file, uint file_sprite_id)
|
||||
size_t file_pos = file.GetPos();
|
||||
|
||||
/* Read sprite header. */
|
||||
uint32 num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord();
|
||||
uint32_t num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord();
|
||||
if (num == 0) return false;
|
||||
byte grf_type = file.ReadByte();
|
||||
|
||||
@@ -627,7 +625,7 @@ bool LoadNextSprite(int load_index, SpriteFile &file, uint file_sprite_id)
|
||||
file.ReadByte();
|
||||
return false;
|
||||
}
|
||||
type = ST_RECOLOUR;
|
||||
type = SpriteType::Recolour;
|
||||
data = ReadRecolourSprite(file, num);
|
||||
} else if (file.GetContainerVersion() >= 2 && grf_type == 0xFD) {
|
||||
if (num != 4) {
|
||||
@@ -643,25 +641,25 @@ bool LoadNextSprite(int load_index, SpriteFile &file, uint file_sprite_id)
|
||||
} else {
|
||||
file_pos = SIZE_MAX;
|
||||
}
|
||||
type = ST_NORMAL;
|
||||
type = SpriteType::Normal;
|
||||
} else {
|
||||
file.SkipBytes(7);
|
||||
type = SkipSpriteData(file, grf_type, num - 8) ? ST_NORMAL : ST_INVALID;
|
||||
type = SkipSpriteData(file, grf_type, num - 8) ? SpriteType::Normal : SpriteType::Invalid;
|
||||
/* Inline sprites are not supported for container version >= 2. */
|
||||
if (file.GetContainerVersion() >= 2) return false;
|
||||
}
|
||||
|
||||
if (type == ST_INVALID) return false;
|
||||
if (type == SpriteType::Invalid) return false;
|
||||
|
||||
if (load_index >= MAX_SPRITES) {
|
||||
usererror("Tried to load too many sprites (#%d; max %d)", load_index, MAX_SPRITES);
|
||||
UserError("Tried to load too many sprites (#{}; max {})", load_index, MAX_SPRITES);
|
||||
}
|
||||
|
||||
bool is_mapgen = IsMapgenSpriteID(load_index);
|
||||
|
||||
if (is_mapgen) {
|
||||
if (type != ST_NORMAL) usererror("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
|
||||
type = ST_MAPGEN;
|
||||
if (type != SpriteType::Normal) UserError("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
|
||||
type = SpriteType::MapGen;
|
||||
}
|
||||
|
||||
SpriteCache *sc = AllocateSpriteCache(load_index);
|
||||
@@ -728,7 +726,7 @@ void IncreaseSpriteLRU()
|
||||
if (_sprite_lru_counter > 16384) {
|
||||
SpriteID i;
|
||||
|
||||
Debug(sprite, 3, "Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
|
||||
Debug(sprite, 5, "Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
|
||||
|
||||
for (i = 0; i != _spritecache_items; i++) {
|
||||
SpriteCache *sc = GetSpriteCache(i);
|
||||
@@ -826,7 +824,7 @@ static void DeleteEntryFromSpriteCache()
|
||||
cur_lru = 0xffff;
|
||||
for (SpriteID i = 0; i != _spritecache_items; i++) {
|
||||
SpriteCache *sc = GetSpriteCache(i);
|
||||
if (sc->type != ST_RECOLOUR && sc->ptr != nullptr && sc->lru < cur_lru) {
|
||||
if (sc->type != SpriteType::Recolour && sc->ptr != nullptr && sc->lru < cur_lru) {
|
||||
cur_lru = sc->lru;
|
||||
best = i;
|
||||
}
|
||||
@@ -834,12 +832,12 @@ static void DeleteEntryFromSpriteCache()
|
||||
|
||||
/* Display an error message and die, in case we found no sprite at all.
|
||||
* This shouldn't really happen, unless all sprites are locked. */
|
||||
if (best == UINT_MAX) error("Out of sprite memory");
|
||||
if (best == UINT_MAX) FatalError("Out of sprite memory");
|
||||
|
||||
DeleteEntryFromSpriteCache(best);
|
||||
}
|
||||
|
||||
static void *AllocSprite(size_t mem_req)
|
||||
void *AllocSprite(size_t mem_req)
|
||||
{
|
||||
mem_req += sizeof(MemBlock);
|
||||
|
||||
@@ -886,43 +884,43 @@ void *SimpleSpriteAlloc(size_t size)
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* For SpriteType::Font sprites, it is normal. In other cases, default sprite is loaded instead.
|
||||
* @param sprite ID of loaded sprite
|
||||
* @param requested requested sprite type
|
||||
* @param sc the currently known sprite cache for the requested sprite
|
||||
* @return fallback sprite
|
||||
* @note this function will do usererror() in the case the fallback sprite isn't available
|
||||
* @note this function will do UserError() in the case the fallback sprite isn't available
|
||||
*/
|
||||
static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
|
||||
{
|
||||
static const char * const sprite_types[] = {
|
||||
"normal", // ST_NORMAL
|
||||
"map generator", // ST_MAPGEN
|
||||
"character", // ST_FONT
|
||||
"recolour", // ST_RECOLOUR
|
||||
"normal", // SpriteType::Normal
|
||||
"map generator", // SpriteType::MapGen
|
||||
"character", // SpriteType::Font
|
||||
"recolour", // SpriteType::Recolour
|
||||
};
|
||||
|
||||
SpriteType available = sc->type;
|
||||
if (requested == ST_FONT && available == ST_NORMAL) {
|
||||
if (sc->ptr == nullptr) sc->type = ST_FONT;
|
||||
if (requested == SpriteType::Font && available == SpriteType::Normal) {
|
||||
if (sc->ptr == nullptr) sc->type = SpriteType::Font;
|
||||
return GetRawSprite(sprite, sc->type, allocator);
|
||||
}
|
||||
|
||||
byte warning_level = sc->warned ? 6 : 0;
|
||||
sc->warned = true;
|
||||
Debug(sprite, warning_level, "Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[available], sprite, sprite_types[requested]);
|
||||
Debug(sprite, warning_level, "Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[static_cast<byte>(available)], sprite, sprite_types[static_cast<byte>(requested)]);
|
||||
|
||||
switch (requested) {
|
||||
case ST_NORMAL:
|
||||
if (sprite == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?");
|
||||
FALLTHROUGH;
|
||||
case ST_FONT:
|
||||
return GetRawSprite(SPR_IMG_QUERY, ST_NORMAL, allocator);
|
||||
case ST_RECOLOUR:
|
||||
if (sprite == PALETTE_TO_DARK_BLUE) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?");
|
||||
return GetRawSprite(PALETTE_TO_DARK_BLUE, ST_RECOLOUR, allocator);
|
||||
case ST_MAPGEN:
|
||||
/* this shouldn't happen, overriding of ST_MAPGEN sprites is checked in LoadNextSprite()
|
||||
case SpriteType::Normal:
|
||||
if (sprite == SPR_IMG_QUERY) UserError("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?");
|
||||
[[fallthrough]];
|
||||
case SpriteType::Font:
|
||||
return GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator);
|
||||
case SpriteType::Recolour:
|
||||
if (sprite == PALETTE_TO_DARK_BLUE) UserError("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?");
|
||||
return GetRawSprite(PALETTE_TO_DARK_BLUE, SpriteType::Recolour, allocator);
|
||||
case SpriteType::MapGen:
|
||||
/* this shouldn't happen, overriding of SpriteType::MapGen sprites is checked in LoadNextSprite()
|
||||
* (the only case the check fails is when these sprites weren't even loaded...) */
|
||||
default:
|
||||
NOT_REACHED();
|
||||
@@ -940,8 +938,8 @@ static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, S
|
||||
*/
|
||||
void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator, SpriteEncoder *encoder)
|
||||
{
|
||||
assert(type != ST_MAPGEN || IsMapgenSpriteID(sprite));
|
||||
assert(type < ST_INVALID);
|
||||
assert(type != SpriteType::MapGen || IsMapgenSpriteID(sprite));
|
||||
assert(type < SpriteType::Invalid);
|
||||
|
||||
if (!SpriteExists(sprite)) {
|
||||
Debug(sprite, 1, "Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
|
||||
@@ -987,19 +985,15 @@ static void GfxInitSpriteCache()
|
||||
_allocated_sprite_cache_size = target_size;
|
||||
|
||||
do {
|
||||
try {
|
||||
/* Try to allocate 50% more to make sure we do not allocate almost all available. */
|
||||
_spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
|
||||
} catch (std::bad_alloc &) {
|
||||
_spritecache_ptr = nullptr;
|
||||
}
|
||||
/* Try to allocate 50% more to make sure we do not allocate almost all available. */
|
||||
_spritecache_ptr = reinterpret_cast<MemBlock *>(new(std::nothrow) byte[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
|
||||
|
||||
if (_spritecache_ptr != nullptr) {
|
||||
/* Allocation succeeded, but we wanted less. */
|
||||
delete[] reinterpret_cast<byte *>(_spritecache_ptr);
|
||||
_spritecache_ptr = reinterpret_cast<MemBlock *>(new byte[_allocated_sprite_cache_size]);
|
||||
} else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
|
||||
usererror("Cannot allocate spritecache");
|
||||
UserError("Cannot allocate spritecache");
|
||||
} else {
|
||||
/* Try again to allocate half. */
|
||||
_allocated_sprite_cache_size >>= 1;
|
||||
@@ -1044,10 +1038,23 @@ void GfxClearSpriteCache()
|
||||
/* Clear sprite ptr for all cached items */
|
||||
for (uint i = 0; i != _spritecache_items; i++) {
|
||||
SpriteCache *sc = GetSpriteCache(i);
|
||||
if (sc->type != ST_RECOLOUR && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
|
||||
if (sc->type != SpriteType::Recolour && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
|
||||
}
|
||||
|
||||
VideoDriver::GetInstance()->ClearSystemSprites();
|
||||
}
|
||||
|
||||
/* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer[ZOOM_LVL_COUNT];
|
||||
/**
|
||||
* Remove all encoded font sprites from the sprite cache without
|
||||
* discarding sprite location information.
|
||||
*/
|
||||
void GfxClearFontSpriteCache()
|
||||
{
|
||||
/* Clear sprite ptr for all cached font items */
|
||||
for (uint i = 0; i != _spritecache_items; i++) {
|
||||
SpriteCache *sc = GetSpriteCache(i);
|
||||
if (sc->type == SpriteType::Font && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* static */ ReusableBuffer<SpriteLoader::CommonPixel> SpriteLoader::Sprite::buffer[ZOOM_LVL_END];
|
||||
|
||||
Reference in New Issue
Block a user