Page 1 of 1
New to Love, Lua. love.graphics.rectangle performance question
Posted: Sat Mar 25, 2017 7:37 pm
by sludgefrog
Hello, I'm new to Love. I'm trying to create a game with a 4096x4096 grid of solid colours. However, iterating over a grid and drawing it brings my higher-end CPU to its knees (less than one frame per second). I have attached the problematic code.
What is the best way to draw a terrain-style grid using Love2d? I want this to go FAST!
Code: Select all
function terrain.draw(t,tx,ty,scale)
for y=1,t.height do
for x=1,t.width do
love.graphics.rectangle("fill",
(tx+x)*scale, (ty+y)*scale,
scale, scale)
end
end
end
end
Re: New to Love, Lua. love.graphics.rectangle performance question
Posted: Sat Mar 25, 2017 10:11 pm
by peterrust
sludgefrog,
Is the entire grid visible? If not, you could make things faster by only drawing the ones that are visible.
I don't know much about performance tuning in Love, I came here to see if anyone else had answered your question & was bummed that no one had yet.
I do know that the STI library (
https://github.com/karai17/Simple-Tiled-Implementation/) has been tuned to get good performance (using a sprite batch operation to do bulk drawing was one performance improvement IIRC). I wrote up a tutorial on how to programmatically generate a tilemap (
https://github.com/prust/sti-pg-example), but if you're looking for solid colors instead of graphics, I'm not sure it's the direction you'll want to go.
I would guess that it would be much more performant to draw to a Canvas (aka Framebuffer) once and then display that on the screen. You should find that moving it around and/or changing parts of it will be more performant than re-drawing the whole thing every time. But I'm only guessing, I haven't yet done or optimized this sort of thing. Here's the docs for Canvas:
https://love2d.org/wiki/Canvas
Re: New to Love, Lua. love.graphics.rectangle performance question
Posted: Sat Mar 25, 2017 10:20 pm
by s-ol
First, what peterrust said is right: only draw what you need, that should be the easiest fix.
If it's static, go ahead and use a Canvas.
Second, there are lots of ways to optimize from there:
- use a SpriteBatch, a 1px white texture, then scale and tint it
- use a Mesh (could get a bit complicated)
- use a shader (multiple ways to use one are thinkable)
the spritebatch is probably the easiest of these.