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

Author Topic: How to draw many shapes efficiently?  (Read 5728 times)

0 Members and 2 Guests are viewing this topic.

Piorjade

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to draw many shapes efficiently?
« on: May 15, 2018, 04:04:06 pm »
Soo I'm currently developing a "virtual retro-like computer" to get into SFML and C/C++.
The problem currently is though that I want to draw pseudo-pixels (basically just many small rectangles that the user can manipulate, just like pixels) and if I go to a resolution of 200x80 (~16k pseudo-pixels) the performance drops to 60FPS (with a resolution of 51x19 I had about 400-500).

Is there any way that I can improve the performance? Or is it as fast as it can get with 16k shapes?

I also wanted to make the user decide what the resolution is via configuration file but if 200x80 gets to 55-60FPS (with a GTX 960, 16GB RAM and a FX-8350) I'll probably hard-code that..

Also, the way how I store these pixels is that the pixels are objects containing information about their color and their actual shape object and they are all stored in a 2-dimensional array, which gets iterated through to draw every pixel each frame, if that is of any meaning.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to draw many shapes efficiently?
« Reply #1 on: May 15, 2018, 04:19:41 pm »
Use std::vector<sf::Vertex> (or sf::VertexArray) and draw quads. So you need four vertices per quad.
That way you'll end up with one draw call.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Piorjade

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to draw many shapes efficiently?
« Reply #2 on: May 15, 2018, 04:21:29 pm »
And this vector should store every pixel?
So I'll basically just push_back 4 vertices per pixel into that one vector, right?

Thanks for your reply btw

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to draw many shapes efficiently?
« Reply #3 on: May 15, 2018, 04:27:11 pm »
Yes. Instead of having a rectangle shape, you just define each point of the rectangle yourself in the right order and add it to the vector, which you then can draw with one draw call (check the documentation).

To get familiar with the concept you should also read the official tutorial on vertex arrays.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Piorjade

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to draw many shapes efficiently?
« Reply #4 on: May 15, 2018, 04:27:58 pm »
Alright, thanks dude!  :)

EDIT: Yeah I got up to 1500-2000FPS now, thank you very much!

Guess I shouldn't be so scared of more lower-level stuff :)
« Last Edit: May 15, 2018, 04:37:07 pm by Piorjade »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to draw many shapes efficiently?
« Reply #5 on: May 15, 2018, 05:44:38 pm »
Glad it worked out :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything