New to Love, Lua. love.graphics.rectangle performance question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sludgefrog
Prole
Posts: 3
Joined: Sat Mar 25, 2017 7:32 pm

New to Love, Lua. love.graphics.rectangle performance question

Post 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
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: New to Love, Lua. love.graphics.rectangle performance question

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: New to Love, Lua. love.graphics.rectangle performance question

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests