diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 7e3a9a4f22..4929b35865 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -3,6 +3,7 @@ module gg import gx +import os import sokol import sokol.sapp import sokol.sgl diff --git a/vlib/gg/image.v b/vlib/gg/image.v index 15fb79b8f4..9b73211366 100644 --- a/vlib/gg/image.v +++ b/vlib/gg/image.v @@ -2,61 +2,44 @@ // Use of this source code is governed by an MIT license that can be found in the LICENSE file. module gg +import gx import os import sokol +import sokol.sapp import sokol.sgl +import sokol.gfx import stbi pub struct Image { pub mut: + //id int width int height int nr_channels int ok bool data voidptr ext string + simg_ok bool + simg C.sg_image path string - cache_slot &ImageCacheSlot = 0 } -// -struct ImageCacheSlot { - id int -mut: - is_simg_ok bool - simg C.sg_image -} -fn (mut cs ImageCacheSlot) set_sokol_image(simg C.sg_image) { - cs.simg = simg - cs.is_simg_ok = true -} -// -[ref_only] + +/* struct ImageCache { + id int mut: - current_image_id int = -1 - slots []ImageCacheSlot + simg C.sg_image + ok bool } -fn (ic &ImageCache) get_new_cache_slot() &ImageCacheSlot { - mut mic := &ImageCache(0) - unsafe { - mic = ic - } - mic.current_image_id++ - mic.slots << ImageCacheSlot { - id: mic.current_image_id - is_simg_ok: false - } - return &mic.slots[mic.slots.len-1] -} -const ( - image_cache = &ImageCache{} -) -// +*/ + +//pub fn (mut ctx Context) create_image(file string) Image { pub fn create_image(file string) Image { if !os.exists(file) { println('gg.create_image(): file not found: $file') return Image{} // none } + //id := ctx.img_buf.len stb_img := stbi.load(file) mut img := Image{ width: stb_img.width @@ -66,8 +49,12 @@ pub fn create_image(file string) Image { data: stb_img.data ext: stb_img.ext path: file - cache_slot: image_cache.get_new_cache_slot() + //id: id } + img.init_sokol_image() + //ctx.img_buf << ImageCache { + //id: id + //} return img } @@ -80,7 +67,6 @@ pub fn create_image_from_memory(buf byteptr, bufsize int) Image { ok: stb_img.ok data: stb_img.data ext: stb_img.ext - cache_slot: image_cache.get_new_cache_slot() } return img } @@ -89,8 +75,8 @@ pub fn create_image_from_byte_array(b []byte) Image { return create_image_from_memory(b.data, b.len) } -pub fn (img &Image) new_sokol_image() C.sg_image { - //eprintln('> new_sokol_image from img: $img') +pub fn (mut img Image) init_sokol_image() &Image { + println('\n init sokol image $img.path ok=$img.simg_ok') mut img_desc := C.sg_image_desc{ width: img.width height: img.height @@ -104,25 +90,41 @@ pub fn (img &Image) new_sokol_image() C.sg_image { ptr: img.data size: img.nr_channels * img.width * img.height } - simg := C.sg_make_image(&img_desc) - return simg + img.simg = C.sg_make_image(&img_desc) + img.simg_ok = true + return img } -pub fn (img &Image) get_sokol_image() C.sg_image { - slot := img.cache_slot - if slot.is_simg_ok { - return slot.simg +fn (mut ctx Context) cache_sokol_image(img &Image) { + //println('\ncache sokol image $img.path ok=$img.simg_ok') + mut img_desc := C.sg_image_desc{ + width: img.width + height: img.height + num_mipmaps: 0 + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 } - simg := img.new_sokol_image() - unsafe { - mut cs := slot - cs.set_sokol_image(simg) + img_desc.content.subimage[0][0] = C.sg_subimage_content{ + ptr: img.data + size: img.nr_channels * img.width * img.height } - return simg + //ctx.img_cache[img.id].simg = C.sg_make_image(&img_desc) + //ctx.img_cache[img.id].ok = true } pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) { - simg := img.get_sokol_image() + if !img.simg_ok { + return + //ctx.cache_sokol_image(img) + /* + unsafe { + mut image := img + image.init_sokol_image() + } + */ + } u0 := f32(0.0) v0 := f32(0.0) u1 := f32(1.0) @@ -134,7 +136,7 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) { // sgl.load_pipeline(ctx.timage_pip) sgl.enable_texture() - sgl.texture(simg) + sgl.texture(img.simg) sgl.begin_quads() sgl.c4b(255, 255, 255, 255) sgl.v2f_t2f(x0, y0, u0, v0) @@ -144,3 +146,5 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) { sgl.end() sgl.disable_texture() } + +