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,6 +88,29 @@ TexturePtr SpriteManager::loadSpriteTexture(int id)
return TexturePtr(new Texture(32, 32, 4, pixels)); return TexturePtr(new Texture(32, 32, 4, pixels));
} }
TexturePtr SpriteManager::loadSpriteMask(TexturePtr spriteTex, SpriteMask mask)
{
auto pixels = spriteTex->getPixels();
static RGBA maskColors[4] = { Color::red.rgba(), Color::green.rgba(), Color::blue.rgba(), Color::yellow.rgba() };
RGBA maskColor = maskColors[mask];
RGBA whiteColor = Color::white.rgba();
RGBA alphaColor = Color::alpha.rgba();
// convert pixels
// masked color -> white color
// any other color -> alpha color
for(int i=0;i<4096;i+=4) {
RGBA& currentColor = *(RGBA*)&pixels[i];
if(currentColor == maskColor)
currentColor = whiteColor;
else
currentColor = alphaColor;
}
return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
}
TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask) TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
{ {
if(id == 0) if(id == 0)
@ -104,27 +127,8 @@ TexturePtr SpriteManager::getSpriteTexture(int id, SpriteMask mask)
// to avoid massive texture allocations // to avoid massive texture allocations
if(mask != SpriteMaskNone) { if(mask != SpriteMaskNone) {
if(!sprite.masks[mask]) { if(!sprite.masks[mask])
auto pixels = sprite.texture->getPixels(); sprite.masks[mask] = loadSpriteMask(sprite.texture, mask);
static RGBA maskColors[4] = { Color::red.rgba(), Color::green.rgba(), Color::blue.rgba(), Color::yellow.rgba() };
RGBA maskColor = maskColors[mask];
RGBA whiteColor = Color::white.rgba();
RGBA alphaColor = Color::alpha.rgba();
// convert pixels
// masked color -> white color
// any other color -> alpha color
for(int i=0;i<4096;i+=4) {
RGBA& currentColor = *(RGBA*)&pixels[i];
if(currentColor == maskColor)
currentColor = whiteColor;
else
currentColor = alphaColor;
}
sprite.masks[mask] = TexturePtr(new Texture(32, 32, 4, &pixels[0]));
}
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;