From 70a658a265db580bf519ff8d80f13fcf43a72540 Mon Sep 17 00:00:00 2001 From: Benjamin Stigsen Date: Fri, 20 Aug 2021 00:14:25 +0200 Subject: [PATCH] gg: set_pixels() (#11236) --- examples/gg/set_pixels.v | 39 +++++++++++++++++++++++++++++++++++++++ vlib/gg/gg.v | 21 +++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 examples/gg/set_pixels.v diff --git a/examples/gg/set_pixels.v b/examples/gg/set_pixels.v new file mode 100644 index 0000000000..69dc53dd2a --- /dev/null +++ b/examples/gg/set_pixels.v @@ -0,0 +1,39 @@ +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: 100 + height: 100 + window_title: 'Set Pixels' + frame_fn: frame + user_data: app + ) + app.gg.run() +} + +fn frame(mut app App) { + mut pixels := []f32{} + + for x in 30 .. 60 { + for y in 30 .. 60 { + pixels << f32(x) + pixels << f32(y) + } + } + + app.gg.begin() + app.gg.set_pixels(pixels, gx.red) + app.gg.end() +} diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 8a8faaaf13..c6edaa38db 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -268,6 +268,27 @@ pub fn (ctx &Context) set_pixel(x f32, y f32, c gx.Color) { ctx.draw_square(x, y, 1, c) } +pub fn (ctx &Context) set_pixels(points []f32, c gx.Color) { + assert points.len % 2 == 0 + len := points.len / 2 + + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } + + sgl.c4b(c.r, c.g, c.b, c.a) + sgl.begin_quads() + for i in 0 .. len { + x, y := points[i * 2], points[i * 2 + 1] + + sgl.v2f(x * ctx.scale, y * ctx.scale) + sgl.v2f((x + 1) * ctx.scale, y * ctx.scale) + sgl.v2f((x + 1) * ctx.scale, (y + 1) * ctx.scale) + sgl.v2f(x * ctx.scale, (y + 1) * ctx.scale) + } + sgl.end() +} + pub fn (ctx &Context) draw_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color) { if c.a != 255 { sgl.load_pipeline(ctx.timage_pip)