Undertsanding SpriteBatch, and tilesets vs individual png's

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
2_bit_encryption
Prole
Posts: 3
Joined: Tue Jan 19, 2016 9:18 pm

Undertsanding SpriteBatch, and tilesets vs individual png's

Post by 2_bit_encryption »

I'm at a fork in the road where I'm trying to decide if I want my game to use tilesets (i.e., one large .png of all my tiles), or individual .png's for each tile.

Up to this point I've been using one individual .png for each tile, without issue. For example, if my whole screen should be filled with rows and rows of "brick" tiles, my code would look like

Code: Select all

brickImage = love.graphics.newImage('brick.png')
for y=0,tilesHeight do
    for x=0,tilesWidth do
        love.graphics.draw(brickImage, x * tilePixelWidth, y * tilePixelHeight)
    end
end
I had a table full of all my images, and a table relating each x,y position with an image id, so I'd iterate through each x,y position, see which image id is there, and then draw allImages[imageId]. You get the picture.

Now I'm considering switching to using a SpriteBatch, since Tiled (the program I'm using to make my tile maps) works better with tilesets than individual png images. However, this complicates my love game since now I don't have just a table of images (which is simple) but a SpriteBatch object which then needs a whole table of Quads to pick out tiles. Not only that, but when I create a SpriteBatch, I need to pass in a parameter "maxSprites" which is "The maximum number of sprites that the SpriteBatch can contain at any given time."

* What does that mean? Does that mean the maximum number of times a single sprite can be drawn (i.e., if my batch of sprites only contains one grass tile, which will be drawn 256*256 times to cover the screen, should I put in 1 because it is only holding 1 tile, or 256*256 because it is painting it that many times?)

* Is there any way to just get image objects out of a larger tileset, so I can make my drawing work more similarly to how it did before when everything was an individual .png? I.e., instead of making a spritebatch out of my image and using quads to decide where everything will go, could I just pull out each tile as its own image?

* What happens if I :add() a quad to a SpriteBatch at an x,y location that was already occupied by another tile? If it just replaces the existing one that'd be great, so I could do a sort of "painter's algorithm" style thing by adding each layer lowest to highest to the SpriteBatch, so that the highest layers will always appear above the lowest layers and will never use more than screen_tile_width * screen_tile_height tiles...

Thanks, and sorry for the rambly question. I found the tutorial in the wiki to be extremely obtuse and unhelpful -- took me forever to figure out what it was trying to say, and the example images that weren't even related to the tutorial really threw me off.
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Undertsanding SpriteBatch, and tilesets vs individual png's

Post by zorg »

For your first bullet point question, the latter is the answer, you'll want 256*256 tiles.

For the second, i don't believe so.

For the third, adding to a spritebatch returns a number, the added sprite's id; you can use the :set function to set an already existing sprite's data. So yes, you can use set for that purpose, however, adding sprites to the same location will only stack them on top of each other.

For layers, i'd recommend using more than one spritebatch with the same atlas. (or different ones, depending on what you want to accomplish)
But the batch is drawn out with the order with which the sprites were added, so if you set them but don't modify their positions, it should still work like that, even with just one spritebatch.
That said, you'll need 256*256*z-layers number of maximum sprites then.
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.
2_bit_encryption
Prole
Posts: 3
Joined: Tue Jan 19, 2016 9:18 pm

Re: Undertsanding SpriteBatch, and tilesets vs individual png's

Post by 2_bit_encryption »

Hi zorg, thanks for your answer. You seem to understand this very well, so I'm going to ask just some clarifying questions:

I want to make sure I fully understand how the SpriteBatch works. Is this correct?

Say I have a tileset image, TileSet.png, where all the tiles are the same dimensions (16x16, for instance).

I create an image out of the TileSet with

Code: Select all

tileSetImg = love.graphics.newImage('TileSet.png')
I make Quads that represent specific tiles within the TileSet based on their position and size. Example:

Code: Select all

grassTile = love.graphics.newQuad(x_pos * tileSize, y_pos * tileSize, tileSize, tileSize, tileSetImg:getWidth(), tileSetImg:getHeight())
By now, I can think of grassTile as representing the individual 16x16 grass tile image, in a way.

I create a SpriteBatch, which is what I use to say which quads (which represent individual tiles in the TileSet) are drawn where.

BEFORE, this was pretty much done directly in my draw() function (I'd say

Code: Select all

love.graphics.draw(someTile, at_x, at_y) to draw an image (tile) at x,y)
NOW, we're giving this information to the SpriteBatch instead, and then simply passing the SpriteBatch to the draw function in one fell swoop.

So we'd say

Code: Select all

spriteBatch:add(grassTile, at_x, at_y)
, where grassTile is a quad indexing a tile in the TileSet image, and when we do

Code: Select all

love.graphics.draw(spriteBatch)
that will do what we want to do.

Sorry for writing this out so verbosely, I just want to make sure I fully understand how this is working before I invest the time into converting everything to a SpriteBatch system.
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Undertsanding SpriteBatch, and tilesets vs individual png's

Post by zorg »

I think yes, what you wrote is the gist of it! :3
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest