cache sprites masks

master
Eduardo Bart 13 years ago
parent 7b13161036
commit 0df7e2ed6a

@ -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,37 +96,36 @@ 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();
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;
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];
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]));
}
//TODO: cache sprites mask into a texture
return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
}
return texture;
return sprite.masks[mask];
} else
return sprite.texture;
}

@ -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;
};

Loading…
Cancel
Save