gg: use sgl.begin_points for pixels + improve pixels example (#12085)
parent
d2d70450a5
commit
09cc0c7247
|
@ -6,11 +6,27 @@ import gx
|
||||||
struct App {
|
struct App {
|
||||||
mut:
|
mut:
|
||||||
gg &gg.Context
|
gg &gg.Context
|
||||||
|
pixels []f32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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{
|
mut app := &App{
|
||||||
gg: 0
|
gg: 0
|
||||||
|
pixels: pixels
|
||||||
}
|
}
|
||||||
app.gg = gg.new_context(
|
app.gg = gg.new_context(
|
||||||
bg_color: gx.rgb(174, 198, 255)
|
bg_color: gx.rgb(174, 198, 255)
|
||||||
|
@ -24,16 +40,13 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame(mut app App) {
|
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.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()
|
app.gg.end()
|
||||||
}
|
}
|
||||||
|
|
16
vlib/gg/gg.v
16
vlib/gg/gg.v
|
@ -270,9 +270,17 @@ pub fn (ctx &Context) draw_square(x f32, y f32, s f32, c gx.Color) {
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (ctx &Context) set_pixel(x f32, y f32, c gx.Color) {
|
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) {
|
pub fn (ctx &Context) set_pixels(points []f32, c gx.Color) {
|
||||||
assert points.len % 2 == 0
|
assert points.len % 2 == 0
|
||||||
len := points.len / 2
|
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.c4b(c.r, c.g, c.b, c.a)
|
||||||
sgl.begin_quads()
|
sgl.begin_points()
|
||||||
for i in 0 .. len {
|
for i in 0 .. len {
|
||||||
x, y := points[i * 2], points[i * 2 + 1]
|
x, y := points[i * 2], points[i * 2 + 1]
|
||||||
|
|
||||||
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
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()
|
sgl.end()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue