Skip to content

Commit

Permalink
GRAPHICS: Don't try to reload dynamic textures
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 20, 2014
1 parent 5efcdf0 commit 0db12cf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
7 changes: 1 addition & 6 deletions src/graphics/aurora/ttffont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "common/error.h"
#include "common/ustring.h"
#include "common/threads.h"
#include "common/uuid.h"

#include "aurora/resman.h"

Expand Down Expand Up @@ -81,21 +80,17 @@ void TTFFont::Page::createTexture() {
return RequestMan.callInMainThread(functor);
}

const Common::UString name = Common::generateIDNumberString();

const Ogre::TextureType texType = Ogre::TEX_TYPE_2D;

const uint width = surface->getWidth();
const uint height = surface->getHeight();

// If the image has only have one mipmap, automatically generate the smaller ones
const int mipMaps = 1;
const int usage = Ogre::TU_DYNAMIC_WRITE_ONLY | Ogre::TU_AUTOMIPMAP;

const Ogre::PixelFormat format = (Ogre::PixelFormat) surface->getFormat();

texture = Ogre::TextureManager::getSingleton().createManual(name.c_str(), "General",
texType, width, height, mipMaps, format, usage);
texture = TextureMan.createDynamic(texType, width, height, mipMaps, format, usage);
}

void TTFFont::Page::copyTexture() {
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/materialman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void MaterialManager::create(const MaterialDeclaration &decl, Ogre::MaterialPtr
if (!texture->hasAlpha() || ((PixelFormat)texture->getFormat() == kPixelFormatDXT1))
transparent = false;

if (!TextureMan.getProperties(decl.textures[t]).getBool("decal"))
if (!TextureMan.getProperties(texture).getBool("decal"))
decal = false;
}

Expand Down
73 changes: 58 additions & 15 deletions src/graphics/textureman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "common/error.h"
#include "common/stream.h"
#include "common/threads.h"
#include "common/uuid.h"

#include "../aurora/types.h"
#include "../aurora/resman.h"
Expand Down Expand Up @@ -261,12 +262,14 @@ Ogre::TexturePtr TextureManager::get(const Common::UString &name) {
return RequestMan.callInMainThread(functor);
}

Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName(name.c_str());
const Common::UString textureName = canonicalName(name);

Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName(textureName.c_str());
if (!texture.isNull())
return texture;

try {
texture = create(name);
texture = create(name, textureName);
} catch (Common::Exception &e) {
e.add("Failed to load texture \"%s\"", name.c_str());
throw;
Expand All @@ -275,20 +278,27 @@ Ogre::TexturePtr TextureManager::get(const Common::UString &name) {
return texture;
}

ImageDecoder *TextureManager::createImage(const Common::UString &name) {
Ogre::TexturePtr TextureManager::createDynamic(Ogre::TextureType type, uint width, uint height,
int mipMaps, Ogre::PixelFormat format, int usage) {

return Ogre::TextureManager::getSingleton().createManual(dynamicName().c_str(), "General",
type, width, height, mipMaps, format, usage);
}

ImageDecoder *TextureManager::createImage(const Common::UString &imageName, const Common::UString &textureName) {
// Get the image resource
::Aurora::FileType type;
Common::SeekableReadStream *img = ResMan.getResource(::Aurora::kResourceImage, name, &type);
Common::SeekableReadStream *img = ResMan.getResource(::Aurora::kResourceImage, imageName, &type);
if (!img)
throw Common::Exception("No such image resource", name.c_str());
throw Common::Exception("No such image resource", imageName.c_str());

// (Re)Create properties
std::pair<Properties::iterator, bool> p = _properties.insert(std::make_pair(name, (TextureProperties *) 0));
std::pair<Properties::iterator, bool> p = _properties.insert(std::make_pair(textureName, (TextureProperties *) 0));
delete p.first->second;

p.first->second = new TextureProperties;

p.first->second->loadFromTXI(name);
p.first->second->loadFromTXI(imageName);

ImageDecoder *image = 0;
WinIconImage *cursor = 0;
Expand Down Expand Up @@ -340,13 +350,13 @@ ImageDecoder *TextureManager::createImage(const Common::UString &name) {
return image;
}

Ogre::TexturePtr TextureManager::create(const Common::UString &name) {
Ogre::TexturePtr TextureManager::create(const Common::UString &imageName, const Common::UString &textureName) {
ImageDecoder *image = 0;
Ogre::TexturePtr texture((Ogre::Texture *) 0);

try {
image = createImage(name);
texture = create(name, *image);
image = createImage(imageName, textureName);
texture = create(imageName, textureName, *image);
} catch (...) {
delete image;
throw;
Expand All @@ -356,7 +366,7 @@ Ogre::TexturePtr TextureManager::create(const Common::UString &name) {
return texture;
}

Ogre::TexturePtr TextureManager::create(const Common::UString &name, const ImageDecoder &image) {
Ogre::TexturePtr TextureManager::create(const Common::UString &imageName, const Common::UString &textureName, const ImageDecoder &image) {
const Ogre::TextureType texType = Ogre::TEX_TYPE_2D;

const uint width = image.getMipMap(0).width;
Expand All @@ -370,7 +380,7 @@ Ogre::TexturePtr TextureManager::create(const Common::UString &name, const Image

Ogre::TexturePtr texture((Ogre::Texture *) 0);
try {
texture = Ogre::TextureManager::getSingleton().createManual(name.c_str(), "General",
texture = Ogre::TextureManager::getSingleton().createManual(textureName.c_str(), "General",
texType, width, height, mipMaps, format, usage);

// Make sure the image dimensions fit
Expand Down Expand Up @@ -434,10 +444,12 @@ void TextureManager::convert(Ogre::TexturePtr &texture, const ImageDecoder &imag
}

bool TextureManager::dumpTGA(const Common::UString &name, const Common::UString &fileName) {
const Common::UString textureName = canonicalName(name);

ImageDecoder *image = 0;

try {
image = createImage(name);
image = createImage(name, textureName);
image->dumpTGA(fileName);
} catch (Common::Exception &e) {
delete image;
Expand All @@ -459,6 +471,8 @@ void TextureManager::reloadAll() {
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>();
if (isDynamic(texture))
continue;

try {
reload(texture);
Expand All @@ -470,12 +484,16 @@ void TextureManager::reloadAll() {
}

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

if (imageName.empty())
throw Common::Exception("Invalid name scheme");

ImageDecoder *image = 0;

try {
image = createImage(name);
image = createImage(imageName, textureName);

reload(texture, *image);

Expand Down Expand Up @@ -519,4 +537,29 @@ void TextureManager::reload(Ogre::TexturePtr &texture, const ImageDecoder &image
}
}

bool TextureManager::isDynamic(const Ogre::TexturePtr &texture) {
assert(!texture.isNull());

return !Common::UString(texture->getName().c_str()).beginsWith("static/");
}

Common::UString TextureManager::canonicalName(const Common::UString &texture) {
return Common::UString("static/" + texture);
}

Common::UString TextureManager::dynamicName() {
return Common::UString("dynamic/") + Common::generateIDNumberString();
}

Common::UString TextureManager::getImageName(const Ogre::TexturePtr &texture) {
if (TextureMan.isDynamic(texture))
return "";

Common::UString name = texture->getName().c_str();

name.erase(name.begin(), name.getPosition(Common::UString("static/").size()));

return name;
}

} // End of namespace Graphics
18 changes: 15 additions & 3 deletions src/graphics/textureman.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <OgrePrerequisites.h>
#include <OgrePixelFormat.h>
#include <OgreTexture.h>

#include "common/singleton.h"
#include "common/stringmap.h"
Expand Down Expand Up @@ -97,6 +98,10 @@ class TextureManager : public Common::Singleton<TextureManager> {
/** Get/Load a texture. */
Ogre::TexturePtr get(const Common::UString &name);

/** Create a dynamic texture. */
Ogre::TexturePtr createDynamic(Ogre::TextureType type, uint width, uint height,
int mipMaps, Ogre::PixelFormat format, int usage);

/** Get the properties of a texture. */
const TextureProperties &getProperties(const Common::UString &name);
/** Get the properties of a texture. */
Expand All @@ -114,15 +119,22 @@ class TextureManager : public Common::Singleton<TextureManager> {
Properties _properties;


Ogre::TexturePtr create(const Common::UString &name);
Ogre::TexturePtr create(const Common::UString &name, const ImageDecoder &image);
Ogre::TexturePtr create(const Common::UString &imageName, const Common::UString &textureName);
Ogre::TexturePtr create(const Common::UString &imageName, const Common::UString &textureName, const ImageDecoder &image);

ImageDecoder *createImage(const Common::UString &name);
ImageDecoder *createImage(const Common::UString &imageName, const Common::UString &textureName);

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

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

static bool isDynamic(const Ogre::TexturePtr &texture);

static Common::UString canonicalName(const Common::UString &texture);
static Common::UString dynamicName();

Common::UString getImageName(const Ogre::TexturePtr &texture);
};

} // End of namespace Graphics
Expand Down
4 changes: 2 additions & 2 deletions src/video/aurora/videoplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class VideoFrameRenderer : public Ogre::FrameListener {

// Create the material and texture for the video content

_videoTexture = Ogre::TextureManager::getSingleton().createManual(nameVideo.c_str(), "General",
Ogre::TEX_TYPE_2D, textureWidth, textureHeight, 1, Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE | Ogre::TU_AUTOMIPMAP);
_videoTexture = TextureMan.createDynamic(Ogre::TEX_TYPE_2D, textureWidth, textureHeight, 1,
Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE | Ogre::TU_AUTOMIPMAP);

Ogre::HardwarePixelBufferSharedPtr buffer = _videoTexture->getBuffer();
memset(buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD), 0, buffer->getSizeInBytes());
Expand Down

0 comments on commit 0db12cf

Please sign in to comment.