Hi I've been make this game about a fortnight now and I've been having this problem with the tiles/blocks being rendered when the view is moved.
I've been fixing the view to the character but when the character/view is moved in the y-axis (and sometimes the x-axis but very rare) the tiles seem have these lines:
I've figured out that these lines are cause by the texturerect of the tile (which is a sprite) is being moved cause some of the rest of the texture to be show in it. The Blue lines are where texture is transparent and the brown lines on some of the grass tiles, the texture is showing another tile.
I would show some minimal code if I could but I cannot seem to replicate this on a small level so here are snippets of my code:
Place tiles into sprites
m_tiles => std::Vector<sf::Sprite>
terrain2.png is a 256x256 png containing 256 tiles (akin to older version of minecraft)
for (uint8_t y = 0; y < 16; y++)
{
for (uint8_t x = 0; x < 16; x++)
{
sf::Sprite stemp;
stemp.setTexture(textureManager.getTexture("res/image/terrain2.png"));
stemp.setTextureRect( sf::IntRect(16*x, 16*y, 16, 16) );
stemp.setOrigin(8, 8);
m_tiles.push_back(stemp);
}
}
Display Tiles
mapLevel => std::vector< std::vector<uint8_t> > this way I call a tile like mapLevel[x][y]
// Only display what is on screen
int drawRangeX = window.getSize().x/64;
int drawRangeY = window.getSize().y/64;
// Add one to draw range
drawRangeX++;
drawRangeY++;
for (unsigned int y = std::max((m_character.getPosition().y/16.0f)-drawRangeY, 0.0f);
y < std::min((m_character.getPosition().y/16.0f)+drawRangeY, (float)(gridSize.y-1)); y++)
{
for (unsigned int x = std::max((m_character.getPosition().x/16.0f)-drawRangeX, 0.0f);
x < std::min((m_character.getPosition().x/16.0f)+drawRangeX, (float)(gridSize.x-1)); x++)
{
uint8_t tileId = mapLevel[x][y];
if (tileId != 0x00)
{
switch (tileId) // used to "randomize" tiles or get specific tile
{
case 0x05: // Grass
tileId += (x+y)%2;
break;
case 0x30: // Ore
tileId += (3*x + 2*y)%2;
break;
case 0x0C: // Leaves
if (mapLevel[x-1][y] != 0x0C && mapLevel[x][y-1] != 0x0C)
tileId += 1;
else if (mapLevel[x+1][y] != 0x0C && mapLevel[x][y-1] != 0x0C)
tileId += 2;
break;
default:
break;
}
sf::Sprite temp = m_tiles[tileId];
temp.setPosition(x*16, y*16);
if (layerBrightness != 0xFF)
temp.setColor(sf::Color(layerBrightness, layerBrightness, layerBrightness));
window.draw(temp);
}
}
}