Hello guys,
can someone take a look at the below code please?
I’m currently trying to display as about as 500 objects on screen, but FPS does not look so very stable.
I know I can do much more than this with mesh, so what’s wrong with my code?
Ipad 3 IOS 8.
Thank you in advance
parameter.integer("interval",0,10,1)
function setup()
fps = FPS()
frame = 0
timer = 0
dnl = false
getImage()
end
function draw()
background(183, 183, 183, 255)
if dnl == true then
timer = timer + 1
if timer == 1 then
objSprite = readImage("Documents:Foe-Class-B")
ar = aRect()
end
ar:draw()
frame = (frame+1)%60
if frame%interval == 0 then
ar:Addrect(math.random(WIDTH),HEIGHT+50,40,60)
end
else
text("Downloading Image",WIDTH/2,HEIGHT/2)
end
fps:draw()
end
function getImage()
http.request("https://dl.dropboxusercontent.com/u/3106041/Foe-Class-B.png", gotImage)
end
function gotImage(img)
saveImage("Documents:Foe-Class-B",img)
sound(SOUND_PICKUP, 16598)
dnl = true
end
aRect = class()
function aRect:init()
self.m=mesh()
self.m.texture = objSprite
self.ships = {}
self.index = 0
local index
end
function aRect:Addrect(x,y,w,h)
self.index = self.m:addRect(x,y,w,h)
self.m:setRectTex(self.index,0.93,0.45,0.06,0.20)
table.insert(self.ships,FoeBolt(self.index,x,y,math.random(-10,10),3,0,0) )
end
function aRect:removeRect(index,key)
self.m:setRect(index, 1, 1, 0, 0, 0)
table.remove(self.ships,key)
end
function aRect:draw()
text("Items : "..table.maxn(self.ships),100,60)
self.m:clear()
for i,v in ipairs(self.ships) do
v.x = v.x + v.xa * v.speed
v.y = v.y - v.ya * v.speed
if v.y > HEIGHT + 200 or v.y < - 200 then
self:removeRect(v.index,i)
end
index = self.m:addRect(v.x,v.y,40,60)
self.m:setRectTex(index,0.93,0.45,0.06,0.20)
self.m:setRect(index, v.x, v.y,40, 60, 0)
end
self.m:draw()
end
FoeBolt = class()
function FoeBolt:init(i,x,y,a,speed,btype,rotation)
self.index = i
self.x = x
self.y = y
self.angle = a
self.rotation = rotation
self.xa = math.sin(math.rad(self.angle))
self.ya = math.cos(math.rad(self.angle))
self.timer = 0
self.pow = 3
self.width = width or 9
self.speed = speed or 0.25
self.btype = btype or 0
end
--# FPS
FPS = class()
function FPS:init(frac)
self.val = 60
self.frac = frac or 0.01
-- self.t0 = os.clock()
self.t0 = ElapsedTime
end
function FPS:draw()
pushStyle()
local vShift = 0
-- if jobs:active() then vShift = 30 end
-- if jobs.active then vShift = 30 end
-- update FPS value with some smoothing
local old = self.val
local frac = self.frac
-- local t1 = os.clock()
local t1 = ElapsedTime
local delta = t1 - self.t0
self.t0 = t1
local new = 1/delta or old
if new>65 then new=65 end
local ratio = new/old
if 0.5<ratio and ratio<2 then new = old*(1-frac)+ new*frac end
self.val = new
-- write the FPS on the screen
if math.floor(new) >= 60 then
fill(48, 255, 0, 255)
else
fill(255, 0, 0, 255)
end
-- fontSize(25)
--font("BITDUST TWO")
rectMode(CENTER)
text(math.floor(new).." FPS",WIDTH/2,20-vShift)
popStyle()
end