cache sprites masks
This commit is contained in:
parent
7b13161036
commit
0df7e2ed6a
|
@ -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,37 +96,36 @@ 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];
|
||||||
RGBA whiteColor = Color::white.rgba();
|
RGBA whiteColor = Color::white.rgba();
|
||||||
RGBA alphaColor = Color::alpha.rgba();
|
RGBA alphaColor = Color::alpha.rgba();
|
||||||
|
|
||||||
// convert pixels
|
// convert pixels
|
||||||
// masked color -> white color
|
// masked color -> white color
|
||||||
// any other color -> alpha color
|
// any other color -> alpha color
|
||||||
for(int i=0;i<4096;i+=4) {
|
for(int i=0;i<4096;i+=4) {
|
||||||
RGBA& currentColor = *(RGBA*)&pixels[i];
|
RGBA& currentColor = *(RGBA*)&pixels[i];
|
||||||
if(currentColor == maskColor)
|
if(currentColor == maskColor)
|
||||||
currentColor = whiteColor;
|
currentColor = whiteColor;
|
||||||
else
|
else
|
||||||
currentColor = alphaColor;
|
currentColor = alphaColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprite.masks[mask] = TexturePtr(new Texture(32, 32, 4, &pixels[0]));
|
||||||
}
|
}
|
||||||
|
return sprite.masks[mask];
|
||||||
//TODO: cache sprites mask into a texture
|
} else
|
||||||
return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
|
return sprite.texture;
|
||||||
}
|
|
||||||
|
|
||||||
return texture;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue