cache sprites masks

This commit is contained in:
Eduardo Bart 2011-08-19 11:23:02 -03:00
parent 0df7e2ed6a
commit 09548488fa
2 changed files with 26 additions and 21 deletions

View File

@ -88,24 +88,9 @@ TexturePtr SpriteManager::loadSpriteTexture(int id)
return TexturePtr(new Texture(32, 32, 4, pixels)); return TexturePtr(new Texture(32, 32, 4, pixels));
} }
TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask) TexturePtr SpriteManager::loadSpriteMask(TexturePtr spriteTex, SpriteMask mask)
{ {
if(id == 0) auto pixels = spriteTex->getPixels();
return g_graphics.getEmptyTexture();
assert(id > 0 && id <= m_spritesCount);
// load sprites on demand
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) {
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() }; static RGBA maskColors[4] = { Color::red.rgba(), Color::green.rgba(), Color::blue.rgba(), Color::yellow.rgba() };
RGBA maskColor = maskColors[mask]; RGBA maskColor = maskColors[mask];
@ -123,8 +108,27 @@ TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
currentColor = alphaColor; currentColor = alphaColor;
} }
sprite.masks[mask] = TexturePtr(new Texture(32, 32, 4, &pixels[0])); return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
} }
TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
{
if(id == 0)
return g_graphics.getEmptyTexture();
assert(id > 0 && id <= m_spritesCount);
// load sprites on demand
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) {
if(!sprite.masks[mask])
sprite.masks[mask] = loadSpriteMask(sprite.texture, mask);
return sprite.masks[mask]; return sprite.masks[mask];
} else } else
return sprite.texture; return sprite.texture;

View File

@ -24,6 +24,7 @@ public:
private: private:
TexturePtr loadSpriteTexture(int id); TexturePtr loadSpriteTexture(int id);
TexturePtr loadSpriteMask(TexturePtr spriteTex, SpriteMask mask);
uint32 m_signature; uint32 m_signature;
uint16 m_spritesCount; uint16 m_spritesCount;