love.graphics.newSpriteBatch

Creates a new SpriteBatch object.

O.png This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused!  



Function

Synopsis

spriteBatch = love.graphics.newSpriteBatch( image, maxsprites )

Arguments

Image image
The Image to use for the sprites.
number maxsprites (1000)
The maximum number of sprites that the SpriteBatch can contain at any given time. Since version 11.0, additional sprites added past this number will automatically grow the spritebatch.

Returns

SpriteBatch spriteBatch
The new SpriteBatch.

Function

Available since LÖVE 0.8.0
This variant is not supported in earlier versions.

Synopsis

spriteBatch = love.graphics.newSpriteBatch( image, maxsprites, usage )

Arguments

Image image
The Image to use for the sprites.
number maxsprites (1000)
The maximum number of sprites that the SpriteBatch can contain at any given time. Since version 11.0, additional sprites added past this number will automatically grow the spritebatch.
SpriteBatchUsage usage ("dynamic")
The expected usage of the SpriteBatch. The specified usage mode affects the SpriteBatch's memory usage and performance.

Returns

SpriteBatch spriteBatch
The new SpriteBatch.

Function

Available since LÖVE 0.9.1
This variant is not supported in earlier versions.

Synopsis

spriteBatch = love.graphics.newSpriteBatch( texture, maxsprites, usage )

Arguments

Texture texture
The Image or Canvas to use for the sprites.
number maxsprites (1000)
The maximum number of sprites that the SpriteBatch can contain at any given time. Since version 11.0, additional sprites added past this number will automatically grow the spritebatch.
SpriteBatchUsage usage ("dynamic")
The expected usage of the SpriteBatch. The specified usage mode affects the SpriteBatch's memory usage and performance.

Returns

SpriteBatch spriteBatch
The new SpriteBatch.

Examples

This example uses this image for all the graphics:

spriteBatchExample.png

local entities = {}
local spriteBatch, protagonist

function love.load()
	local image = love.graphics.newImage("spriteBatchExample.png")
	spriteBatch = love.graphics.newSpriteBatch(image)

	local quadPlayer   = love.graphics.newQuad(0,  0,  32, 32, image:getDimensions())
	local quadTree     = love.graphics.newQuad(32, 0,  32, 32, image:getDimensions())
	local quadSign     = love.graphics.newQuad(0,  32, 32, 32, image:getDimensions())
	local quadDogHouse = love.graphics.newQuad(32, 32, 32, 32, image:getDimensions())

	local windowWidth, windowHeight = love.graphics.getDimensions()

	love.graphics.setBackgroundColor(.05, .15, .05)

	-- Spawn lots of random things on an uneven grid.
	for baseY = 0, windowHeight, 30 do
		for baseX = 0, windowWidth, 40 do
			local quad = (
				love.math.random() < .10 and quadSign     or
				love.math.random() < .03 and quadDogHouse or
				quadTree
			)
			table.insert(entities, {
				quad   = quad,
				x      = baseX + math.random(-12, 12),
				y      = baseY + math.random(-8,  8 ),
				serial = #entities,
			})
		end
	end

	-- Spawn protagonist.
	protagonist = {quad=quadPlayer, x=0, y=0, serial=#entities} -- We set the position in love.update().
	table.insert(entities, protagonist)
end

function love.update(dt)
	local windowWidth, windowHeight = love.graphics.getDimensions()

	-- Make the protagonist move around in a circle.
	local angle   = .4 * love.timer.getTime()
	protagonist.x = windowWidth/2  + .3*windowWidth  * math.cos(angle)
	protagonist.y = windowHeight/2 + .3*windowHeight * math.sin(angle)
end

function love.draw()
	-- Sort entities by their Y position.
	table.sort(entities, function(entity1, entity2)
		if entity1.y ~= entity2.y then
			return entity1.y < entity2.y
		end
		return entity1.serial < entity2.serial -- If Y is the same, use the serial number as fallback.
	end)

	-- Add entities to sprite batch for drawing.
	spriteBatch:clear()
	for _, entity in ipairs(entities) do
		spriteBatch:add(entity.quad, math.floor(entity.x), math.floor(entity.y))
	end

	-- Finally, draw the sprite batch to the screen.
	love.graphics.draw(spriteBatch)
end

See Also


Other Languages