SFML community forums
Help => General => Topic started by: GamDev on June 07, 2018, 05:44:35 pm
-
Hello, please tell me what I'm doing wrong
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;
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);
}
for(unsigned int x = 0; x < 100; ++x)
for(unsigned int y = 0; y < 100; ++y)
{
switch(TileMap [ x ] [ y ] )
{
case 1: 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);
}
//==================================================================================================
on 1 image my tileSet, the second result of the brown I'm trying to reach, and 3 that turns out
Tell me please how can I put sprite on the sprite?
-
Easiest and most likely best looking solution is to create the tile in an image editor of your choosing.
If you grey part of the image is transparent, you could alpha blend them, but that will leave you with double as strong corner pieces.
Bet shaders can work some magic somehow, but at this point it's just overkill.
Create a tilesheet with all the images and the rotate accordingly when needed.