gg: clean up draw_image()

pull/6056/head
Alexander Medvednikov 2020-08-04 01:26:56 +02:00
parent 7a7572e478
commit 60c642f42d
4 changed files with 21 additions and 30 deletions

View File

@ -16,23 +16,25 @@ mut:
} }
fn main() { fn main() {
mut app := &App{ mut app := &App{}
//image: gg.create_image2('logo.png')
}
app.gg = gg.new_context( app.gg = gg.new_context(
bg_color: gx.white bg_color: gx.white
width: win_width width: win_width
height: win_height height: win_height
use_ortho: true // This is needed for 2D drawing use_ortho: true // This is needed for 2D drawing
create_window: true create_window: true
window_title: 'Empty window' window_title: 'Rectangles'
frame_fn: frame frame_fn: frame
init_fn: init_app
user_data: app user_data: app
//font_path: os.resource_abs_path('assets/fonts/RobotoMono-Regular.ttf')
) )
app.gg.run() app.gg.run()
} }
fn init_app(mut app App) {
app.image = gg.create_image('logo.png')
}
fn frame(app &App) { fn frame(app &App) {
app.gg.begin() app.gg.begin()
app.draw() app.draw()
@ -44,5 +46,5 @@ fn (app &App) draw() {
//app.gg.draw_text_def(300,300, 'привет') //app.gg.draw_text_def(300,300, 'привет')
app.gg.draw_rect(10, 10, 100, 30, gx.blue) app.gg.draw_rect(10, 10, 100, 30, gx.blue)
app.gg.draw_empty_rect(110, 150, 80, 40, gx.black) 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)
} }

View File

@ -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 (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 struct Image {
pub mut: pub mut:
width int width int
@ -265,9 +254,9 @@ pub mut:
sokol_img C.sg_image sokol_img C.sg_image
} }
pub fn create_image2(file string) Image { pub fn create_image(file string) Image {
if !os.exists(file) { if !os.exists(file) {
println('gg create image no such file "$file"') println('gg.create_image(): file not found: $file')
return Image{} // none return Image{} // none
} }
stb_img := stbi.load(file) 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() sgl.end()
} }
pub fn (ctx &Context) draw_image(x, y, width, height f32, img u32) { pub fn (ctx &Context) draw_image(x, y, width, height f32, img Image) {
}
pub fn (ctx &Context) draw_image2(x, y, width, height f32, img Image) {
u0 := f32(0.0) u0 := f32(0.0)
v0 := f32(0.0) v0 := f32(0.0)
u1 := f32(1.0) u1 := f32(1.0)
v1 := f32(1.0) v1 := f32(1.0)
x0 := f32(x) x0 := f32(x) * ctx.scale
y0 := f32(y) y0 := f32(y) * ctx.scale
x1 := f32(x + width) x1 := f32(x + width) * ctx.scale
y1 := f32(y + height) y1 := f32(y + height) * ctx.scale
// //
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture() sgl.enable_texture()

View File

@ -24,6 +24,10 @@ fn C.stbi_load_from_memory() voidptr
fn C.stbi_image_free() fn C.stbi_image_free()
fn C.stbi_set_flip_vertically_on_load() fn C.stbi_set_flip_vertically_on_load()
fn init() {
set_flip_vertically_on_load(false)
}
pub fn load(path string) Image { pub fn load(path string) Image {
ext := path.all_after_last('.') ext := path.all_after_last('.')
mut res := Image { mut res := Image {

View File

@ -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) { fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
mut cur_line := '' mut cur_line := ''
mut tmp := '' mut tmp := ''
free := g.pref.autofree && g.inside_call && !g.inside_return && free := g.pref.autofree && g.inside_call && !g.inside_return && g.inside_ternary == 0 && !g.inside_const
g.inside_ternary == 0 && !g.inside_const
// && g.cur_fn != 0 && // && g.cur_fn != 0 &&
// g.cur_fn.name != '' // 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. // Save the string expr in a temporary variable, so that it can be removed after the call.
tmp = g.new_tmp_var() tmp = g.new_tmp_var()
/* /*