Drawing Geometries

Currently I'm doing a lot of drawing-of-things using playdate.graphics.drawLine(), playdate.graphics.drawArc(), etc, and recomputing coordinates each step. I would much rather work with geometries that I can keep around/manipulate.

Unfortunately, I'm using lots of different geometries, and rendering them all seems awkward; I would love to just throw them all into a table and pass it to a function that says gfx.drawGeometries(tbl). Or, A function on each geometry that automatically draws it into a context. Something like:

for i,v in ipairs(tbl) do v.draw() end

Instead, as far as I can tell, I need to do some type checking on my end, either by grouping things of the same geometry type into different tables, or something like:

for i,v in ipairs(tbl) do
  if "arc" == type(v) then gfx.drawArc(v)
  elseif "line" == type(v) then gfx.drawLine(v)
 .... etc etc

This seems ugly. Is there something better that I'm missing?


It's a cool idea. A display list of sorts.

I'm wondering if there's a performance benefit of drawing all shapes like this one after another than drawing them from all over the code (which is what I do).

I asked ChatGPT and it did it your way. Then I asked it if it could be done more optimally and it came up with the idea of using a table of functions, which is perhaps more readable and could perhaps be more performant, you might also try relocating the table of functions outside of the drawShapes function so it's only defined once. Unless performance was a problem, all of this would be personal preference.

(Love2D code, but simple to translate the shape calls to Playdate SDK)

edit: I'm wondering if you could sort the display list to draw all shapes with the same colour/fill to reduce the need for repeat setting of colours.

Thank you! That table solution is pretty clean, I think I'll use that!


1 Like