Increasing the frame rate

I’ve started to measure the frame rate in my games and found it to be very low sometimes (around 15 FPS). This only happens when I have a lot of sprites on the screen. Is there any way to increase the frame rate of a game? If so, what are the best ways to do so (i.e. using different file formats, not using sprites, etc.)?

There was this discussion: http://codea.io/talk/discussion/4807/efficiency#Item_14

Also:

•Use setContext() to sprite things like the ground that never move/change onto an image then sprite that image -one sprite call rather than hundreds

•Only draw stuff that’s actually on the screen

•Make your algorithms more efficient

•Don’t use expensive calculations like math.sqrt()

•Never do the same thing twice (as you already know the answer)

•Lots of printing takes ages

Image:get() can be slow when used a lot

@Coder Oh ok, sorry I didn’t realize there was already a discussion similar to this. Thanks for the helpful advice as well!

No problem, when I posted that discussion there were probably a few like that already, and also your question is much more specific than mine was so it is better to start a new thread anyway(please don’t quote me on this though)

@Staples - are you using meshes? If not, you should learn how to use them, and try them out., because they can be much more efficient where you have large numbers of images.

They are the heavy duty version of sprites (in fact, sprites are just simplified meshes).

@Ignatz I’m not using meshes, but I will soon. I’m learning about them on your tutorials! :slight_smile:

@Ignatz I am trying to use meshes, but I keep encountering a problem. I have a texture with 8 images and everything works fine when I use the first image (e.g. 0,0,0.125,1), but when I try to use the second image (e.g. 0.12625,0,0.25625,1) it shows half of the second image mad half of the third image. What am I doing wrong?

Here is my code

function setup()
    sp = mesh()
    sp.texture = "Dropbox:SparkTexture"
    img = readImage("Dropbox:SparkTexture")
    w,h = 800,100
    number = 8
    print((101/8))
end


function draw()
    
    background(40, 40, 50)
    strokeWidth(5)
    sp:clear()
    for i = 1,number do
        x = WIDTH/2-150
        y = HEIGHT/2
        r = sp:addRect(x,y,w/8,h)
        sp:setRectTex(r,0,0,1,1) --works fine
        sp:setRectTex(sp:addRect(x+150,y,w/8,h),0,0.12625,0,0.25625,1)--doesn't work
    end
    sp:draw()
end

If the images are equally spaced horizontally use this formulae

imgNum=2
(1/8*(imgNum-1), 0, 1/8*imgNum, 1)

@Coder That results in the same outcome. Half of the second image is shown and half of the third image. If you want to try out the code with my image, here is the texture.

What image are you using? Edit: sorry i didn’t read your above post.

@Coder If you’re referring to the file type, it is a png. If you’re referring to the actual file, I posted it in the above comment. Here it is again.

@Stapels it works now, I took ages but eventually realised that it was my own stupid mistake:
SetRect needs x,y,w,h not x,y,x2,y2 as I gave in my previous answer. See fixed code below:

function setup()
    img = readImage("Dropbox:SparkTexture")
    w,h=img.width,img.height
    sp = mesh()
    sp.texture = img
    number=8
end

function draw()
    background(40, 40, 50)
    sp:clear()
    for i = 1,8 do
        local x,y,x2,y2=1/number*(i-1),0,1/number*(i),1
        local r=sp:addRect(i*90,300,80,80,0)
        sp:setRectTex(r,x,y,x2-x,y2-y)
    end
    sp:draw()
end

Ohhhh!! Now I just feel stupid! Thanks @Coder!

No problem, I should check I’m giving the right advice in future :slight_smile:

@Staples Tip: you don’t need to create 8 squares every frame, you can just store all of them in a table, and then in draw change their coordinates.

Here’s an example of pulling meshes out of a sheet. Think of the 8x8 mesh image that I show as 64 different meshes. Tap the screen to show each individual mesh as a larger one.


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
    img = readImage("Cargo Bot:Startup Screen")
    number=8
    w1=img.width/number
    h1=img.height/number
    sp = mesh()
    sp.texture = img
    for x=1,number do
        for y=1,number do
            r=sp:addRect(x*w1/2,y*h1/2,w1/2-2,h1/2-2,0)
            sp:setRectTex(r,(x-1)/number,(y-1)/number,1/number,1/number)
        end
    end
    sp1=mesh()
    x=1
    y=1
end

function draw()
    background(40,40,50)
    fill(255)
    text("Tap screen to show each seperate mesh",WIDTH/2,HEIGHT/2+50)
    sp:draw()
end

function touched(t)
    if t.state==BEGAN then
        if x>number then
            y=y+1
            x=1
        end
        r=sp:addRect(300,800,w1*3,h1*3,0)
        sp:setRectTex(r,(x-1)/number,(y-1)/number,1/number,1/number)
        x=x+1
    end
end