Add live_textures_reload command

This commit is contained in:
Eduardo Bart
2013-03-01 05:46:55 -03:00
parent e85afd4b63
commit 8314b84f69
15 changed files with 93 additions and 31 deletions

View File

@@ -30,11 +30,13 @@
Texture::Texture()
{
m_id = 0;
m_time = 0;
}
Texture::Texture(const Size& size)
{
m_id = 0;
m_time = 0;
if(!setupSize(size))
return;
@@ -49,9 +51,7 @@ Texture::Texture(const Size& size)
Texture::Texture(const ImagePtr& image, bool buildMipmaps, bool compress)
{
m_id = 0;
if(!setupSize(image->getSize(), buildMipmaps))
return;
m_time = 0;
createTexture();
@@ -70,6 +70,9 @@ Texture::~Texture()
void Texture::uploadPixels(const ImagePtr& image, bool buildMipmaps, bool compress)
{
if(!setupSize(image->getSize(), buildMipmaps))
return;
ImagePtr glImage = image;
if(m_size != m_glSize) {
glImage = ImagePtr(new Image(m_glSize, image->getBpp()));

View File

@@ -41,8 +41,10 @@ public:
virtual void setSmooth(bool smooth);
virtual void setRepeat(bool repeat);
void setUpsideDown(bool upsideDown);
void setTime(ticks_t time) { m_time = time; }
uint getId() { return m_id; }
uint getId() { return m_id; }
ticks_t getTime() { return m_time; }
int getWidth() { return m_size.width(); }
int getHeight() { return m_size.height(); }
const Size& getSize() { return m_size; }
@@ -62,6 +64,7 @@ protected:
void setupPixels(int level, const Size& size, uchar *pixels, int channels = 4, bool compress = false);
uint m_id;
ticks_t m_time;
Size m_size;
Size m_glSize;
Matrix3 m_transformMatrix;

View File

@@ -27,6 +27,7 @@
#include <framework/core/resourcemanager.h>
#include <framework/core/clock.h>
#include <framework/core/eventdispatcher.h>
#include <framework/graphics/apngloader.h>
TextureManager g_textures;
@@ -38,6 +39,10 @@ void TextureManager::init()
void TextureManager::terminate()
{
if(m_liveReloadEvent) {
m_liveReloadEvent->cancel();
m_liveReloadEvent = nullptr;
}
m_textures.clear();
m_animatedTextures.clear();
m_emptyTexture = nullptr;
@@ -56,12 +61,32 @@ void TextureManager::poll()
animatedTexture->updateAnimation();
}
void TextureManager::clearTexturesCache()
void TextureManager::clearCache()
{
m_animatedTextures.clear();
m_textures.clear();
}
void TextureManager::liveReload()
{
if(m_liveReloadEvent)
return;
m_liveReloadEvent = g_dispatcher.cycleEvent([this] {
for(auto& it : m_textures) {
const std::string& path = g_resources.guessFilePath(it.first, "png");
const TexturePtr& tex = it.second;
if(tex->getTime() >= g_resources.getFileTime(path))
continue;
ImagePtr image = Image::load(path);
if(!image)
continue;
tex->uploadPixels(image, tex->hasMipmaps());
tex->setTime(stdext::time());
}
}, 1000);
}
TexturePtr TextureManager::getTexture(const std::string& fileName)
{
TexturePtr texture;
@@ -83,13 +108,14 @@ TexturePtr TextureManager::getTexture(const std::string& fileName)
// load texture file data
std::stringstream fin;
g_resources.readFileStream(filePathEx, fin);
texture = loadPNG(fin);
texture = loadTexture(fin);
} catch(stdext::exception& e) {
g_logger.error(stdext::format("Unable to load texture '%s': %s", fileName, e.what()));
texture = g_textures.getEmptyTexture();
}
if(texture) {
texture->setTime(stdext::time());
texture->setSmooth(true);
m_textures[filePath] = texture;
}
@@ -98,7 +124,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName)
return texture;
}
TexturePtr TextureManager::loadPNG(std::stringstream& file)
TexturePtr TextureManager::loadTexture(std::stringstream& file)
{
TexturePtr texture;

View File

@@ -24,6 +24,7 @@
#define TEXTUREMANAGER_H
#include "texture.h"
#include <framework/core/declarations.h>
class TextureManager
{
@@ -32,17 +33,20 @@ public:
void terminate();
void poll();
void clearTexturesCache();
void clearCache();
void liveReload();
void preload(const std::string& fileName) { getTexture(fileName); }
TexturePtr getTexture(const std::string& fileName);
const TexturePtr& getEmptyTexture() { return m_emptyTexture; }
private:
TexturePtr loadPNG(std::stringstream& file);
TexturePtr loadTexture(std::stringstream& file);
std::unordered_map<std::string, TexturePtr> m_textures;
std::vector<AnimatedTexturePtr> m_animatedTextures;
TexturePtr m_emptyTexture;
ScheduledEventPtr m_liveReloadEvent;
};
extern TextureManager g_textures;