Skip to content

Instantly share code, notes, and snippets.

@xenobrain
Last active October 12, 2023 14:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenobrain/bd1d5e8909686930e27f658e3d385d7a to your computer and use it in GitHub Desktop.
Save xenobrain/bd1d5e8909686930e27f658e3d385d7a to your computer and use it in GitHub Desktop.
circles for dragonruby
module GTK
class Runtime
def draw_circle c
radius = c.radius.to_i || 0
xc = c.x.to_i + radius
yc = c.y.to_i + radius
t = c.thickness || 1
r = c.r || 0
g = c.g || 0
b = c.b || 0
a = c.a || 255
x = 0
y = radius
d = 3 - 2 * radius
while y >= x
d = d.positive? ? d + 4 * (x - (y -= 1)) + 10 : d + 4 * x + 6
@ffi_draw.draw_solid(xc + x, yc + y, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc - x, yc + y, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc + x, yc - y, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc - x, yc - y, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc + y, yc + x, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc - y, yc + x, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc + y, yc - x, t, t, r, g, b, a)
@ffi_draw.draw_solid(xc - y, yc - x, t, t, r, g, b, a)
x += 1
end
end
def draw_solid_circle c
radius = c.radius.to_i || 0
xc = c.x.to_i + radius
yc = c.y.to_i + radius
r = c.r || 0
g = c.g || 0
b = c.b || 0
a = c.a || 255
x = 0
y = radius
d = 3 - 2 * radius
while y >= x
d = d.positive? ? d + 4 * (x - (y -= 1)) + 10 : d + 4 * x + 6
@ffi_draw.draw_line(xc - x, yc - y, xc + x, yc + y, r, g, b, a)
@ffi_draw.draw_line(xc - y, yc - x, xc + y, yc + x, r, g, b, a)
@ffi_draw.draw_line(xc - x, yc + y, xc + x, yc - y, r, g, b, a)
@ffi_draw.draw_line(xc - y, yc + x, xc + y, yc - x, r, g, b, a)
x += 1
end
end
def draw_primitive p
return draw_circle p if p.primitive_marker == :circle
return draw_solid_circle p if p.primitive_marker == :solid_circle
super
end
end
end
def tick args
args.outputs.primitives << { x: 640-100, y: 360-100, radius: 50, thickness: 2, **Color::GREEN, primitive_marker: :circle }
args.outputs.primitives << { x: 640-100, y: 360-100, radius: 50, **Color::BLUE, primitive_marker: :solid_circle }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment