It is good to put everything into mesh?

If I put the whole screen into mesh and then mesh:draw, will it increase the performance?
Example of the code:

function setup()
    -- some code
    mymesh = mesh()
    screen = image(WIDTH,HEIGHT)
end

function draw()
    setContext(screen)
    -- some code
    setContext()
    mymesh:clear()
    mymesh.texture = screen
    mymesh:addRect( WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    mymesh:draw()
end

@olzenkhaw When you post code, put ~~~ on a line before and after the code so it formats correctly. I added them to your code. Not everything needs or can be in a mesh, but using a mesh for certain things will increase the performance. You will learn that as you continue coding or looking at various examples.

@olzenkhaw If I look at your example and understand correctly,
I don’t think that would work. In fact I think you would make even more work for the program because you would be drawing all the items on the screen onto an image which you would then draw afterwards.

But meshes do increase performance, but only if you’re replacing lots of sprites.

Putting lots of objects onto one mesh is the key to high performance, yes. Don’t use addRect every frame though. In setup, add all the rects you need to a pool, then when you draw, use setRect to move the meshes. If the number of objects fluctuates (ie, a character dies and needs to be removed), use setRect to set the width to 0 (and perhaps move it off screen to be sure), and add it to the pool of not-in-use rects, to be recycled later.

Points to note with this technique:

  • all the rects will use the same texture. If you want rects on the same mesh to have different textures, you need to combine textures into one image (called a sprite atlas or texture atlas)
  • the rects will be drawn in the order that they were added, and it isn’t easy to change the order. A common approach to manage draw order is to have different meshes for different layers of the scene (a background mesh, middleground mesh, foreground mesh etc)

good luck!

@yojimbo2000 I think you misread @olzenkhaw. If you look at his example code in draw, you see this:

function draw()
    setContext(screen)
    -- some code
    setContext()
    ...
    mymesh.texture = screen
    ...
    mymesh:draw()
end

note: I removed some of his code to make it easier to see what he’s doing

I think @olzenkhaw is trying to draw normally (with functions like sprite, rect, ect) but onto an image that later gets drawn. I don’t think this helps performance at all.