From 0d74b1739be82cf0ddeccb67423bdf38cbf415fe Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 18 Jan 2018 12:35:41 +0800 Subject: [PATCH] Allow mesh textures to be reloaded --- src/graphics/sp/sp_texture_manager.cpp | 62 ++++++++++++++++-- src/graphics/sp/sp_texture_manager.hpp | 7 +- src/graphics/stk_tex_manager.cpp | 89 ++++---------------------- src/graphics/stk_tex_manager.hpp | 4 -- src/utils/debug.cpp | 12 ++-- 5 files changed, 81 insertions(+), 93 deletions(-) diff --git a/src/graphics/sp/sp_texture_manager.cpp b/src/graphics/sp/sp_texture_manager.cpp index c89dc179f..654f1f2e4 100644 --- a/src/graphics/sp/sp_texture_manager.cpp +++ b/src/graphics/sp/sp_texture_manager.cpp @@ -169,14 +169,68 @@ void SPTextureManager::removeUnusedTextures() } // removeUnusedTextures // ---------------------------------------------------------------------------- -void SPTextureManager::dumpAllTexture() +void SPTextureManager::dumpAllTextures() { for (auto p : m_textures) { - Log::info("STKTexManager", "%s size: %0.2fK", p.first.c_str(), - (p.second->getWidth() * p.second->getHeight() * 4) / 1024.0f); + Log::info("SPTextureManager", "%s", p.first.c_str()); } -} // dumpAllTexture +} // dumpAllTextures + +// ---------------------------------------------------------------------------- +core::stringw SPTextureManager::reloadTexture(const core::stringw& name) +{ + core::stringw result; +#ifndef SERVER_ONLY + if (name.empty()) + { + for (auto p : m_textures) + { + if (p.second->getPath().empty() || + p.second->getPath() == "unicolor_white") + { + continue; + } + addThreadedFunction(std::bind(&SPTexture::threadedLoad, p.second)); + Log::info("SPTextureManager", "%s reloaded", + p.second->getPath().c_str()); + } + return L"All textures reloaded."; + } + + core::stringw list = name; + list.make_lower().replace(L'\u005C', L'\u002F'); + std::vector names = + StringUtils::split(StringUtils::wideToUtf8(list), ';'); + for (const std::string& fname : names) + { + for (auto p : m_textures) + { + if (p.second->getPath().empty() || + p.second->getPath() == "unicolor_white") + { + continue; + } + std::string tex_path = + StringUtils::toLowerCase(p.second->getPath()); + std::string tex_name = StringUtils::getBasename(tex_path); + if (fname == tex_name || fname == tex_path) + { + addThreadedFunction(std::bind(&SPTexture::threadedLoad, + p.second)); + result += tex_name.c_str(); + result += L" "; + break; + } + } + } + if (result.empty()) + { + return L"Texture(s) not found!"; + } +#endif // !SERVER_ONLY + return result + "reloaded."; +} // reloadTexture // ---------------------------------------------------------------------------- } diff --git a/src/graphics/sp/sp_texture_manager.hpp b/src/graphics/sp/sp_texture_manager.hpp index 21707a1ac..1dfc91abf 100644 --- a/src/graphics/sp/sp_texture_manager.hpp +++ b/src/graphics/sp/sp_texture_manager.hpp @@ -31,11 +31,12 @@ #include #include #include -#include #include #include #include +#include "irrString.h" + class Material; namespace SP @@ -108,9 +109,9 @@ public: Material* m, bool undo_srgb, const std::string& container_id); // ------------------------------------------------------------------------ - int dumpTextureUsage(); + void dumpAllTextures(); // ------------------------------------------------------------------------ - void dumpAllTexture(); + irr::core::stringw reloadTexture(const irr::core::stringw& name); }; diff --git a/src/graphics/stk_tex_manager.cpp b/src/graphics/stk_tex_manager.cpp index f0b37bc1e..974b00ca5 100644 --- a/src/graphics/stk_tex_manager.cpp +++ b/src/graphics/stk_tex_manager.cpp @@ -152,81 +152,6 @@ void STKTexManager::removeTexture(STKTexture* texture, bool remove_all) #endif } // removeTexture -// ---------------------------------------------------------------------------- -void STKTexManager::dumpAllTexture(bool mesh_texture) -{ - for (auto p : m_all_textures) - { - if (!p.second || (mesh_texture && !p.second->isMeshTexture())) - continue; - Log::info("STKTexManager", "%s size: %0.2fK", p.first.c_str(), - float(p.second->getTextureSize()) / 1024); - } -} // dumpAllTexture - -// ---------------------------------------------------------------------------- -int STKTexManager::dumpTextureUsage() -{ - int size = 0; - for (auto p : m_all_textures) - { - if (p.second == NULL) - continue; - size += p.second->getTextureSize() / 1024 / 1024; - } - Log::info("STKTexManager", "Total %dMB", size); - return size; -} // dumpAllTexture - -// ---------------------------------------------------------------------------- -core::stringw STKTexManager::reloadTexture(const irr::core::stringw& name) -{ - core::stringw result; -#ifndef SERVER_ONLY - if (CVS->isTextureCompressionEnabled()) - return L"Please disable texture compression for reloading textures."; - - if (name.empty()) - { - for (auto p : m_all_textures) - { - if (p.second == NULL || !p.second->isMeshTexture()) - continue; - p.second->reload(); - Log::info("STKTexManager", "%s reloaded", - p.second->getName().getPtr()); - } - return L"All textures reloaded."; - } - - core::stringw list = name; - list.make_lower().replace(L'\u005C', L'\u002F'); - std::vector names = - StringUtils::split(StringUtils::wideToUtf8(list), ';'); - for (const std::string& fname : names) - { - for (auto p : m_all_textures) - { - if (p.second == NULL || !p.second->isMeshTexture()) - continue; - std::string tex_path = - StringUtils::toLowerCase(p.second->getName().getPtr()); - std::string tex_name = StringUtils::getBasename(tex_path); - if (fname == tex_name || fname == tex_path) - { - p.second->reload(); - result += tex_name.c_str(); - result += L" "; - break; - } - } - } - if (result.empty()) - return L"Texture(s) not found!"; -#endif // !SERVER_ONLY - return result + "reloaded."; -} // reloadTexture - // ---------------------------------------------------------------------------- /** Sets an error message to be displayed when a texture is not found. This * error message is shown before the "Texture %s not found or invalid" @@ -244,3 +169,17 @@ void STKTexManager::setTextureErrorMessage(const std::string &error, else m_texture_error_message = StringUtils::insertValues(error, detail); } // setTextureErrorMessage + +// ---------------------------------------------------------------------------- +int STKTexManager::dumpTextureUsage() +{ + int size = 0; + for (auto p : m_all_textures) + { + if (p.second == NULL) + continue; + size += p.second->getTextureSize() / 1024 / 1024; + } + Log::info("STKTexManager", "Total %dMB", size); + return size; +} // dumpTextureUsage diff --git a/src/graphics/stk_tex_manager.hpp b/src/graphics/stk_tex_manager.hpp index 1e5889a22..ebad439b7 100644 --- a/src/graphics/stk_tex_manager.hpp +++ b/src/graphics/stk_tex_manager.hpp @@ -83,12 +83,8 @@ public: // ------------------------------------------------------------------------ void removeTexture(STKTexture* texture, bool remove_all = false); // ------------------------------------------------------------------------ - void dumpAllTexture(bool mesh_texture); - // ------------------------------------------------------------------------ int dumpTextureUsage(); // ------------------------------------------------------------------------ - irr::core::stringw reloadTexture(const irr::core::stringw& name); - // ------------------------------------------------------------------------ /** Returns the currently defined texture error message, which is used * by event_handler.cpp to print additional info about irrlicht * internal errors or warnings. If no error message is currently diff --git a/src/utils/debug.cpp b/src/utils/debug.cpp index 50f0be8b3..b2deb9d2f 100644 --- a/src/utils/debug.cpp +++ b/src/utils/debug.cpp @@ -29,7 +29,6 @@ #include "graphics/irr_driver.hpp" #include "graphics/light.hpp" #include "graphics/shader.hpp" -#include "graphics/stk_tex_manager.hpp" #include "graphics/sp/sp_base.hpp" #include "graphics/sp/sp_texture_manager.hpp" #include "guiengine/widgets/label_widget.hpp" @@ -697,20 +696,19 @@ bool handleContextMenuAction(s32 cmd_id) new GeneralTextFieldDialog( L"Enter the texture filename(s) (separate names by ;)" " to be reloaded (empty to reload all)\n" - "Press tus; for texture usage stats (shown in console)", [] + "Press tus; for showing all mesh textures (shown in console)", [] (const irr::core::stringw& text) {}, [] (GUIEngine::LabelWidget* lw, GUIEngine::TextBoxWidget* tb)->bool { #ifndef SERVER_ONLY core::stringw t = tb->getText(); - STKTexManager* stktm = STKTexManager::getInstance(); + SP::SPTextureManager* sptm = SP::SPTextureManager::get(); if (t == "tus;") { - SP::SPTextureManager::get()->dumpAllTexture(); - stktm->dumpTextureUsage(); + sptm->dumpAllTextures(); return false; } - lw->setText(stktm->reloadTexture(t), true); + lw->setText(sptm->reloadTexture(t), true); #endif // Don't close the dialog after each run return false; @@ -888,7 +886,7 @@ bool handleStaticAction(int key) } else if (key == IRR_KEY_F3) { - STKTexManager::getInstance()->reloadTexture(""); + SP::SPTextureManager::get()->reloadTexture(""); return true; } // TODO: create more keyboard shortcuts