Page 1 of 1

Performance drop after drawing a lot of rectangles?

Posted: Tue Oct 24, 2017 9:26 pm
by slmjkdbtl
I add rectangles (tried circles too) to arrays and draw them on screen (for bleeding effect) but FPS drops to half after certain amount of rectangles are added and drawn. Why is this happening? Is there any alternative way of doing similar thing?

My code is like

Code: Select all


Timer.every(0.1, function()
    self.pixels[#self.pixels + 1] = Pixel(x, y, w, h) -- Pixel returns a table that has position, size and a draw function
end
	

Re: Performance drop after drawing a lot of rectangles?

Posted: Tue Oct 24, 2017 9:31 pm
by Tjakka5
Every rectangle is one draw call, and each draw call takes (relatively) long to process.
When you have lots of them, your framerate will suffer.

There's 2 ways to go about solving this:
1. Use a spritebatch. It's a buffer which takes a texture. Then you tell it where you want to "put" all the images, and then you render it with only one draw call.
2. Use a particlesystem. This seems to be more fit for your situation. It works similairly to the spritebatch. But instead of telling where you want to put the images, you instead define a "emitter" which spawns them, and define some rules for the transformation.

https://love2d.org/wiki/SpriteBatch
https://love2d.org/wiki/ParticleSystem

Re: Performance drop after drawing a lot of rectangles?

Posted: Mon Oct 30, 2017 3:44 am
by slmjkdbtl
Tjakka5 wrote: Tue Oct 24, 2017 9:31 pm Every rectangle is one draw call, and each draw call takes (relatively) long to process.
When you have lots of them, your framerate will suffer.

There's 2 ways to go about solving this:
1. Use a spritebatch. It's a buffer which takes a texture. Then you tell it where you want to "put" all the images, and then you render it with only one draw call.
2. Use a particlesystem. This seems to be more fit for your situation. It works similairly to the spritebatch. But instead of telling where you want to put the images, you instead define a "emitter" which spawns them, and define some rules for the transformation.

https://love2d.org/wiki/SpriteBatch
https://love2d.org/wiki/ParticleSystem
Thanks, the performance increased dramatically after using SpriteBatch.