SFML community forums

Help => General => Topic started by: Austin J on April 08, 2014, 07:00:11 pm

Title: A way to cobble sprites?
Post by: Austin J on April 08, 2014, 07:00:11 pm
  I have a map editor, which uses a sprite for each tile. Obviously since it's a map editor it needs to be ready to change the tiles etc. and I just keep all the tiles to themselves.

  However, in the game itself, I don't want to have to do this. The tiles aren't going to need changes or do anything but be displayed really, and so I'd prefer not to have to make that many draw calls (which leads to lower performance). My maps can have a lot of tiles, so it would certainly be beneficial in my opinion to simply have some big (or maybe even just one?) cobbled together textures to reduce the amount of draw calls.

  Is this correct, and if so, is there a means to do so? I haven't been able to find anything on the subject.
Title: Re: A way to cobble sprites?
Post by: eXpl0it3r on April 08, 2014, 07:03:07 pm
You'll want to use a vertex array, see the dedicated tutorial (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php). ;)
Title: Re: A way to cobble sprites?
Post by: Austin J on April 08, 2014, 09:56:20 pm
That's a perfect tutorial, thank's exploitr ;)
Title: Re: A way to cobble sprites?
Post by: Austin J on April 09, 2014, 06:35:08 pm
Erm, actually, I may still have an issue here, sorry :/

  The problem I see with this is that it seems to only work if all of your tiles are within a single spritesheet. For our game this simply was not viable. I.e a tile can load random variants in a spritesheet dedicated to that tile, automatically keeping terrain from looking homogeneous. Using a giant sprite sheet for this wasn't viable.

  Is there a way I can get around this dilemna of using different png's rather than just using texture rects?
Title: Re: A way to cobble sprites?
Post by: zsbzsb on April 09, 2014, 06:42:22 pm
The best you can do is 1 draw call (1 vertex array) per texture.
Title: Re: A way to cobble sprites?
Post by: Jesper Juhl on April 09, 2014, 07:04:42 pm
It doesn't really matter if you have one sprite sheet or a handful or even a few tens of sprite sheets. What matters is that you don't have many tens or hundreds of sprite sheets (textures).
So just split your images into a few - like sprites A through J on one sheet, K through T on another etc - that should be fine.
Or if you want just one for convenience, then look at something like Thor::BigTexture.
Title: Re: A way to cobble sprites?
Post by: Austin J on April 09, 2014, 09:57:50 pm
Joseph, I'm not sure you read what I said with full effort.

We are not going to use a big sprite sheet(s), as it makes what we need/want a lot harder. The only time we use a sprite sheet is when we want random variants of a tile. For example, we have a spritesheet with several versions of an ice tile. Of course it's all loaded as one texture, but when the brush puts ice down, the selected sprites randomly select which version of the ice tile in the sheet to use.

  We will not however, keep every single tile of the entire game in one monstrous sprite sheet, for us that's simply a horrible idea.

  I may just be out of luck here?
Title: Re: A way to cobble sprites?
Post by: Sqasher on April 09, 2014, 11:03:05 pm
It's the simplest way of doing it.

If you want to keep the textures seperated, you could "stitch" them together at runtime. So when the game starts you load all texturefiles, build a big texturefile out of them and use that in combination with a vertex array. You can use the copy function of sf::Image for that. Minecraft uses this approach too.
Title: Re: A way to cobble sprites?
Post by: Austin J on April 09, 2014, 11:34:12 pm
@Squasher, it's not the best way of doing it in our case however.

I'm looking around some more, sf::RenderTexture may be what I need actually.
Title: Re: A way to cobble sprites?
Post by: Insentience on April 13, 2014, 03:04:06 am
I'm looking around some more, sf::RenderTexture may be what I need actually.
Yeah this is the solution I would go for

On loading the map, draw each tile once to a separate RenderTexture, then on each frame redraw this prepared RenderTexture to the window