diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2013-05-26 14:50:23 +0200 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2013-05-26 14:50:23 +0200 |
commit | 8f6fd2a1316432970f1d86515819e7bbe15ddab3 (patch) | |
tree | 5852619dfe41c59bb7bf23df2652b7a6d8d3689e /src/graphics | |
parent | b41957f2f95d8f62817705a1c82322d9463d28a2 (diff) | |
download | colobot-8f6fd2a1316432970f1d86515819e7bbe15ddab3.tar.gz colobot-8f6fd2a1316432970f1d86515819e7bbe15ddab3.tar.bz2 colobot-8f6fd2a1316432970f1d86515819e7bbe15ddab3.zip |
Changes in texture code & refactoring
* refactored and altered slightly the texture pack code
* added flushing of texture cache
* some refactoring and const-correctness in CApplication methods
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/engine/engine.cpp | 85 | ||||
-rw-r--r-- | src/graphics/engine/engine.h | 11 |
2 files changed, 47 insertions, 49 deletions
diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index b697a37..3959c02 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -62,8 +62,6 @@ CEngine::CEngine(CApplication *app) m_sound = nullptr; m_terrain = nullptr; - m_texPack = ""; - m_showStats = false; m_focus = 0.75f; @@ -238,12 +236,6 @@ void CEngine::SetTerrain(CTerrain* terrain) m_terrain = terrain; } -void CEngine::SetTexturePack(const std::string& texpackName) -{ - m_texPack = texpackName; -} - - bool CEngine::Create() { m_size = m_app->GetVideoConfig().size; @@ -323,7 +315,7 @@ void CEngine::ResetAfterDeviceChanged() m_text->FlushCache(); - // TODO reload textures, reset device state, etc. + FlushTextureCache(); } bool CEngine::ProcessEvent(const Event &event) @@ -2117,7 +2109,7 @@ void CEngine::SetViewParams(const Math::Vector& eyePt, const Math::Vector& looka m_sound->SetListener(eyePt, lookatPt); } -Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params, CImage* image, std::string orginalName) +Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params, CImage* image) { if (texName.empty()) return Texture(); // invalid texture @@ -2126,39 +2118,53 @@ Texture CEngine::CreateTexture(const std::string& texName, const TextureCreatePa return Texture(); // invalid texture Texture tex; + CImage img; if (image == nullptr) { - CImage img; - if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName))) + bool loadedFromTexPack = false; + + std::string texPackName = m_app->GetTexPackFilePath(texName); + if (! texPackName.empty()) { - std::string error = img.GetError(); - if(orginalName == "") GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str()); - m_texBlacklist.insert(texName); - return Texture(); // invalid texture + if (img.Load(texPackName)) + { + loadedFromTexPack = true; + } + else + { + std::string error = img.GetError(); + GetLogger()->Error("Couldn't load texture '%s' from texpack: %s, blacklisting the texpack path\n", + texName.c_str(), error.c_str()); + m_texBlacklist.insert(texPackName); + } } - tex = m_device->CreateTexture(&img, params); - } - else - { - tex = m_device->CreateTexture(image, params); + if (!loadedFromTexPack) + { + if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName))) + { + std::string error = img.GetError(); + GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str()); + m_texBlacklist.insert(texName); + return Texture(); // invalid texture + } + } + + image = &img; } + tex = m_device->CreateTexture(&img, params); + if (! tex.Valid()) { - if(orginalName == "") GetLogger()->Error("Couldn't load texture '%s', blacklisting\n", texName.c_str()); + GetLogger()->Error("Couldn't load texture '%s', blacklisting\n", texName.c_str()); m_texBlacklist.insert(texName); return tex; } - if(orginalName == "") { - m_texNameMap[texName] = tex; - m_revTexNameMap[tex] = texName; - } else { - m_texNameMap[orginalName] = tex; - m_revTexNameMap[tex] = orginalName; - } + m_texNameMap[texName] = tex; + m_revTexNameMap[tex] = texName; return tex; } @@ -2183,19 +2189,7 @@ Texture CEngine::LoadTexture(const std::string& name, const TextureCreateParams& if (it != m_texNameMap.end()) return (*it).second; - Texture tex; - if (m_texPack != "") { - std::string name_texpack = m_texPack + "/" + name; - - if (m_texBlacklist.find(name_texpack) == m_texBlacklist.end()) { - tex = CreateTexture(name_texpack, params, nullptr, name); - if (tex.Valid()) - return tex; - } - } - - tex = CreateTexture(name, params); - return tex; + return CreateTexture(name, params); } bool CEngine::LoadAllTextures() @@ -2453,6 +2447,13 @@ void CEngine::DeleteTexture(const Texture& tex) m_texNameMap.erase(it); } +void CEngine::FlushTextureCache() +{ + m_texNameMap.clear(); + m_revTexNameMap.clear(); + m_texBlacklist.clear(); +} + bool CEngine::SetTexture(const std::string& name, int stage) { auto it = m_texNameMap.find(name); diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index 8df912e..af8cf6a 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -726,9 +726,6 @@ public: //! Writes a screenshot containing the current frame bool WriteScreenShot(const std::string& fileName, int width, int height); - //! Set texture pack - void SetTexturePack(const std::string& texpackName); - //@{ //! Management of game pause mode @@ -961,6 +958,9 @@ public: //! Deletes the given texture, unloading it and removing from cache void DeleteTexture(const Texture& tex); + //! Empties the texture cache + void FlushTextureCache(); + //! Defines of the distance field of vision void SetTerrainVision(float vision); @@ -1233,7 +1233,7 @@ protected: const Material& mat, int state); //! Create texture and add it to cache - Texture CreateTexture(const std::string &texName, const TextureCreateParams ¶ms, CImage* image = nullptr, std::string orginalName = ""); + Texture CreateTexture(const std::string &texName, const TextureCreateParams ¶ms, CImage* image = nullptr); //! Tests whether the given object is visible bool IsVisible(int objRank); @@ -1407,9 +1407,6 @@ protected: * so are disabled for subsequent load calls. */ std::set<std::string> m_texBlacklist; - //! Texture pack - std::string m_texPack; - //! Mouse cursor definitions EngineMouse m_mice[ENG_MOUSE_COUNT]; //! Texture with mouse cursors |