(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.

This commit is contained in:
peter1138 2008-01-22 07:27:06 +00:00
parent 9444eb4484
commit ffec79bbcf
8 changed files with 24 additions and 27 deletions

View File

@ -33,6 +33,7 @@ struct Fio {
FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use
#if defined(LIMITED_FDS)
uint open_handles; ///< current amount of open handles
uint usage_count[MAX_FILE_SLOTS]; ///< count how many times this file has been opened
@ -47,9 +48,9 @@ uint32 FioGetPos()
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
}
const char *FioGetFilename()
const char *FioGetFilename(uint8 slot)
{
return _fio.filename;
return _fio.shortnames[slot];
}
void FioSeekTo(uint32 pos, int mode)
@ -131,6 +132,10 @@ static inline void FioCloseFile(int slot)
{
if (_fio.handles[slot] != NULL) {
fclose(_fio.handles[slot]);
free(_fio.shortnames[slot]);
_fio.shortnames[slot] = NULL;
_fio.handles[slot] = NULL;
#if defined(LIMITED_FDS)
_fio.open_handles--;
@ -184,6 +189,14 @@ void FioOpenFile(int slot, const char *filename)
FioCloseFile(slot); // if file was opened before, close it
_fio.handles[slot] = f;
_fio.filenames[slot] = filename;
/* Store the filename without path and extension */
const char *t = strrchr(filename, PATHSEPCHAR);
_fio.shortnames[slot] = strdup(t == NULL ? filename : t);
char *t2 = strrchr(_fio.shortnames[slot], '.');
if (t2 != NULL) *t2 = '\0';
strtolower(_fio.shortnames[slot]);
#if defined(LIMITED_FDS)
_fio.usage_count[slot] = 0;
_fio.open_handles++;

View File

@ -12,7 +12,7 @@
void FioSeekTo(uint32 pos, int mode);
void FioSeekToFile(uint8 slot, uint32 pos);
uint32 FioGetPos();
const char *FioGetFilename();
const char *FioGetFilename(uint8 slot);
byte FioReadByte();
uint16 FioReadWord();
uint32 FioReadDword();

View File

@ -24,7 +24,6 @@ uint _sprite_cache_size = 4;
struct SpriteCache {
void *ptr;
const char *grf_name;
uint32 id;
uint32 file_pos;
uint16 file_slot;
@ -143,7 +142,7 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
SpriteLoaderPNG sprite_loader;
SpriteLoader::Sprite sprite;
if (sprite_loader.LoadSprite(&sprite, sc->grf_name, 0, sc->id)) {
if (sprite_loader.LoadSprite(&sprite, file_slot, sc->id)) {
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
free(sprite.data);
@ -230,7 +229,7 @@ static void* ReadSprite(SpriteCache *sc, SpriteID id, bool real_sprite)
SpriteLoaderGrf sprite_loader;
SpriteLoader::Sprite sprite;
if (!sprite_loader.LoadSprite(&sprite, sc->grf_name, file_slot, file_pos)) return NULL;
if (!sprite_loader.LoadSprite(&sprite, file_slot, file_pos)) return NULL;
if (id == 142) sprite.height = 10; // Compensate for a TTD bug
sc->ptr = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, &AllocSprite);
free(sprite.data);
@ -257,19 +256,6 @@ bool LoadNextSprite(int load_index, byte file_slot, uint file_sprite_id)
sc->lru = 0;
sc->id = file_sprite_id;
const char *fio_grf_name = FioGetFilename();
const char *t = strrchr(fio_grf_name, PATHSEPCHAR);
char *grf_name;
if (t == NULL) grf_name = strdup(fio_grf_name);
else grf_name = strdup(t);
/* Make the string lowercase and strip extension */
char *t2 = strrchr(grf_name, '.');
if (t2 != NULL) *t2 = '\0';
strtolower(grf_name);
free((char *)sc->grf_name);
sc->grf_name = grf_name;
return true;
}
@ -283,8 +269,6 @@ void DupSprite(SpriteID old_spr, SpriteID new_spr)
scnew->file_pos = scold->file_pos;
scnew->ptr = NULL;
scnew->id = scold->id;
free((char *)scnew->grf_name);
scnew->grf_name = strdup(scold->grf_name);
}
@ -493,7 +477,6 @@ void GfxInitSpriteMem()
NextBlock(_spritecache_ptr)->size = 0;
/* Reset the spritecache 'pool' */
for (uint i = 0; i < _spritecache_items; i++) free((char *)_spritecache[i].grf_name);
free(_spritecache);
_spritecache_items = 0;
_spritecache = NULL;

View File

@ -9,7 +9,7 @@
#include "../core/alloc_func.hpp"
#include "grf.hpp"
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, const char *filename, uint8 file_slot, uint32 file_pos)
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos)
{
/* 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, const char *filename, uint8 file_slot, uint32 file_pos);
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos);
};
#endif /* SPRITELOADER_GRF_HPP */

View File

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

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, const char *filename, uint8 file_slot, uint32 file_pos);
bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos);
};
#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, const char *filename, uint8 file_slot, uint32 file_pos) = 0;
virtual bool LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos) = 0;
virtual ~SpriteLoader() { }
};