When loading tiled map, I draw it once on a RenderTexture instance and then I copy its texture into sf::Texture instance which is then drawn on every frame, beacuse that seemed to be very fast and portable way to do it (read: tested & works on shit hardware). But then I figured i need a way to hide part of map from player. Since I could no longer choose what tiles to draw on each frame.
And this is what I did:
class Game
{
//...
sf::Sprite HideMapHack[24][32];
};
//...
sf::Texture HideHack;
HideHack.LoadFromFile("Tiles/Hack.png"); //Hack.png is 32x32 completly black tile
for(int y=0; y<24; ++y)
{
for(int x=0; x<32; ++x)
{
HideMapHack[y][x].SetTexture(HideHack);
HideMapHack[y][x].SetPosition(x*32, y*32);
}
}
And on every frame, after drawing map, enemies and stuff, I do this:
inline int Distance(int x1, int y1, int x2, int y2)
{
return (int)sqrt((float)(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
//...
void DrawAll()
{
//...
for(int y=0; y<24; ++y)
{
for(int x=0; x<32; ++x)
{
if(Distance(Player.GetX()/32, Player.GetY()/32, x, y) > 3)
{
Window.Draw(HideMapHack[y][x]);
}
}
}
}
Was that a smart idea? Is there a better way to do this?