A Question about SpriteBatch and Maxsprites

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sphyrth
Party member
Posts: 108
Joined: Mon Jul 07, 2014 11:04 am
Contact:

A Question about SpriteBatch and Maxsprites

Post by sphyrth » Fri Jun 28, 2019 1:56 pm

So this is what I've read in the SpriteBatch Wiki:
01.png
01.png (14.23 KiB) Viewed 3192 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'?

User avatar
zorg
Party member
Posts: 2654
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: A Question about SpriteBatch and Maxsprites

Post by zorg » Fri Jun 28, 2019 3:14 pm

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

KayleMaster
Party member
Posts: 208
Joined: Mon Aug 29, 2016 8:51 am

Re: A Question about SpriteBatch and Maxsprites

Post by KayleMaster » Fri Jun 28, 2019 5:05 pm

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.

Nelvin
Citizen
Posts: 87
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: A Question about SpriteBatch and Maxsprites

Post by Nelvin » Fri Jun 28, 2019 5:38 pm

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)

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests