Skip to content

Commit

Permalink
GRAPHICS: Add TextureManager::reloadAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Feb 12, 2014
1 parent 9cbdc6b commit 027498d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/graphics/textureman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <OgreTextureManager.h>
#include <OgreHardwarePixelBuffer.h>
#include <OgreResourceManager.h>

#include "common/error.h"
#include "common/stream.h"
Expand Down Expand Up @@ -448,4 +449,74 @@ bool TextureManager::dumpTGA(const Common::UString &name, const Common::UString
return true;
}

void TextureManager::reloadAll() {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<void> functor(boost::bind(&TextureManager::reloadAll, this));

return RequestMan.callInMainThread(functor);
}

Ogre::TextureManager &texMan = Ogre::TextureManager::getSingleton();
for (Ogre::ResourceManager::ResourceMapIterator t = texMan.getResourceIterator(); t.hasMoreElements(); t.moveNext()) {
Ogre::TexturePtr texture = t.current()->second.staticCast<Ogre::Texture>();

try {
reload(texture);
} catch (Common::Exception &e) {
e.add("Failed to reload texture \"%s\"", texture->getName().c_str());
Common::printException(e, "WARNING: ");
}
}
}

void TextureManager::reload(Ogre::TexturePtr &texture) {
const Common::UString name = texture->getName().c_str();

ImageDecoder *image = 0;

try {
image = createImage(name);

reload(texture, *image);

} catch (...) {
delete image;
throw;
}

delete image;
}

void TextureManager::reload(Ogre::TexturePtr &texture, const ImageDecoder &image) {
const uint width = image.getMipMap(0).width;
const uint height = image.getMipMap(0).height;

// If the image has only have one mipmap, automatically generate the smaller ones
const int mipMaps = (image.getMipMapCount() > 1) ? image.getMipMapCount() : Ogre::MIP_UNLIMITED;
const int usage = Ogre::TU_STATIC_WRITE_ONLY | ((image.getMipMapCount() > 1) ? 0 : Ogre::TU_AUTOMIPMAP);

const Ogre::PixelFormat format = (Ogre::PixelFormat) image.getFormat();

try {
texture->freeInternalResources();
texture->setWidth(width);
texture->setHeight(height);
texture->setNumMipmaps(mipMaps);
texture->setUsage(usage);
texture->setFormat(format);
texture->createInternalResources();

// Make sure the image dimensions fit
if ((texture->getWidth() != width) || (texture->getHeight() != height))
throw Common::Exception("Requested texture size mismatch (%dx%d vs %dx%d)",
width, height, texture->getWidth(), texture->getHeight());

// Convert the image into a texture
convert(texture, image, (mipMaps == Ogre::MIP_UNLIMITED) ? 1 : mipMaps);

} catch (std::exception &se) {
throw Common::Exception("%s", se.what());
}
}

} // End of namespace Graphics
6 changes: 6 additions & 0 deletions src/graphics/textureman.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class TextureManager : public Common::Singleton<TextureManager> {
/** Dump a texture into a TGA file. */
bool dumpTGA(const Common::UString &name, const Common::UString &fileName);

/** Reload all textures from their image resources. */
void reloadAll();

private:
typedef std::map<Common::UString, TextureProperties *> Properties;

Expand All @@ -117,6 +120,9 @@ class TextureManager : public Common::Singleton<TextureManager> {
ImageDecoder *createImage(const Common::UString &name);

void convert(Ogre::TexturePtr &texture, const ImageDecoder &image, int mipMaps);

void reload(Ogre::TexturePtr &texture);
void reload(Ogre::TexturePtr &texture, const ImageDecoder &image);
};

} // End of namespace Graphics
Expand Down

0 comments on commit 027498d

Please sign in to comment.