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

Author Topic: Efficiency question  (Read 2382 times)

0 Members and 1 Guest are viewing this topic.

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Efficiency question
« on: April 26, 2015, 12:28:58 am »
So im drawing level to screen, and cpu usage is kinda high i still have a lot of refactoring to do but since im lacking knowledge about sfml, i wonder if drawing part is a problem?
This is my draw function:
(click to show/hide)
This feels ineffiecient, how can i improve it?
Thanks  ;D

Sub

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Efficiency question
« Reply #1 on: April 26, 2015, 01:00:09 am »
Are you limiting the frame rate?  If not, try calling either .setFramerateLimit(60) or .setVerticalSyncEnabled(true) on your renderwindow.

It's probably not a problem, but you could experience some lag if you have too many draw calls.  Taken from the SFML tutorial

Quote
SFML provides simple classes for the most common 2D entities. And while more complex entities can easily be created from these building blocks, it isn't always the most efficient solution. For example, you'll reach the limits of your graphics card very quickly if you draw a large number of sprites. The reason is that performance depends in large part on the number of calls to the draw function. Indeed, each call involves setting a set of OpenGL states, resetting matrices, changing textures, etc. All of this is required even when simply drawing two triangles (a sprite). This is far from optimal for your graphics card: Today's GPUs are designed to process large batches of triangles, typically several thousand to millions.

To fill this gap, SFML provides a lower-level mechanism to draw things: Vertex arrays. As a matter of fact, vertex arrays are used internally by all other SFML classes. They allow for a more flexible definition of 2D entities, containing as many triangles as you need. They even allow drawing points or lines.

I didn't really look at the code too much, but every frame you're declaring a vector and pushing the sprites into the vector to be drawn after the loop.  Why don't you just skip the indirectness and draw the sprites directly in the loop? 
« Last Edit: April 26, 2015, 01:05:39 am by Sub »

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Efficiency question
« Reply #2 on: April 26, 2015, 10:09:54 am »
Thanks for fast reply, i think problem is amount of sprites i draw, ill try to make it work with vertex arrays.

grok

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • Email
Re: Efficiency question
« Reply #3 on: April 26, 2015, 10:22:47 am »
don't draw the sprites which are not visible. the current version draws them all, no matter, whether or not they fall into the game view.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Efficiency question
« Reply #4 on: April 26, 2015, 11:47:51 am »
About std::move:
1. it's totally useless in this context (SFML classes don't support move semantics yet)
2. it's dangerous, since you reuse instances after moving them (remember: moving something leaves it in an unspecified state, all you can safely do is destroy or assign it)
Laurent Gomila - SFML developer

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Efficiency question
« Reply #5 on: April 26, 2015, 12:45:59 pm »
Thanks, didn't know that.  But otherwise is this ok way to draw sprites, (i'll also draw only those within game view)? Im drawing around 60, only 5 textures tho, or should i use vertex array for this? :D
« Last Edit: April 26, 2015, 12:57:22 pm by aegir »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Efficiency question
« Reply #6 on: April 26, 2015, 02:57:17 pm »
Quote
Im drawing around 60, only 5 textures tho, or should i use vertex array for this?
This is ridiculously low, so do what Sub said (enable v-sync or framerate limit) and then have a look again at the CPU consumption. 100% CPU doesn't mean that your code is inefficient, just that there is no pause in it.
Laurent Gomila - SFML developer

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Efficiency question
« Reply #7 on: April 26, 2015, 03:02:31 pm »
Thank you so much :)

 

anything