From d812db19f86faf76f4bbd5628d77f73df290d8e3 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 25 Mar 2016 17:06:01 +0100 Subject: [PATCH] Fixed ground spots not working after changing texture params; use UpdateTexture instead of destroy and recreate --- src/graphics/engine/engine.cpp | 43 ++++++++++++++-------------------- src/graphics/engine/engine.h | 3 +++ src/level/robotmain.cpp | 5 +--- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 95deda73..d2d0d0a6 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -374,6 +374,7 @@ void CEngine::ReloadAllTextures() m_text->FlushCache(); CRobotMain::GetInstancePointer()->ReloadAllTextures(); //TODO: Remove cross-reference to CRobotMain + UpdateGroundSpotTextures(); LoadAllTextures(); } @@ -1503,12 +1504,7 @@ void CEngine::DeleteAllGroundSpots() str << "shadow" << std::setfill('0') << std::setw(2) << s << ".png"; std::string texName = str.str(); - DeleteTexture(texName); - - Gfx::Texture tex = m_device->CreateTexture(&shadowImg, m_defaultTexParams); - - m_texNameMap[texName] = tex; - m_revTexNameMap[tex] = texName; + CreateOrUpdateTexture(texName, &shadowImg); } } @@ -2367,8 +2363,6 @@ bool CEngine::ChangeTextureColor(const std::string& texName, Math::Point ts, Math::Point ti, Math::Point *exclude, float shift, bool hsv) { - DeleteTexture(texName); - CImage img; if (!img.Load(srcName)) { @@ -2474,18 +2468,7 @@ bool CEngine::ChangeTextureColor(const std::string& texName, } } - - Texture tex = m_device->CreateTexture(&img, m_defaultTexParams); - - if (! tex.Valid()) - { - GetLogger()->Error("Couldn't load texture '%s', blacklisting\n", texName.c_str()); - m_texBlacklist.insert(texName); - return false; - } - - m_texNameMap[texName] = tex; - m_revTexNameMap[tex] = texName; + CreateOrUpdateTexture(texName, &img); return true; } @@ -2531,6 +2514,19 @@ void CEngine::DeleteTexture(const Texture& tex) m_texNameMap.erase(it); } +void CEngine::CreateOrUpdateTexture(const std::string& texName, CImage* img) +{ + auto it = m_texNameMap.find(texName); + if (it == m_texNameMap.end()) + { + LoadTexture(texName, img); + } + else + { + m_device->UpdateTexture((*it).second, Math::IntPoint(0, 0), img->GetData(), m_defaultTexParams.format); + } +} + void CEngine::FlushTextureCache() { m_device->DestroyAllTextures(); @@ -4135,12 +4131,7 @@ void CEngine::UpdateGroundSpotTextures() str << "textures/shadow" << std::setfill('0') << std::setw(2) << s << ".png"; std::string texName = str.str(); - DeleteTexture(texName); - - Gfx::Texture tex = m_device->CreateTexture(&shadowImg, m_defaultTexParams); - - m_texNameMap[texName] = tex; - m_revTexNameMap[tex] = texName; + CreateOrUpdateTexture(texName, &shadowImg); } } diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index 97edea91..5fc65a2c 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -946,6 +946,9 @@ public: //! Deletes the given texture, unloading it and removing from cache void DeleteTexture(const Texture& tex); + //! Creates or updates the given texture with given image data + void CreateOrUpdateTexture(const std::string& texName, CImage* img); + //! Empties the texture cache void FlushTextureCache(); diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index cbcd5f89..514d2181 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -3398,7 +3398,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) InitEye(); SetMovieLock(false); - if(!resetObject) + if (!resetObject) ChangeColor(); // changes the colors of texture if (!m_sceneReadPath.empty()) // loading file ? @@ -4011,9 +4011,6 @@ void CRobotMain::ChangeColor() ts = Math::Point(0.00f, 0.75f); ti = Math::Point(0.25f, 1.00f); m_engine->ChangeTextureColor("textures/effect02.png", m_colorRefWater, m_colorNewWater, colorRef2, colorNew2, 0.20f, -1.0f, ts, ti, nullptr, m_colorShiftWater, true); - - // This loads the newly recolored textures to objects - m_engine->LoadAllTextures(); } //! Calculates the distance to the nearest object