I wrote a small test, and I don't understand what happens...
I always thought that rendering through SpriteBatch should work faster than multiples love.graphics.draw().
Where is my mistake?
Code: Select all
local a,b,c
local q1,q2
local batch1,batch2,batch3
local mode=1
local yPos,direction=100,1
local t1={}
local t2={}
local function getModeName(m)
if m==1 then
return 'multiple draws'
elseif m==2 then
return 'stream batch'
elseif m==3 then
return 'dynamic batch'
elseif m==4 then
return 'static batch'
end
end
function love.load()
a=love.graphics.newImage('1.png')
b=love.graphics.newImage('2.png')
c=love.graphics.newImage('3.png')
q1=love.graphics.newQuad(0,0,160,160,c:getWidth(),c:getHeight())
q2=love.graphics.newQuad(160,0,160,160,c:getWidth(),c:getHeight())
batch1=love.graphics.newSpriteBatch(c,2000,'stream')
batch2=love.graphics.newSpriteBatch(c,2000,'dynamic')
for i=1,1000 do
t1[#t1+1] = batch2:add(q1,i,yPos)
t2[#t2+1] = batch2:add(q2,i,yPos+200)
end
batch3=love.graphics.newSpriteBatch(c,2000,'static')
for i=1,1000 do
batch3:add(q1,i,yPos)
batch3:add(q2,i,yPos+200)
end
end
function love.draw()
if mode==1 then --multiple draws
for i=1,1000 do
love.graphics.draw(a,i,yPos)
love.graphics.draw(b,i,yPos+200)
end
elseif mode==2 then --stream batch
love.graphics.draw(batch1)
elseif mode==3 then --dynamic batch
love.graphics.draw(batch2)
elseif mode==4 then --static batch
love.graphics.draw(batch3)
end
love.graphics.print('click to change draw mode\nfps: '..love.timer.getFPS()..'\nmode: '..getModeName(mode))
end
function love.mousepressed()
mode = mode+1
if mode>4 then
mode=1
end
end
function love.update(dt)
--change y position
yPos=yPos+100*dt*direction
if yPos<100 then
direction=1
elseif yPos>200 then
direction=-1
end
--change stream batch
batch1:clear()
for i=1,1000 do
batch1:add(q1,i,yPos)
batch1:add(q2,i,yPos+200)
end
--change dynamic batch
for i=1,1000 do
batch2:set(t1[i],q1,i,yPos)
batch2:set(t2[i],q2,i,yPos+200)
end
end