Page 1 of 1

A Question about SpriteBatch and Maxsprites

Posted: Fri Jun 28, 2019 1:56 pm
by sphyrth
So this is what I've read in the SpriteBatch Wiki:
01.png
01.png (14.23 KiB) Viewed 5984 times
Because of that, I started coding like this:

Code: Select all

spriteBatch = love.graphics.newSpriteBatch(img, 1)
I just set maxsprites to 1, and let any tile I add grow it. Is it a good programming practice, or should I go with something like this:

Code: Select all

spriteBatch = love.graphics.newSpriteBatch(img, 50, 'static')
Yeah, I read further down the wiki. If I understand it correctly, is the only reasonable way to use a fixed maxsprites is to set usage to 'static'?

Re: A Question about SpriteBatch and Maxsprites

Posted: Fri Jun 28, 2019 3:14 pm
by zorg
The SpriteBatchUsage page says that the 'static' type means "The object will not be modified after initial sprites or vertices are added."

To me, it's not that clear-cut whether that means that one can't even call the set method on sprite objects defined when the batch was created, or just that one can't add/remove sprites to/from the batch, but can modify the existing ones... or whether the usage has absolutely zero to do with changing the amount of sprites in the batch and just with whether you update the existing ones always (once per frame), rarely, or never.

As for whether allowing löve to auto-grow your spritebatches, or pre-defining a max number is better in terms of processing speed or memory size... if it has that large of an impact, you'd best test it yourself, otherwise both should be fine... although i would lean towards just setting it to a number you'd assume you'd never go beyond.

Re: A Question about SpriteBatch and Maxsprites

Posted: Fri Jun 28, 2019 5:05 pm
by KayleMaster
Since it's probably allocating memory, it's better to alocate at the start of the game (select a higher amount of max sprites) than to increment 1 by 1 during the game as it may cause hiccups. However for such a small memory allocation, it may not matter at all, but I'd still go with the first route.

Re: A Question about SpriteBatch and Maxsprites

Posted: Fri Jun 28, 2019 5:38 pm
by Nelvin
Pre 11, the size of the batch was a fixed value, quads added beyond the limit were discarded.

With the current version, the buffer grows dynamically as required and to still offer good performance it does the same as your typical dynamic array/vector does, i.e. it doubles the size whenever more space is required (which results in an average O(1) complexity). The cost for this is, in a worst case, the buffer allocated will be almost twice as much as you need (2*required - 1)

So, if you know the size of your batches, it's more efficient to use the required initial value.

The size does preallocate the buffers but the actual number of rendered quads is defined by the number of quads added, you can change/modify or clear a batch and add different sprites (and also assign a different texture) at any time. I do this all time, the rendering of my game is done by constantly clearing a sprite batch, assigning a texture, adding as much quads as possible from the same texture and then draing the batch (I use 0.10.2, so autobatching was/is not available)