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

Author Topic: Laggs when drawing.  (Read 1518 times)

0 Members and 1 Guest are viewing this topic.

Euqcerin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Laggs when drawing.
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Laggs when drawing.
« Reply #1 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:
  • Don't use arrays, but vectors.
  • Don't use multi-dimensional containers, but use one big container and calculate the multi-dimensions on your own.
  • Don't use sprites for tile-maps but vertex arrays to get skyrocketing performances. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Laggs when drawing.
« Reply #2 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);
        }
    }
 
SFML.Utils - useful extensions for SFML.Net

 

anything