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

View File

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