Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Performance question  (Read 1351 times)

0 Members and 1 Guest are viewing this topic.

Krofna

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Performance question
« on: February 19, 2012, 03:47:12 pm »
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:

Code: [Select]

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:
Code: [Select]
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?

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Performance question
« Reply #1 on: February 26, 2012, 12:41:35 am »
well, since no one more qualified is responding, I've seen tile hiding be used before, and as long as your not hiding more than 1000-2000 at a time, it won't get laggy (depending on what processor you have), a better idea is to only create the black squares over the tiles once that part of the map is within the screen, then remove it when it is in range of the player, that way you will only have to loop thru the tiles within your screen and not the tiles within the entire map, and you could use a 2-dimensional array to store which tiles are discovered or not
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

 

anything