cache sprites masks

This commit is contained in:
Eduardo Bart 2011-08-19 11:18:24 -03:00
parent 7b13161036
commit 0df7e2ed6a
2 changed files with 32 additions and 28 deletions

View File

@ -26,7 +26,7 @@ void SpriteManager::unload()
m_signature = 0;
}
TexturePtr SpriteManager::loadSprite(int id)
TexturePtr SpriteManager::loadSpriteTexture(int id)
{
m_fin.seekg(((id-1) * 4) + 6, std::ios_base::beg);
@ -96,17 +96,16 @@ TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
assert(id > 0 && id <= m_spritesCount);
// load sprites on demand
TexturePtr texture = m_sprites[id-1];
if(!texture) {
texture = loadSprite(id);
m_sprites[id-1] = texture;
}
Sprite& sprite = m_sprites[id-1];
if(!sprite.texture)
sprite.texture = loadSpriteTexture(id);
//TODO: release unused sprites textures after X seconds
// to avoid massive texture allocations
if(mask != SpriteMaskNone) {
auto pixels = texture->getPixels();
if(!sprite.masks[mask]) {
auto pixels = sprite.texture->getPixels();
static RGBA maskColors[4] = { Color::red.rgba(), Color::green.rgba(), Color::blue.rgba(), Color::yellow.rgba() };
RGBA maskColor = maskColors[mask];
@ -124,9 +123,9 @@ TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
currentColor = alphaColor;
}
//TODO: cache sprites mask into a texture
return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
sprite.masks[mask] = TexturePtr(new Texture(32, 32, 4, &pixels[0]));
}
return texture;
return sprite.masks[mask];
} else
return sprite.texture;
}

View File

@ -4,6 +4,11 @@
#include "declarations.h"
#include <framework/graphics/texture.h>
struct Sprite {
TexturePtr texture;
TexturePtr masks[4];
};
class SpriteManager
{
public:
@ -18,12 +23,12 @@ public:
TexturePtr getSpriteTexture(int id, SpriteMask mask = SpriteMaskNone);
private:
TexturePtr loadSprite(int id);
TexturePtr loadSpriteTexture(int id);
uint32 m_signature;
uint16 m_spritesCount;
std::stringstream m_fin;
std::vector<TexturePtr> m_sprites;
std::vector<Sprite> m_sprites;
TexturePtr m_transparentSprite;
};