SpriteBatch basics (for characters, specifically)?

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
Esbee
Prole
Posts: 4
Joined: Wed Nov 08, 2017 8:17 pm

SpriteBatch basics (for characters, specifically)?

Post by Esbee »

Hey there, I've got a question: how in the world do you use a spritebatch to animate your characters?

The flow as I understand it is (assume a single controllable character on a blank scene):

- create newImage from your spritesheet-style png/etc.
- create newSpriteBatch with the newImage
- create all the newQuad stuff for coordinates of each frame

And here's where I get a little confused. Would I then set it up so that each time the character needs to animate/update position/etc. I clear out the entire spritebatch and add the new, single frame (assuming no layering is necessary) back (and then redraw it, obviously)? For multiple characters am I essentially making the entire spritebatch an overlay on the screen with *all* characters added to it per update (essentially drawing one big picture with updated positions/animations)? But wait... if that's true, wouldn't it be best to make the entire viewport (characters, environment, projectiles, effects, etc) in a single spritebatch? Or am I just totally misunderstanding the proper use here...

As you can tell, I'm a little fuzzy on spritebatches in general. I understand the spritesheet concept (I've used them for years in web dev and whatnot), but the love2d implementation is something I don't understand just yet... especially when it comes to optimization compared to drawing everything individually.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: SpriteBatch basics (for characters, specifically)?

Post by KayleMaster »

SpriteBatches shine in things that don't change, like terrain/land.
However it's still fine to use for other things. You don't have to clear the entire spritebatch, you can only change the sprite you wanna animate:
https://love2d.org/wiki/SpriteBatch:set
Making your spritebatch the size of your world, especially if you're modifying it every 2nd tick, may not be a good idea. You can try, though.
A lot of things you just have to profile to see if they work.
Usually I'd go with chunking so you can minimize the amount of drawing outside of the screen.
Yes, it's gonna be 1 draw call only, but you're still drawing pixels, and it's a matter of time before you run out of fill rate.
There's also this: https://love2d.org/wiki/SpriteBatchUsage , which you can apply to:
spriteBatch = love.graphics.newSpriteBatch( image, maxsprites, usage )
Esbee
Prole
Posts: 4
Joined: Wed Nov 08, 2017 8:17 pm

Re: SpriteBatch basics (for characters, specifically)?

Post by Esbee »

Well for world stuff I'm going with the general idea outlined here https://love2d.org/wiki/Tutorial:Effici ... _Scrolling (though I'll be tweaking it a bit to be more of a "smart scrolling" system with a fair amount of deadzone in the middle of the playfield). I just wasn't sure if it was appropriate to be tossing all my characters/etc. into spritebatch usage as well, or how that was even accomplished properly.

And derp! Somehow the "set" usage never clicked in my mind, which is a large part of the confusion I was having with thinking "hmm.. how is redoing the entire spritebatch anytime something changes a good idea?". Thank you!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 86 guests