gg: add a missing math import and vfmt

pull/6595/head^2
Alexander Medvednikov 2020-10-10 10:37:17 +02:00
parent 1332bba7af
commit d8d80fbf42
1 changed files with 19 additions and 47 deletions

View File

@ -8,6 +8,7 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.sgl import sokol.sgl
import sokol.gfx import sokol.gfx
import math
// import time // import time
pub type FNCb = fn (x voidptr) pub type FNCb = fn (x voidptr)
@ -105,7 +106,6 @@ fn gg_init_sokol_window(user_data voidptr) {
// fb_h := sapp.height() // fb_h := sapp.height()
// println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h') // println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
// if g.config.init_text { // if g.config.init_text {
// `os.is_file()` won't work on Android if the font file is embedded into the APK // `os.is_file()` won't work on Android if the font file is embedded into the APK
exists := $if !android { os.is_file(g.config.font_path) } $else { true } exists := $if !android { os.is_file(g.config.font_path) } $else { true }
if g.config.font_path != '' && exists { if g.config.font_path != '' && exists {
@ -232,8 +232,8 @@ pub fn (gg &Context) run() {
} }
pub fn (mut ctx Context) set_bg_color(c gx.Color) { pub fn (mut ctx Context) set_bg_color(c gx.Color) {
ctx.clear_pass = gfx.create_clear_pass(f32(c.r) / 255.0, f32(c.g) / 255.0, ctx.clear_pass = gfx.create_clear_pass(f32(c.r) / 255.0, f32(c.g) / 255.0, f32(c.b) / 255.0,
f32(c.b) / 255.0, f32(c.a) / 255.0) f32(c.a) / 255.0)
} }
// TODO: Fix alpha // TODO: Fix alpha
@ -284,17 +284,14 @@ pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
sgl.end() sgl.end()
} }
pub fn (ctx &Context) draw_circle_line(x, y, r int, segments int, c gx.Color) { pub fn (ctx &Context) draw_circle_line(x, y, r, segments int, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
mut theta := f32(0) mut theta := f32(0)
mut xx := f32(0) mut xx := f32(0)
mut yy := f32(0) mut yy := f32(0)
sgl.begin_line_strip() sgl.begin_line_strip()
for i := 0; i < segments + 1; i++ { for i := 0; i < segments + 1; i++ {
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments) theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
@ -305,16 +302,14 @@ pub fn (ctx &Context) draw_circle_line(x, y, r int, segments int, c gx.Color) {
sgl.end() sgl.end()
} }
pub fn (ctx &Context) draw_circle(x, y, r int, segments int, c gx.Color) { pub fn (ctx &Context) draw_circle(x, y, r, segments int, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
mut theta := f32(0) mut theta := f32(0)
mut xx := f32(0) mut xx := f32(0)
mut yy := f32(0) mut yy := f32(0)
sgl.begin_triangle_strip() sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ { for i := 0; i < segments + 1; i++ {
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments) theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
@ -326,70 +321,50 @@ pub fn (ctx &Context) draw_circle(x, y, r int, segments int, c gx.Color) {
sgl.end() sgl.end()
} }
pub fn (ctx &Context) draw_arc_line(x, y, r int, start_angle, arc_angle f32, segments int, c gx.Color) {
pub fn (ctx &Context) draw_arc_line(x, y, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
mut theta := f32(arc_angle / f32(segments)) mut theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta) tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta) rad_factor := math.cosf(theta)
mut xx := f32(r * math.cosf(start_angle)) mut xx := f32(r * math.cosf(start_angle))
mut yy := f32(r * math.sinf(start_angle)) mut yy := f32(r * math.sinf(start_angle))
sgl.begin_line_strip() sgl.begin_line_strip()
for i := 0; i < segments + 1; i++ { for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + x, yy + y) sgl.v2f(xx + x, yy + y)
tx := -yy tx := -yy
ty := xx ty := xx
xx += tx * tan_factor xx += tx * tan_factor
yy += ty * tan_factor yy += ty * tan_factor
xx *= rad_factor xx *= rad_factor
yy *= rad_factor yy *= rad_factor
} }
sgl.end() sgl.end()
} }
pub fn (ctx &Context) draw_arc(x, y, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) { pub fn (ctx &Context) draw_arc(x, y, r int, start_angle, arc_angle f32, segments int, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
mut theta := f32(arc_angle / f32(segments)) mut theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta) tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta) rad_factor := math.cosf(theta)
mut xx := f32(r * math.cosf(start_angle)) mut xx := f32(r * math.cosf(start_angle))
mut yy := f32(r * math.sinf(start_angle)) mut yy := f32(r * math.sinf(start_angle))
sgl.begin_triangle_strip() sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ { for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + x, yy + y) sgl.v2f(xx + x, yy + y)
sgl.v2f(x, y) sgl.v2f(x, y)
tx := -yy tx := -yy
ty := xx ty := xx
xx += tx * tan_factor xx += tx * tan_factor
yy += ty * tan_factor yy += ty * tan_factor
xx *= rad_factor xx *= rad_factor
yy *= rad_factor yy *= rad_factor
} }
sgl.end() sgl.end()
} }
@ -420,7 +395,6 @@ fn abs(a f32) f32 {
return -a return -a
} }
pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) { pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
@ -431,11 +405,9 @@ pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
mut height := abs(y2 - y) mut height := abs(y2 - y)
if width == 0 { if width == 0 {
width = 1 width = 1
} } else if height == 0 {
else if height == 0 {
height = 1 height = 1
} }
ctx.draw_rect(x, y, width, height, c) ctx.draw_rect(x, y, width, height, c)
return return
} }