Looking for a bit of clarity on the use of sprites vs meshes. I put together a sample bit of code to test the relative speed difference between the two and as suspected the mesh approach vastly outperforms the sprite approach.
My question is, what can sprites do that meshes cannot? Or in other words, when should I be using sprites instead of meshes? Are sprites basically intended as a simple method of getting images up and running ( a single command “sprite” as opposed to the four mesh(), texture, addRect and setRectTex) or am I missing something.
Many thanks in advance
-- Use this function to perform your initial setup
function setup()
--gameState is the current state of play
-- 1 is sprites
-- 2 is meshes
iparameter("gameState",1,2,1)
iparameter("size",25,100,40)
g=mesh()
g.texture="Planet Cute:Brown Block"
xpos=0 --used for the movement of the background
ground={}
roof={}
for i=1,200 do
ground[i]=math.random(20)
roof[i]=math.random(20)
end
FPS = 0
watch("FPS")
timeInterval = 0
frameCount = 0
end
-- This function gets called once every frame
function draw()
--FPS print out from Minesweeper by @Reefwing
frameCount = frameCount + 1
timeInterval = timeInterval + DeltaTime
if timeInterval > 1 then
FPS = math.floor((frameCount / timeInterval)+0.5)
timeInterval = 0
frameCount = 0
end
elementcount=0
background(0)
if gameState==1 then
for i = 1,(1.5*WIDTH/size) do
for h=1,ground[i] do
sprite("Planet Cute:Ramp South",xpos+size*i-size,-50+(size/2)*h,size,size)
elementcount = elementcount + 1
end
for h=1,roof[i] do
sprite("Planet Cute:Ramp South",xpos+size*i-size,HEIGHT+50-(size/2)*h,size,size)
elementcount = elementcount + 1
end
end
elseif gameState==2 then
g:clear()
for i = 1,(1.5*WIDTH/size) do
for h=1,ground[i] do
local idx = g:addRect(xpos+size*i-size,-50+(size/2)*h,size,size)
g:setRectTex(idx, 0, 0, 1, 1)
elementcount = elementcount + 1
end
for h=1,roof[i] do
local idx = g:addRect(xpos+size*i-size,HEIGHT+50-(size/2)*h,size,size)
g:setRectTex(idx, 0, 0, 1, 1)
elementcount = elementcount + 1
end
end
g:draw()
end
xpos=xpos-1
if xpos<-1*size then
xpos=0
end
temp=ground[1]
tempr=roof[1]
for c=1,#ground-1 do
ground[c]=ground[c+1]
roof[c]=roof[c+1]
end
ground[#ground]=temp
roof[#roof]=tempr
text("Elements drawn: "..elementcount,WIDTH/2,HEIGHT/2)
end