diff options
author | peter1138 <peter1138@openttd.org> | 2008-01-22 07:27:06 +0000 |
---|---|---|
committer | peter1138 <peter1138@openttd.org> | 2008-01-22 07:27:06 +0000 |
commit | ffec79bbcf36028bc676310c71a40b0b033e85c5 (patch) | |
tree | aa8442d483b420571317c63234db3640f7e6df7b /src | |
parent | 9444eb4484e5691a52ddc458bd8b401c6e19571d (diff) | |
download | openttd-ffec79bbcf36028bc676310c71a40b0b033e85c5.tar.xz |
(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.
Diffstat (limited to 'src')
-rw-r--r-- | src/fileio.cpp | 17 | ||||
-rw-r--r-- | src/fileio.h | 2 | ||||
-rw-r--r-- | src/spritecache.cpp | 21 | ||||
-rw-r--r-- | src/spriteloader/grf.cpp | 2 | ||||
-rw-r--r-- | src/spriteloader/grf.hpp | 2 | ||||
-rw-r--r-- | src/spriteloader/png.cpp | 3 | ||||
-rw-r--r-- | src/spriteloader/png.hpp | 2 | ||||
-rw-r--r-- | src/spriteloader/spriteloader.hpp | 2 |
8 files changed, 24 insertions, 27 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp index 34b3cf835..09329b84f 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -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++; diff --git a/src/fileio.h b/src/fileio.h index d60d8ed2e..881c983f3 100644 --- a/src/fileio.h +++ b/src/fileio.h @@ -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(); diff --git a/src/spritecache.cpp b/src/spritecache.cpp index 48c4d67e2..0db889946 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -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; diff --git a/src/spriteloader/grf.cpp b/src/spriteloader/grf.cpp index d0bc0e334..bbc9d3f8f 100644 --- a/src/spriteloader/grf.cpp +++ b/src/spriteloader/grf.cpp @@ -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); diff --git a/src/spriteloader/grf.hpp b/src/spriteloader/grf.hpp index fe9405339..65b5f6233 100644 --- a/src/spriteloader/grf.hpp +++ b/src/spriteloader/grf.hpp @@ -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 */ diff --git a/src/spriteloader/png.cpp b/src/spriteloader/png.cpp index abfce00fa..2af9d0253 100644 --- a/src/spriteloader/png.cpp +++ b/src/spriteloader/png.cpp @@ -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; diff --git a/src/spriteloader/png.hpp b/src/spriteloader/png.hpp index eb3719180..7d821dc71 100644 --- a/src/spriteloader/png.hpp +++ b/src/spriteloader/png.hpp @@ -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 */ diff --git a/src/spriteloader/spriteloader.hpp b/src/spriteloader/spriteloader.hpp index 47d1b7d40..09ad505a8 100644 --- a/src/spriteloader/spriteloader.hpp +++ b/src/spriteloader/spriteloader.hpp @@ -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() { } }; |