SFML community forums

Help => Graphics => Topic started by: Euqcerin on December 17, 2012, 09:26:23 pm

Title: Laggs when drawing.
Post by: Euqcerin on December 17, 2012, 09:26:23 pm
So I'm working on a 2D game and right now I have a problem with drawing out my tiles. It laggs terribly and I don't really know what to do about it.

I got a set of tiles:  Tile * tiles[32][32];

   
for(int i = 0; i < 32; i++)
        {
                for(int j = 0; j < 32; j++)
                {
                        if(tile[j][i]->getPosition().x > (cameraCenter.x-cx-tss) &&
                                tile[j][i]->getPosition().x < (cameraCenter.x+cx+tss) &&
                                tile[j][i]->getPosition().y > (cameraCenter.y-cy-tss) &&
                                tile[j][i]->getPosition().y < (cameraCenter.y+cy+tss)){
                                        renderWindow.draw(tile[j][i]->sprite);
                        }
                }
        }

This is my code when I draw it.

Now I tried drawing when my Tiles wasn't pointer and I had no lagg.

Is there a reason why this is ?

And is there someone who can give some pointers to one that is new in both c++ and SFML.

If you need more of anything, just ask.
Thanks
Title: Re: Laggs when drawing.
Post by: eXpl0it3r on December 17, 2012, 09:46:24 pm
If you don't allocate the space dynamically but statically your tiles will be continues in memory and thus the cache misses of the CPU will be reduced. If you allocate it manually on the heap, you'll get pointers to arrays and there will be more cache misses. If that's the case here isn't really obvious, but it might.

I suggest three things:
Title: Re: Laggs when drawing.
Post by: krzat on December 18, 2012, 02:15:47 am
Don't use sprites for tile-maps but vertex arrays to get skyrocketing performances. ;)
I wonder why SFML doesn't provide something like XNA's SpritBatch. I wrote one for myself(.NET version), and for tilemaps it works just great.

As to the topic, how big are your tiles? 32x32 should fit on the screen.
Try this:
for(int x = 0; i < 32; y++)
    {
        for(int y = 0; y < 32; y++)
        {
                    renderWindow.draw(tile[x][y]->sprite);
        }
    }