Port reload texture to use new STKTexManager

This commit is contained in:
Benau 2017-01-06 15:47:54 +08:00
parent 827a483e85
commit 4bcf03e296
5 changed files with 67 additions and 103 deletions

View File

@ -50,24 +50,6 @@ STKTexture* STKTexManager::findTextureInFileSystem(const std::string& filename,
return NULL;
} // findTextureInFileSystem
// ----------------------------------------------------------------------------
STKTexture* STKTexManager::findTexturePathless(const std::string& filename)
{
for (auto p : m_all_textures)
{
if (p.second == NULL)
continue;
std::string lc_name = StringUtils::toLowerCase(filename);
std::string lc_path =
StringUtils::toLowerCase(p.first);
std::string tex_name = StringUtils::getBasename(lc_path);
if (lc_name == tex_name || lc_name == lc_path)
return p.second;
}
return NULL;
} // findTexturePathless
// ----------------------------------------------------------------------------
video::ITexture* STKTexManager::getTexture(const std::string& path, bool srgb,
bool premul_alpha,
@ -151,3 +133,51 @@ video::ITexture* STKTexManager::getUnicolorTexture(const irr::video::SColor &c)
return texture;
} // getUnicolorTexture
// ----------------------------------------------------------------------------
core::stringw STKTexManager::reloadTexture(const irr::core::stringw& name)
{
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 result;
core::stringw list = name;
list.make_lower().replace(L'\u005C', L'\u002F');
std::vector<std::string> 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!";
return result + "reloaded.";
} // reloadTexture

View File

@ -22,6 +22,8 @@
#include "utils/no_copy.hpp"
#include "utils/singleton.hpp"
#include "irrString.h"
#include <algorithm>
#include <string>
#include <unordered_map>
@ -37,8 +39,6 @@ class STKTexManager : public Singleton<STKTexManager>, NoCopy
private:
std::unordered_map<std::string, STKTexture*> m_all_textures;
// ------------------------------------------------------------------------
STKTexture* findTexturePathless(const std::string& filename);
// ------------------------------------------------------------------------
STKTexture* findTextureInFileSystem(const std::string& filename,
std::string* full_path);
@ -66,6 +66,8 @@ public:
int dumpTextureUsage();
// ------------------------------------------------------------------------
void clean();
// ------------------------------------------------------------------------
irr::core::stringw reloadTexture(const irr::core::stringw& name);
}; // STKTexManager

View File

@ -245,79 +245,5 @@ void saveCompressedTexture(const std::string& compressed_tex)
#endif
}
core::stringw reloadTexture(const core::stringw& name)
{
if (!CVS->isGLSL())
return L"Use shader based renderer to reload textures.";
if (CVS->isTextureCompressionEnabled())
return L"Please disable texture compression for reloading textures.";
if (name.empty())
{
for (video::ITexture* tex : AlreadyTransformedTexture)
reloadSingleTexture(tex);
return L"All textures reloaded.";
}
core::stringw result;
core::stringw list = name;
list.make_lower().replace(L'\u005C', L'\u002F');
std::vector<std::string> names =
StringUtils::split(StringUtils::wideToUtf8(list), ';');
for (const std::string& fname : names)
{
for (video::ITexture* tex : AlreadyTransformedTexture)
{
std::string tex_path =
StringUtils::toLowerCase(tex->getName().getPtr());
std::string tex_name = StringUtils::getBasename(tex_path);
if (fname == tex_name || fname == tex_path)
{
if (reloadSingleTexture(tex))
{
result += tex_name.c_str();
result += L" ";
break;
}
}
}
}
if (result.empty())
return L"Texture(s) not found!";
return result + "reloaded.";
}
bool reloadSingleTexture(video::ITexture* tex)
{
video::IImage* tmp =
irr_driver->getVideoDriver()->createImageFromFile(tex->getName());
if (tmp == NULL) return false;
const bool scaling = tmp->getDimension() != tex->getSize();
video::IImage* new_texture =
irr_driver->getVideoDriver()->createImage(video::ECF_A8R8G8B8,
scaling ? tex->getSize() : tmp->getDimension());
if (scaling)
tmp->copyToScaling(new_texture);
else
tmp->copyTo(new_texture);
tmp->drop();
glBindTexture(GL_TEXTURE_2D, getTextureGLuint(tex));
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, new_texture->getDimension().Width,
new_texture->getDimension().Height, GL_BGRA, GL_UNSIGNED_BYTE,
new_texture->lock());
new_texture->unlock();
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
#if defined(USE_GLES2)
static_cast<irr::video::COGLES2Texture*>(tex)->setImage(new_texture);
#else
static_cast<irr::video::COpenGLTexture*>(tex)->setImage(new_texture);
#endif
Log::info("TextureManager", "%s reloaded", tex->getName().getPtr());
return true;
}
#endif // !SERVER_ONLY

View File

@ -26,14 +26,11 @@
#include <string>
GLuint getTextureGLuint(irr::video::ITexture *tex);
void resetTextureTable();
void cleanUnicolorTextures();
void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha = false);
bool loadCompressedTexture(const std::string& compressed_tex);
void saveCompressedTexture(const std::string& compressed_tex);
void insertTextureHandle(uint64_t handle);
bool reloadSingleTexture(irr::video::ITexture *tex);
irr::core::stringw reloadTexture(const irr::core::stringw& name);
#endif

View File

@ -29,7 +29,7 @@
#include "graphics/irr_driver.hpp"
#include "graphics/light.hpp"
#include "graphics/shaders.hpp"
#include "graphics/texture_manager.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
#include "items/powerup_manager.hpp"
@ -133,7 +133,7 @@ enum DebugMenuCommand
DEBUG_ADJUST_LIGHTS,
DEBUG_SCRIPT_CONSOLE,
DEBUG_RUN_CUTSCENE,
DEBUG_RELOAD_TEXTURE,
DEBUG_TEXTURE_CONSOLE,
}; // DebugMenuCommand
// -----------------------------------------------------------------------------
@ -687,15 +687,24 @@ bool handleContextMenuAction(s32 cmd_id)
((CutsceneWorld*)World::getWorld())->setParts(parts);
});
break;
case DEBUG_RELOAD_TEXTURE:
case DEBUG_TEXTURE_CONSOLE:
new GeneralTextFieldDialog(
L"Enter the texture filename(s) (separate names by ;)"
" to be reloaded (empty to reload all)", []
" to be reloaded (empty to reload all)\n"
"Press tus; for texture usage stats (shown in console)", []
(const irr::core::stringw& text) {},
[] (GUIEngine::LabelWidget* lw, GUIEngine::TextBoxWidget* tb)->bool
{
#ifndef SERVER_ONLY
lw->setText(reloadTexture(tb->getText()), true);
core::stringw t = tb->getText();
STKTexManager* stktm = STKTexManager::getInstance();
if (t == "tus;")
{
stktm->dumpAllTexture(false/*mesh_texture*/);
stktm->dumpTextureUsage();
return false;
}
lw->setText(stktm->reloadTexture(t), true);
#endif
// Don't close the dialog after each run
return false;
@ -809,7 +818,7 @@ bool onEvent(const SEvent &event)
mnu->addItem(L"Adjust Lights", DEBUG_ADJUST_LIGHTS);
mnu->addItem(L"Scripting console", DEBUG_SCRIPT_CONSOLE);
mnu->addItem(L"Run cutscene(s)", DEBUG_RUN_CUTSCENE);
mnu->addItem(L"Reload texture", DEBUG_RELOAD_TEXTURE);
mnu->addItem(L"Texture console", DEBUG_TEXTURE_CONSOLE);
g_debug_menu_visible = true;
irr_driver->showPointer();