Repeating background - Quad or SpriteBatch?

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
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Repeating background - Quad or SpriteBatch?

Post by msilvestro »

Hi everybody!

I'm trying to render a background made of a repeated tile (let's say 100x100px) in a great square (let's say 3000x3000px) that is the ground where the player walks. I've got four types of tiles, but I display just one at a time! For example, for a while there is a sandy ground, then a grassy ground, and so on. But for a while there is only one type of tile repeated over and over, and then there is a little transition between a place to another, and then the second tile repeated over and over.

Basically, I'm achieving this by making four images (100x100) and a great quad (3000x3000) in which the images are repeated.
The code is roughly this:

Code: Select all

function love.load()
blockimage = {}
for i = 1, 4 do
	blockimage[i] = love.graphics.newImage("graphics/block"..i..".png")
	blockimage[i]:setWrap("repeat", "repeat") 
	blockquad[i] = love.graphics.newQuad(0, 0, 3000, 3000, 100, 100)
end
ind = 1
end

function love.draw()
	love.graphics.draw(blockimage[ind], blockquad[ind], 0, 300)
end
And it's working quite well... but now I want to put all the tiles in a single image and then separate them, but with this implementation is not possible, right? Should I use SpriteBatch? (that I haven't yet understood very well)

And, generally... to display four tipes of different but uniform background, is it better (performance-wise) to use Quads or SpriteBatch? Thanks!
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Repeating background - Quad or SpriteBatch?

Post by micha »

msilvestro wrote: And it's working quite well... but now I want to put all the tiles in a single image and then separate them, but with this implementation is not possible, right? Should I use SpriteBatch? (that I haven't yet understood very well)

And, generally... to display four tipes of different but uniform background, is it better (performance-wise) to use Quads or SpriteBatch? Thanks!
Interesting case. I have never thought of that. I agree, that wrapping an image and having multiple tiles in one image file is impossible. The former means that you create a quad that is bigger than the image file and the latter means you create one that is smaller. That won't work at the same time.

I recommend using a SpriteBatch. Have a look at the tutorial on the wiki. A SpriteBatch is basically an image together with a collection of quads and positions to draw them. That will do the job.
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Repeating background - Quad or SpriteBatch?

Post by msilvestro »

micha wrote: Interesting case. I have never thought of that. I agree, that wrapping an image and having multiple tiles in one image file is impossible. The former means that you create a quad that is bigger than the image file and the latter means you create one that is smaller. That won't work at the same time.

I recommend using a SpriteBatch. Have a look at the tutorial on the wiki. A SpriteBatch is basically an image together with a collection of quads and positions to draw them. That will do the job.
Thank you! I'm trying to use a SpriteBatch, will post the result of my work as soon as I finish it!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests