SFML community forums
Help => General => Topic started by: GamDev on June 07, 2018, 07:48:31 pm
-
Hello, please tell me it's possible to do this:
I have an image size of 128x64, that is 2 sprite.
I draw them like this:
class DrawTileMap : public sf::Drawable
{
public:
void load (const Texture tileset, int ( &TileMap )[100][100]);
void update (int ( &TileMap )[100][100]);
private:
virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const;
VertexArray m_vertices;
Texture m_tileset;
Vector2u tileSize = {64, 64};
};
//==================================================================================================
void DrawTileMap::load(const Texture tileset, int ( &TileMap )[100][100])
{
m_tileset = tileset;
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(100 * 100 * 4);
}
//==================================================================================================
void DrawTileMap::update(int ( &TileMap )[100][100])
{
int tu = 0;
int tv = 0;
for(unsigned int x = 0; x < 100; ++x)
for(unsigned int y = 0; y < 100; ++y)
{
switch(TileMap [ x ] [ y ] )
{
case 1: tu = 0;tv = 0;break;
case 2: tu = 1;tv = 0;break;
default:tu = 0;tv = 0;break;
}
sf::Vertex* tile64 = &m_vertices[(x + y * 100) * 4];
tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);
tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
}
//==================================================================================================
void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.texture = &m_tileset;
target.draw(m_vertices, states);
}
//==================================================================================================
Tell me, please, is it possible to use the shader only on the second sprite inside the DrawTileMap class?
-
As I see it, you would need someway of telling the shader when and when not to be applied. One idea, if you don't need the alpha channels of the texture, is to make one of them fully transparent (still keeping the colours of course!). Then, in the shader, if check the texture's alpha channel and if it's higher or lower than half, apply the effect or pass the pixel unaffected.