Created
September 12, 2012 00:57
-
-
Save benui-dev/3703388 to your computer and use it in GitHub Desktop.
How to use Löve2d's SpriteBatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.load() | |
-- Load our image we want to draw many times | |
image = love.graphics.newImage("dirt.png") | |
-- The number of tiles we want to draw is pretty much the number | |
-- that will fit on the screen | |
maxX = math.ceil(love.graphics.getWidth() / image:getWidth()) + 2 | |
maxY = math.ceil(love.graphics.getHeight() / image:getHeight()) + 2 | |
local size = maxX * maxY | |
print(size) | |
-- Set up a sprite batch with our single image and the max number of times we | |
-- want to be able to draw it. Later we will call spriteBatch:add() to tell | |
-- Love where we want to draw our image | |
spriteBatch = love.graphics.newSpriteBatch(image, size) | |
setupSpriteBatch() | |
end | |
function love.update(dt) | |
-- If we were using a moving background, we would want to only call | |
-- setupSpriteBatch when its range or position changed | |
setupSpriteBatch() | |
end | |
function setupSpriteBatch() | |
spriteBatch:clear() | |
-- Set up (but don't draw) our images in a grid | |
for y = 0, maxY do | |
for x = 0, maxX do | |
-- Convert our x/y grid references to x/y pixel coordinates | |
local xPos = x * image:getWidth() | |
local yPos = y * image:getHeight() | |
-- Add the image we previously set to this point | |
spriteBatch:add(xPos, yPos) | |
end | |
end | |
end | |
function love.draw() | |
-- Draw the spriteBatch with only one call! | |
love.graphics.setColor(255,255,255) | |
love.graphics.draw(spriteBatch) | |
-- Draw FPS in the bottom right corner | |
love.graphics.setColor(255,0,0) | |
love.graphics.print(tostring(love.timer.getFPS()), love.graphics.getWidth()-30, love.graphics.getHeight()-20) | |
end |
@evanbrierton for loops auto-increment their iteration variables, that's why they exist. (lines 29, 30)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tried this out and it works perfectly but I'm left wondering what is incrementing x and y within your setupSpriteBatch() function?