gg: add draw_convex_poly/2 and draw_empty_poly/2 methods (#8487)

pull/8499/head
Lucas Hernán Tarche 2021-02-01 13:18:23 -03:00 committed by GitHub
parent 1e07173234
commit 7813ecbb75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,34 @@
module main
import gg
import gx
struct App {
mut:
gg &gg.Context
}
fn main() {
mut app := &App{
gg: 0
}
app.gg = gg.new_context(
bg_color: gx.rgb(174, 198, 255)
width: 600
height: 400
window_title: 'Polygons'
frame_fn: frame
user_data: app
)
app.gg.run()
}
fn frame(mut app App) {
app.gg.begin()
app.gg.draw_convex_poly([f32(100.0), 100.0, 200.0, 100.0, 300.0, 200.0, 200.0, 300.0, 100.0,
300.0,
], gx.blue)
app.gg.draw_empty_poly([f32(50.0), 50.0, 70.0, 60.0, 90.0, 80.0, 70.0, 110.0], gx.black)
app.gg.draw_triangle(450, 142, 530, 280, 370, 280, gx.red)
app.gg.end()
}

View File

@ -652,6 +652,53 @@ pub fn (ctx &Context) draw_empty_rounded_rect(x f32, y f32, w f32, h f32, radius
sgl.end() sgl.end()
} }
// draw_convex_poly draws a convex polygon, given an array of points, and a color.
// Note that the points must be given in clockwise order.
pub fn (ctx &Context) draw_convex_poly(points []f32, c gx.Color) {
assert points.len % 2 == 0
len := points.len / 2
assert len >= 3
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
sgl.begin_triangle_strip()
x0 := points[0]
y0 := points[1]
for i in 1 .. (len / 2 + 1) {
sgl.v2f(x0, y0)
sgl.v2f(points[i * 4 - 2], points[i * 4 - 1])
sgl.v2f(points[i * 4], points[i * 4 + 1])
}
if len % 2 == 0 {
sgl.v2f(points[2 * len - 2], points[2 * len - 1])
}
sgl.end()
}
// draw_empty_poly - draws the borders of a polygon, given an array of points, and a color.
// Note that the points must be given in clockwise order.
pub fn (ctx &Context) draw_empty_poly(points []f32, c gx.Color) {
assert points.len % 2 == 0
len := points.len / 2
assert len >= 3
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
sgl.begin_line_strip()
for i in 0 .. len {
sgl.v2f(points[2 * i], points[2 * i + 1])
}
sgl.v2f(points[0], points[1])
sgl.end()
}
pub fn screen_size() Size { pub fn screen_size() Size {
$if macos { $if macos {
return C.gg_get_screen_size() return C.gg_get_screen_size()