From d4fbf422b3c8715d9fbb2fc19deff8d65be4c6a7 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 20 Sep 2020 12:05:30 +0300 Subject: [PATCH] gg: support transparency for rect/triangle/etc primitives too --- examples/tetris/tetris.v | 2 +- vlib/gg/gg.v | 12 ++++++++++++ vlib/gg/text_rendering.v | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index a6a3cdb707..2ac6e07de3 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -87,7 +87,7 @@ const ( ] background_color = gx.white - ui_color = gx.red + ui_color = gx.rgba(255,0,0, 210) ) // TODO: type Tetro [tetro_size]struct{ x, y int } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 5410cd4b33..12859543de 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -226,6 +226,9 @@ pub fn (mut ctx Context) set_bg_color(c gx.Color) { // TODO: Fix alpha pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } sgl.c4b(c.r, c.g, c.b, c.a) sgl.begin_quads() sgl.v2f(x * ctx.scale, y * ctx.scale) @@ -236,6 +239,9 @@ pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) { } pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } sgl.c4b(c.r, c.g, c.b, c.a) sgl.begin_quads() sgl.v2f(x * ctx.scale, y * ctx.scale) @@ -245,6 +251,9 @@ pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) { } pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } sgl.c4b(c.r, c.g, c.b, c.a) sgl.begin_line_strip() if ctx.scale == 1 { @@ -295,6 +304,9 @@ fn abs(a f32) f32 { pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } if ctx.scale > 1 { // Make the line more clear on hi dpi screens: draw a rectangle mut width := abs(x2 - x) diff --git a/vlib/gg/text_rendering.v b/vlib/gg/text_rendering.v index ca97aa16ea..f7fbb283da 100644 --- a/vlib/gg/text_rendering.v +++ b/vlib/gg/text_rendering.v @@ -3,6 +3,7 @@ module gg import sokol.sfons +import sokol.sgl import gx import os @@ -94,7 +95,10 @@ fn (ctx &Context) set_cfg(cfg gx.TextCfg) { size := if cfg.mono { cfg.size - 2 } else { cfg.size } ctx.ft.fons.set_size(scale * f32(size)) C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align)) - color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255) + color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a) + if cfg.color.a != 255 { + sgl.load_pipeline(ctx.timage_pip) + } C.fonsSetColor(ctx.ft.fons, color) ascender := f32(0.0) descender := f32(0.0)