How to draw rectangles, circles

Is there any way to draw simple shapes like rectangles, circles and give them colors for testing purposes?

Using draw_line message you can draw anything you want. For example:

local function draw_circle (position, radius, segment_count)
	local step = math.rad(360) / segment_count
	local point_a = vmath.vector3(position.x + radius, position.y, 0)
	local color = vmath.vector4(1, 0, 1, 1)
	for i = 1, segment_count do
		local angle = i * step
		local point_b = vmath.vector3(position.x + radius * math.cos(angle), position.y + radius * math.sin(angle), 0)
		msg.post("@render:", "draw_line", { start_point = point_a, end_point = point_b, color = color } )
		point_a = point_b
	end
end -- draw_circle

For debugging purposes, of course.

2 Likes

Could you have the shapes as sprites and tint them? @Pkeod don’t you have a repo with basic shapes?

I never published that one but I could put it up sometime this month. It uses buffers to draw shapes on.

3 Likes

Ah, I thought it was an atlas with png shapes.

Thanks for the reply.
In other engines where they already have the feature to draw basic shapes, is this how it’s done there also? just loop a line around some points?

With a proper drawing API there’s probably functions such as:

draw.line(from, to)
draw.rectangle(bounds)
draw.circle(pos, radius)

And so on…

Defold needs a primitive drawing extension imo. (If extensions even have that kind of power)

Yep, they do. The buffer api allows an extension to modify a texture.

I sadly do not speak C/C++ well, nor do I know anything about openGL (which im assuming is the underlying library?)

In any case, im sure somebody will come along and work on something like this. I do consider it fairly important

edit: or am i understanding that pkeod has already done something?

Yes, maybe :slight_smile: Poll for a next LIb/NE

2 Likes

I miss an ability to draw primitives as meshes/polygons. So I can easily manipulate vertices from Lua and transform the shapes like I do in Corona.

5 Likes

Since this discussion is the first google result for drawing shapes, I wanted to link to AGulev’s drawpixels extension here: https://github.com/AGulev/drawpixels. This has examples for drawing rectangles and circles.

3 Likes