(svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.

This commit is contained in:
rubidium 2008-09-02 15:20:38 +00:00
parent cbe397388f
commit c8e8911ef6
19 changed files with 61 additions and 51 deletions

View File

@ -220,7 +220,7 @@ void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
*/
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height)
{
const Sprite *spr = GetSprite(GetAircraftIcon(engine));
const Sprite *spr = GetSprite(GetAircraftIcon(engine), ST_NORMAL);
width = spr->width;
height = spr->height;

View File

@ -8,7 +8,7 @@
void Blitter_8bppBase::DrawColorMappingRect(void *dst, int width, int height, int pal)
{
const uint8 *ctab = GetNonSprite(pal) + 1;
const uint8 *ctab = GetNonSprite(pal, ST_RECOLOUR) + 1;
do {
for (int i = 0; i != width; i++) *((uint8 *)dst + i) = ctab[((uint8 *)dst)[i]];

View File

@ -281,7 +281,7 @@ struct DepotWindow : Window {
case VEH_ROAD: DrawRoadVehImage( v, x + 24, sprite_y, this->sel, 1); break;
case VEH_SHIP: DrawShipImage( v, x + 19, sprite_y - 1, this->sel); break;
case VEH_AIRCRAFT: {
const Sprite *spr = GetSprite(v->GetImage(DIR_W));
const Sprite *spr = GetSprite(v->GetImage(DIR_W), ST_NORMAL);
DrawAircraftImage(v, x + 12,
y + max(spr->height + spr->y_offs - 14, 0), // tall sprites needs an y offset
this->sel);

View File

@ -401,7 +401,7 @@ const Sprite *GetGlyph(FontSize size, WChar key)
if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
SpriteID sprite = GetUnicodeGlyph(size, key);
if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
return GetSprite(sprite);
return GetSprite(sprite, ST_FONT);
}
/* Check for the glyph in our cache */
@ -470,7 +470,7 @@ uint GetGlyphWidth(FontSize size, WChar key)
if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
SpriteID sprite = GetUnicodeGlyph(size, key);
if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
return SpriteExists(sprite) ? GetSprite(sprite)->width + (size != FS_NORMAL) : 0;
return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0;
}
glyph = GetGlyphPtr(size, key);

View File

@ -46,7 +46,7 @@ static inline const Sprite *GetGlyph(FontSize size, uint32 key)
{
SpriteID sprite = GetUnicodeGlyph(size, key);
if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
return GetSprite(sprite);
return GetSprite(sprite, ST_FONT);
}
@ -55,7 +55,7 @@ static inline uint GetGlyphWidth(FontSize size, uint32 key)
{
SpriteID sprite = GetUnicodeGlyph(size, key);
if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
return SpriteExists(sprite) ? GetSprite(sprite)->width + (size != FS_NORMAL) : 0;
return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0;
}
#endif /* WITH_FREETYPE */

View File

@ -888,13 +888,13 @@ int DoDrawStringTruncated(const char *str, int x, int y, uint16 color, uint maxw
void DrawSprite(SpriteID img, SpriteID pal, int x, int y, const SubSprite *sub)
{
if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
_color_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH)) + 1;
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH)), x, y, BM_TRANSPARENT, sub);
_color_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_TRANSPARENT, sub);
} else if (pal != PAL_NONE) {
_color_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH)) + 1;
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH)), x, y, BM_COLOUR_REMAP, sub);
_color_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1;
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_COLOUR_REMAP, sub);
} else {
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH)), x, y, BM_NORMAL, sub);
GfxMainBlitter(GetSprite(GB(img, 0, SPRITE_WIDTH), ST_NORMAL), x, y, BM_NORMAL, sub);
}
}
@ -1476,7 +1476,7 @@ static void SetCursorSprite(SpriteID cursor, SpriteID pal)
if (cv->sprite == cursor) return;
p = GetSprite(GB(cursor, 0, SPRITE_WIDTH));
p = GetSprite(GB(cursor, 0, SPRITE_WIDTH), ST_NORMAL);
cv->sprite = cursor;
cv->pal = pal;
cv->size.y = p->height;

View File

@ -243,4 +243,12 @@ enum PaletteType {
MAX_PAL = 2, ///< The number of palettes.
};
/** Types of sprites that might be loaded */
enum SpriteType {
ST_NORMAL = 0, ///< The most basic (normal) sprite
ST_MAPGEN = 1, ///< Special sprite for the map generator
ST_FONT = 2, ///< A sprite used for fonts
ST_RECOLOUR = 3, ///< Recolour sprite
};
#endif /* GFX_TYPE_H */

View File

@ -675,7 +675,7 @@ static void GenerateTerrain(int type, uint flag)
{
uint32 r = Random();
const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845);
const Sprite *templ = GetSprite((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845, ST_MAPGEN);
uint x = r & MapMaxX();
uint y = (r >> MapLogX()) & MapMaxY();

View File

@ -386,7 +386,7 @@ void ShowSelectGameWindow();
void SetupColorsAndInitialWindow()
{
for (uint i = 0; i != 16; i++) {
const byte *b = GetNonSprite(PALETTE_RECOLOR_START + i);
const byte *b = GetNonSprite(PALETTE_RECOLOR_START + i, ST_RECOLOUR);
assert(b);
memcpy(_colour_gradient[i], b + 0xC6, sizeof(_colour_gradient[i]));

View File

@ -84,7 +84,7 @@ void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
*/
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
{
const Sprite *spr = GetSprite(GetShipIcon(engine));
const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);
width = spr->width;
height = spr->height;

View File

@ -28,7 +28,7 @@ struct SpriteCache {
uint32 id;
uint16 file_slot;
int16 lru;
bool real_sprite; ///< In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as non-real sprite. If the non-real sprite gets into the cache it might be drawn as real sprite which causes enormous trouble.
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.
};
@ -133,7 +133,7 @@ bool SpriteExists(SpriteID id)
void* AllocSprite(size_t);
static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
static void* ReadSprite(SpriteCache *sc, SpriteID id, SpriteType sprite_type)
{
uint8 file_slot = sc->file_slot;
size_t file_pos = sc->file_pos;
@ -149,17 +149,17 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
file_pos = GetSpriteCache(SPR_IMG_QUERY)->file_pos;
}
if (real_sprite && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
if (sprite_type == ST_NORMAL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) {
#ifdef WITH_PNG
/* Try loading 32bpp graphics in case we are 32bpp output */
SpriteLoaderPNG sprite_loader;
SpriteLoader::Sprite sprite;
if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id)) {
if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id, sprite_type)) {
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
free(sprite.data);
sc->real_sprite = real_sprite;
sc->type = sprite_type;
return sc->ptr;
}
@ -180,18 +180,18 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
byte type = FioReadByte();
/* Type 0xFF indicates either a colormap or some other non-sprite info */
if (type == 0xFF) {
if (real_sprite) {
if (sprite_type != ST_RECOLOUR) {
static byte warning_level = 0;
DEBUG(sprite, warning_level, "Tried to load non sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
DEBUG(sprite, warning_level, "Tried to load recolour sprite #%d as a real sprite. Probable cause: NewGRF interference", id);
warning_level = 6;
if (id == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non- sprite?");
return (void*)GetRawSprite(SPR_IMG_QUERY, true);
if (id == SPR_IMG_QUERY) usererror("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a recolour-sprite?");
return (void*)GetRawSprite(SPR_IMG_QUERY, ST_NORMAL);
}
byte *dest = (byte *)AllocSprite(num);
sc->ptr = dest;
sc->real_sprite = real_sprite;
sc->type = sprite_type;
FioReadBlock(dest, num);
return sc->ptr;
@ -205,8 +205,8 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
* Ugly: yes. Other solution: no. Blame the original author or
* something ;) The image should really have been a data-stream
* (so type = 0xFF basicly). */
if (id >= 4845 && id <= 4881) {
assert(real_sprite);
assert((id >= 4845 && id <= 4881) == (sprite_type == ST_MAPGEN));
if (sprite_type == ST_MAPGEN) {
uint height = FioReadByte();
uint width = FioReadWord();
Sprite *sprite;
@ -234,24 +234,24 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
}
}
sc->real_sprite = real_sprite;
sc->type = sprite_type;
return sc->ptr;
}
if (!real_sprite) {
if (sprite_type == ST_RECOLOUR) {
static byte warning_level = 0;
DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a non sprite. Probable cause: NewGRF interference", id);
DEBUG(sprite, warning_level, "Tried to load real sprite #%d as a recolour sprite. Probable cause: NewGRF interference", id);
warning_level = 6;
return (void*)GetRawSprite(id, true);
return (void*)GetRawSprite(id, ST_NORMAL);
}
SpriteLoaderGrf sprite_loader;
SpriteLoader::Sprite sprite;
sc->real_sprite = real_sprite;
sc->type = sprite_type;
if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos)) return NULL;
if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos, sprite_type)) return NULL;
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
free(sprite.data);
@ -276,7 +276,7 @@ bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
sc->ptr = NULL;
sc->lru = 0;
sc->id = file_sprite_id;
sc->real_sprite = false;
sc->type = ST_NORMAL;
return true;
}
@ -291,7 +291,7 @@ void DupSprite(SpriteID old_spr, SpriteID new_spr)
scnew->file_pos = scold->file_pos;
scnew->ptr = NULL;
scnew->id = scold->id;
scnew->real_sprite = scold->real_sprite;
scnew->type = scold->type;
}
@ -461,7 +461,7 @@ void* AllocSprite(size_t mem_req)
}
const void *GetRawSprite(SpriteID sprite, bool real_sprite)
const void *GetRawSprite(SpriteID sprite, SpriteType type)
{
SpriteCache *sc;
void* p;
@ -476,7 +476,7 @@ const void *GetRawSprite(SpriteID sprite, bool real_sprite)
p = sc->ptr;
/* Load the sprite, if it is not loaded, yet */
if (p == NULL || sc->real_sprite != real_sprite) p = ReadSprite(sc, sprite, real_sprite);
if (p == NULL || sc->type != type) p = ReadSprite(sc, sprite, type);
return p;
}

View File

@ -17,17 +17,19 @@ struct Sprite {
extern uint _sprite_cache_size;
const void *GetRawSprite(SpriteID sprite, bool real_sprite);
const void *GetRawSprite(SpriteID sprite, SpriteType type);
bool SpriteExists(SpriteID sprite);
static inline const Sprite *GetSprite(SpriteID sprite)
static inline const Sprite *GetSprite(SpriteID sprite, SpriteType type)
{
return (Sprite*)GetRawSprite(sprite, true);
assert(type != ST_RECOLOUR);
return (Sprite*)GetRawSprite(sprite, type);
}
static inline const byte *GetNonSprite(SpriteID sprite)
static inline const byte *GetNonSprite(SpriteID sprite, SpriteType type)
{
return (byte*)GetRawSprite(sprite, false);
assert(type == ST_RECOLOUR);
return (byte*)GetRawSprite(sprite, type);
}
void GfxInitSpriteMem();

View File

@ -9,7 +9,7 @@
#include "../core/alloc_func.hpp"
#include "grf.hpp"
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos)
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
{
/* Open the right file and go to the correct position */
FioSeekToFile(file_slot, file_pos);

View File

@ -12,7 +12,7 @@ public:
/**
* Load a sprite from the disk and return a sprite struct which is the same for all loaders.
*/
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos);
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type);
};
#endif /* SPRITELOADER_GRF_HPP */

View File

@ -170,7 +170,7 @@ static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 i
return true;
}
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos)
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
{
const char *filename = FioGetFilename(file_slot);
if (!LoadPNG(sprite, filename, (uint32)file_pos, false)) return false;

View File

@ -12,7 +12,7 @@ public:
/**
* Load a sprite from the disk and return a sprite struct which is the same for all loaders.
*/
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos);
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type);
};
#endif /* SPRITELOADER_PNG_HPP */

View File

@ -26,7 +26,7 @@ public:
/**
* Load a sprite from the disk and return a sprite struct which is the same for all loaders.
*/
virtual bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos) = 0;
virtual bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type) = 0;
virtual ~SpriteLoader() { }
};

View File

@ -234,7 +234,7 @@ void VehiclePositionChanged(Vehicle *v)
{
int img = v->cur_image;
Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
const Sprite *spr = GetSprite(img);
const Sprite *spr = GetSprite(img, ST_NORMAL);
pt.x += spr->x_offs;
pt.y += spr->y_offs;

View File

@ -567,7 +567,7 @@ void OffsetGroundSprite(int x, int y)
static void AddCombinedSprite(SpriteID image, SpriteID pal, int x, int y, byte z, const SubSprite *sub)
{
Point pt = RemapCoords(x, y, z);
const Sprite* spr = GetSprite(image & SPRITE_MASK);
const Sprite* spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
@ -632,7 +632,7 @@ void AddSortableSpriteToDraw(SpriteID image, SpriteID pal, int x, int y, int w,
top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
} else {
const Sprite *spr = GetSprite(image & SPRITE_MASK);
const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
left = tmp_left = (pt.x += spr->x_offs);
right = (pt.x + spr->width );
top = tmp_top = (pt.y += spr->y_offs);