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

Author Topic: Sprite batching in SFML?  (Read 14332 times)

0 Members and 1 Guest are viewing this topic.

Flyverse

  • Newbie
  • *
  • Posts: 9
    • View Profile
Sprite batching in SFML?
« on: October 30, 2014, 03:56:25 pm »
Hello!

I'm currently trying to learn C++ (I programmed in Java before), and wanted to use SFML to create some simple graphical programs. To help me get comfortable with the language, I thought about creating some simple, tiny games, one of them being a game where objects are falling down from the top of the screen and you have to "catch" them. So, while writing the code, I noticed one thing: Apparently you have to make a draw call everytime you want a texture rendered. In my opinion this could be quite a heavy task for the computer, so my question is: Is there something like a SpriteBatch in SFML? (In Java I used a library called LibGDX where you had to use a class called "SpriteBatch". On every render-call, you would use the SpriteBatch#begin() method, make your draw calls, and then call SpriteBatch#end(). Is there something similar in SFML?)

Kind regards

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sprite batching in SFML?
« Reply #1 on: October 30, 2014, 04:00:54 pm »
In SFML, you implement "sprite batching" by drawing a properly-designed vertex array.  See http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php for all the details.

There is no feature in SFML that takes a bunch of sprites or draw calls and batches them for you; as far as I know this isn't actually possible unless you can somehow prove all the sprites/draw calls operate on exactly the same texture, which is the same restriction that vertex arrays have.
« Last Edit: October 30, 2014, 04:03:35 pm by Ixrec »

Flyverse

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprite batching in SFML?
« Reply #2 on: October 30, 2014, 04:05:13 pm »
So you can't batch all of your draw calls at once if you have different textures? Isn't that heavily affecting performance?

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Sprite batching in SFML?
« Reply #3 on: October 30, 2014, 04:35:04 pm »
At SFML, we believe that the developer should have an understanding of the work that is being done by the library when such "optimizations" are undertaken. If we have the feeling that we would have to impose too many artificial limitations on developers just so that some other developers can save writing a bit of code themselves, we often just let them write said facility on top of SFML.

Sprite batching is no different. Many have requested this in the past, and I might be wrong, but I have the feeling not many understand how it really has to work under the hood. Like Ixrec said, you can batch quads together into vertex arrays, and yes, you can do so even with multiple sets of texture data. I explicitly say texture data and not just texture, because logically, the texture object is what the GPU uses to source texture data as you already know. However, nothing prevents you from batching multiple texture data sets into a single texture as well. This is commonly known as texture atlasing. SFML already does this with its text rendering, hence it shouldn't be too big of a problem for you to implement this for sprites as well.

I know what you are going to say now, why doesn't SFML do this for you since you think many might need this and this is what a multimedia library is for. I already mentioned the answer above. Different people will need different kinds of sprite batching. Writing an efficient sprite batcher is very application specific and having to account for every possible thing that developers might use it for would probably end up making it slower than simply drawing the sprites yourself.

It really isn't that hard to write your own, assuming you understand how one works that is. Try it out and you can always come back here if you have questions.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sprite batching in SFML?
« Reply #4 on: October 30, 2014, 04:36:19 pm »
Well, yes, switching textures when it's not necessary hurts performance, but it's not possible to batch draw calls to multiple textures anyway, so I'm not sure what the point of the question is.  The general strategy is to make a spritesheet or tilemap, i.e. one texture that contains all the little textures you want, and then use that to make it possible to do everything in the same draw call.  I don't know what LibGDX does, but if it appears to allow multiple textures in a batch then I assume it's actually performing multiple draw calls under the hood.  Hopefully binary will show up soon to give a more detailed explanation.  Yep, he beat me to it.

Flyverse

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprite batching in SFML?
« Reply #5 on: October 30, 2014, 05:24:35 pm »
Thanks for your answers.

Thing is - I don't know how to do it. I rarely had to do with stuff that is a little bit low-level, so this is all new to me. And I have no idea how I can learn this (without needing years).

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sprite batching in SFML?
« Reply #6 on: October 30, 2014, 05:27:17 pm »
As I linked before, http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php shows you exactly how to do it, with examples.  Learning this takes maybe a couple hours tops (assuming you already know C++ and the easier bits of SFML).

InverseSec

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite batching in SFML?
« Reply #7 on: February 20, 2015, 10:54:40 pm »
Well, yes, switching textures when it's not necessary hurts performance, but it's not possible to batch draw calls to multiple textures anyway, so I'm not sure what the point of the question is.  The general strategy is to make a spritesheet or tilemap, i.e. one texture that contains all the little textures you want, and then use that to make it possible to do everything in the same draw call.  I don't know what LibGDX does, but if it appears to allow multiple textures in a batch then I assume it's actually performing multiple draw calls under the hood.  Hopefully binary will show up soon to give a more detailed explanation.  Yep, he beat me to it.

I know this thread is a bit old, but I really needed help on this part.
I've got one spritesheet with a bunch of images for the game characters, but I have no idea how to draw everything that uses this spritesheet in a single draw call. Can you point me to a resource?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Sprite batching in SFML?
« Reply #8 on: February 20, 2015, 11:15:40 pm »
Instead of bumping two different threads which seem related to your problem, why don't you create your own thread? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

InverseSec

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite batching in SFML?
« Reply #9 on: February 20, 2015, 11:52:45 pm »
Sorry, I thought it might be an easier way to get an answer

 

anything