diff --git a/examples/gg/set_pixels.v b/examples/gg/set_pixels.v index 69dc53dd2a..e9075bd424 100644 --- a/examples/gg/set_pixels.v +++ b/examples/gg/set_pixels.v @@ -5,12 +5,28 @@ import gx struct App { mut: - gg &gg.Context + gg &gg.Context + pixels []f32 } fn main() { + mut pixels := []f32{} + density := 4 + for x in 30 .. 60 { + if x % density == 0 { + continue + } + for y in 30 .. 60 { + if y % density == 0 { + continue + } + pixels << f32(x + density) + pixels << f32(y + density) + } + } mut app := &App{ gg: 0 + pixels: pixels } app.gg = gg.new_context( bg_color: gx.rgb(174, 198, 255) @@ -24,16 +40,13 @@ fn main() { } 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) + // Set a blue pixel near each corner. (Find your magnifying glass) + app.gg.set_pixel(2, 2, gx.blue) + app.gg.set_pixel(app.gg.width - 2, 2, gx.blue) + app.gg.set_pixel(app.gg.width - 2, app.gg.height - 2, gx.blue) + app.gg.set_pixel(2, app.gg.height - 2, gx.blue) + // Set pixels in a grid-like pattern. + app.gg.set_pixels(app.pixels, gx.red) app.gg.end() } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 1282fb571f..3385fd5d01 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -270,9 +270,17 @@ pub fn (ctx &Context) draw_square(x f32, y f32, s f32, c gx.Color) { [inline] pub fn (ctx &Context) set_pixel(x f32, y f32, c gx.Color) { - ctx.draw_square(x, y, 1, c) + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } + + sgl.c4b(c.r, c.g, c.b, c.a) + sgl.begin_points() + sgl.v2f(x * ctx.scale, y * ctx.scale) + sgl.end() } +[direct_array_access; inline] pub fn (ctx &Context) set_pixels(points []f32, c gx.Color) { assert points.len % 2 == 0 len := points.len / 2 @@ -282,14 +290,10 @@ pub fn (ctx &Context) set_pixels(points []f32, c gx.Color) { } sgl.c4b(c.r, c.g, c.b, c.a) - sgl.begin_quads() + sgl.begin_points() 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() }