From 60c642f42da5c22fb40e228edef6f88e6e637d03 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 4 Aug 2020 01:26:56 +0200 Subject: [PATCH] gg: clean up draw_image() --- examples/gg/rectangles.v | 14 ++++++++------ vlib/gg/gg.v | 28 +++++++--------------------- vlib/stbi/stbi.v | 4 ++++ vlib/v/gen/str.v | 5 ++--- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/examples/gg/rectangles.v b/examples/gg/rectangles.v index 192ad43f0c..8a3de89a78 100644 --- a/examples/gg/rectangles.v +++ b/examples/gg/rectangles.v @@ -16,23 +16,25 @@ mut: } fn main() { - mut app := &App{ - //image: gg.create_image2('logo.png') - } + mut app := &App{} app.gg = gg.new_context( bg_color: gx.white width: win_width height: win_height use_ortho: true // This is needed for 2D drawing create_window: true - window_title: 'Empty window' + window_title: 'Rectangles' frame_fn: frame + init_fn: init_app user_data: app - //font_path: os.resource_abs_path('assets/fonts/RobotoMono-Regular.ttf') ) app.gg.run() } +fn init_app(mut app App) { + app.image = gg.create_image('logo.png') +} + fn frame(app &App) { app.gg.begin() app.draw() @@ -44,5 +46,5 @@ fn (app &App) draw() { //app.gg.draw_text_def(300,300, 'привет') app.gg.draw_rect(10, 10, 100, 30, gx.blue) app.gg.draw_empty_rect(110, 150, 80, 40, gx.black) - //app.gg.draw_image2(30,30,100,30, app.image) + app.gg.draw_image(230,30,app.image.width,app.image.height, app.image) } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 19951e6063..b1ea127613 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -243,17 +243,6 @@ pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) { pub fn (ctx &Context) draw_circle(x, y, r f32, c gx.Color) { } -pub fn create_image(file string) u32 { - // println('gg create image "$file"') - if !os.exists(file) { - println('gg create image no such file "$file"') - return u32(0) - } - // img := stbi.load(file) - // img.free() - return 0 // texture -} - pub struct Image { pub mut: width int @@ -265,9 +254,9 @@ pub mut: sokol_img C.sg_image } -pub fn create_image2(file string) Image { +pub fn create_image(file string) Image { if !os.exists(file) { - println('gg create image no such file "$file"') + println('gg.create_image(): file not found: $file') return Image{} // none } stb_img := stbi.load(file) @@ -331,18 +320,15 @@ pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) { sgl.end() } -pub fn (ctx &Context) draw_image(x, y, width, height f32, img u32) { -} - -pub fn (ctx &Context) draw_image2(x, y, width, height f32, img Image) { +pub fn (ctx &Context) draw_image(x, y, width, height f32, img Image) { u0 := f32(0.0) v0 := f32(0.0) u1 := f32(1.0) v1 := f32(1.0) - x0 := f32(x) - y0 := f32(y) - x1 := f32(x + width) - y1 := f32(y + height) + x0 := f32(x) * ctx.scale + y0 := f32(y) * ctx.scale + x1 := f32(x + width) * ctx.scale + y1 := f32(y + height) * ctx.scale // sgl.load_pipeline(ctx.timage_pip) sgl.enable_texture() diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index 6649de54d2..4bc4412b76 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -24,6 +24,10 @@ fn C.stbi_load_from_memory() voidptr fn C.stbi_image_free() fn C.stbi_set_flip_vertically_on_load() +fn init() { + set_flip_vertically_on_load(false) +} + pub fn load(path string) Image { ext := path.all_after_last('.') mut res := Image { diff --git a/vlib/v/gen/str.v b/vlib/v/gen/str.v index 59967e2763..ad2047db5b 100644 --- a/vlib/v/gen/str.v +++ b/vlib/v/gen/str.v @@ -194,11 +194,10 @@ fn (mut g Gen) string_inter_literal_sb_optimized(call_expr ast.CallExpr) { fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { mut cur_line := '' mut tmp := '' - free := g.pref.autofree && g.inside_call && !g.inside_return && - g.inside_ternary == 0 && !g.inside_const + free := g.pref.autofree && g.inside_call && !g.inside_return && g.inside_ternary == 0 && !g.inside_const // && g.cur_fn != 0 && // g.cur_fn.name != '' - if free { + if false && free { // Save the string expr in a temporary variable, so that it can be removed after the call. tmp = g.new_tmp_var() /*