diff --git a/src/gamedata/textures/texturemanager.cpp b/src/gamedata/textures/texturemanager.cpp index 88de868d4..3d841eeb5 100644 --- a/src/gamedata/textures/texturemanager.cpp +++ b/src/gamedata/textures/texturemanager.cpp @@ -38,6 +38,7 @@ #include "doomstat.h" #include "w_wad.h" #include "templates.h" +#include "i_system.h" #include "r_data/r_translate.h" #include "r_data/sprites.h" @@ -396,7 +397,7 @@ FTextureID FTextureManager::GetTextureID (const char *name, ETextureType usetype FTexture *FTextureManager::FindTexture(const char *texname, ETextureType usetype, BITFIELD flags) { FTextureID texnum = CheckForTexture (texname, usetype, flags); - return !texnum.isValid()? NULL : Textures[texnum.GetIndex()].Texture; + return GetTexture(texnum.GetIndex()); } //========================================================================== @@ -1060,8 +1061,8 @@ void FTextureManager::AddLocalizedVariants() if (lang.Len() == 2 || lang.Len() == 3) { uint32_t langid = MAKE_ID(lang[0], lang[1], lang[2], 0); - uint64_t comboid = (uint64_t(langid) << 32) | tex.GetIndex(); - LocalizedTextures.Insert(comboid, GetTexture(origTex)); + uint64_t comboid = (uint64_t(langid) << 32) | origTex.GetIndex(); + LocalizedTextures.Insert(comboid, tex.GetIndex()); Textures[origTex.GetIndex()].HasLocalization = true; } else @@ -1226,12 +1227,31 @@ void FTextureManager::InitPalettedVersions() // //========================================================================== -FTextureID FTextureManager::PalCheck(FTextureID tex) +int FTextureManager::PalCheck(int tex) { // In any true color mode this shouldn't do anything. if (vid_nopalsubstitutions || V_IsTrueColor()) return tex; - auto ftex = GetTexture(tex); - if (ftex != nullptr && ftex->PalVersion != nullptr) return ftex->PalVersion->id; + auto ftex = Textures[tex].Texture; + if (ftex != nullptr && ftex->PalVersion != nullptr) return ftex->PalVersion->id.GetIndex(); + return tex; +} + +//========================================================================== +// +// FTextureManager :: PalCheck +// +//========================================================================== + +int FTextureManager::ResolveLocalizedTexture(int tex) +{ + for(int i = 0; i < 4; i++) + { + uint32_t lang = LanguageIDs[i]; + uint64_t index = (uint64_t(lang) << 32) + tex; + if (auto pTex = LocalizedTextures.CheckKey(index)) return *pTex; + index = (uint64_t(lang & MAKE_ID(255, 255, 0, 0)) << 32) + tex; + if (auto pTex = LocalizedTextures.CheckKey(index)) return *pTex; + } return tex; } diff --git a/src/gamedata/textures/textures.h b/src/gamedata/textures/textures.h index e2e2492f0..442f3b489 100644 --- a/src/gamedata/textures/textures.h +++ b/src/gamedata/textures/textures.h @@ -524,47 +524,48 @@ class FTextureManager public: FTextureManager (); ~FTextureManager (); + +private: + int ResolveLocalizedTexture(int texnum); + int PalCheck(int tex); + FTexture *InternalGetTexture(int texnum, bool animate, bool localize, bool palettesubst) + { + if ((unsigned)texnum >= Textures.Size()) return nullptr; + if (animate) texnum = Translation[texnum]; + if (localize && Textures[texnum].HasLocalization) texnum = ResolveLocalizedTexture(texnum); + if (palettesubst) texnum = PalCheck(texnum); + return Textures[texnum].Texture; + } +public: // This only gets used in UI code so we do not need PALVERS handling. FTexture *GetTextureByName(const char *name, bool animate = false) { FTextureID texnum = GetTextureID (name, ETextureType::MiscPatch); - if (!texnum.Exists()) return nullptr; - if (!animate) return Textures[texnum.GetIndex()].Texture; - else return Textures[Translation[texnum.GetIndex()]].Texture; + return InternalGetTexture(texnum.GetIndex(), animate, true, false); } FTexture *GetTexture(FTextureID texnum, bool animate = false) { - if ((size_t)texnum.GetIndex() >= Textures.Size()) return nullptr; - if (animate) texnum = Translation[texnum.GetIndex()]; - return Textures[texnum.GetIndex()].Texture; + return InternalGetTexture(texnum.GetIndex(), animate, true, false); } // This is the only access function that should be used inside the software renderer. FTexture *GetPalettedTexture(FTextureID texnum, bool animate) { - if ((size_t)texnum.texnum >= Textures.Size()) return nullptr; - if (animate) texnum = Translation[texnum.GetIndex()]; - texnum = PalCheck(texnum).GetIndex(); - return Textures[texnum.GetIndex()].Texture; + return InternalGetTexture(texnum.GetIndex(), animate, true, true); } FTexture *ByIndex(int i, bool animate = false) { - if (unsigned(i) >= Textures.Size()) return NULL; - if (animate) i = Translation[i]; - return Textures[i].Texture; + return InternalGetTexture(i, animate, true, false); } + FTexture *FindTexture(const char *texname, ETextureType usetype = ETextureType::MiscPatch, BITFIELD flags = TEXMAN_TryAny); void FlushAll(); -//public: - - FTextureID PalCheck(FTextureID tex); - enum { TEXMAN_TryAny = 1, @@ -667,7 +668,7 @@ private: }; enum { HASH_END = -1, HASH_SIZE = 1027 }; TArray Textures; - TMap LocalizedTextures; + TMap LocalizedTextures; TArray Translation; int HashFirst[HASH_SIZE]; FTextureID DefaultTexture;