gg: cache sokol C.sg_image handles
parent
ab7936f514
commit
221a777a80
|
@ -3,7 +3,6 @@
|
||||||
module gg
|
module gg
|
||||||
|
|
||||||
import gx
|
import gx
|
||||||
import os
|
|
||||||
import sokol
|
import sokol
|
||||||
import sokol.sapp
|
import sokol.sapp
|
||||||
import sokol.sgl
|
import sokol.sgl
|
||||||
|
|
108
vlib/gg/image.v
108
vlib/gg/image.v
|
@ -2,46 +2,71 @@
|
||||||
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
||||||
module gg
|
module gg
|
||||||
|
|
||||||
import gx
|
|
||||||
import os
|
import os
|
||||||
import sokol
|
import sokol
|
||||||
import sokol.sapp
|
|
||||||
import sokol.sgl
|
import sokol.sgl
|
||||||
import sokol.gfx
|
|
||||||
import stbi
|
import stbi
|
||||||
|
|
||||||
pub struct Image {
|
pub struct Image {
|
||||||
pub mut:
|
pub mut:
|
||||||
//id int
|
id int // used as an index in image_cache.slots
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
nr_channels int
|
nr_channels int
|
||||||
ok bool
|
ok bool
|
||||||
data voidptr
|
data voidptr
|
||||||
ext string
|
ext string
|
||||||
simg_ok bool
|
|
||||||
simg C.sg_image
|
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
//
|
||||||
/*
|
struct ImageCacheSlot {
|
||||||
struct ImageCache {
|
|
||||||
id int
|
id int
|
||||||
mut:
|
simg_ok bool
|
||||||
simg C.sg_image
|
simg C.sg_image
|
||||||
ok bool
|
|
||||||
}
|
}
|
||||||
*/
|
[ref_only]
|
||||||
|
struct ImageCache {
|
||||||
//pub fn (mut ctx Context) create_image(file string) Image {
|
mut:
|
||||||
|
current_image_id int = -1
|
||||||
|
slots []ImageCacheSlot
|
||||||
|
}
|
||||||
|
fn new_image_cache() &ImageCache {
|
||||||
|
return &ImageCache{}
|
||||||
|
}
|
||||||
|
fn (ic &ImageCache) get_new_id() int {
|
||||||
|
mut mic := &ImageCache(0)
|
||||||
|
unsafe {
|
||||||
|
mic = ic
|
||||||
|
}
|
||||||
|
mic.current_image_id++
|
||||||
|
mic.slots << ImageCacheSlot {
|
||||||
|
id: mic.current_image_id
|
||||||
|
simg_ok: false
|
||||||
|
}
|
||||||
|
return ic.current_image_id
|
||||||
|
}
|
||||||
|
fn (ic &ImageCache) set_simg(id int, simg C.sg_image) {
|
||||||
|
if id >= ic.slots.len {
|
||||||
|
panic('invalid image cache id: $id, slots.len: $ic.slots.len')
|
||||||
|
}
|
||||||
|
mut mic := &ImageCache(0)
|
||||||
|
unsafe {
|
||||||
|
mic = ic
|
||||||
|
}
|
||||||
|
mic.slots[id] = ImageCacheSlot{ id: id, simg_ok: true, simg: simg }
|
||||||
|
}
|
||||||
|
const (
|
||||||
|
image_cache = new_image_cache()
|
||||||
|
)
|
||||||
|
//
|
||||||
pub fn create_image(file string) Image {
|
pub fn create_image(file string) Image {
|
||||||
if !os.exists(file) {
|
if !os.exists(file) {
|
||||||
println('gg.create_image(): file not found: $file')
|
println('gg.create_image(): file not found: $file')
|
||||||
return Image{} // none
|
return Image{} // none
|
||||||
}
|
}
|
||||||
//id := ctx.img_buf.len
|
|
||||||
stb_img := stbi.load(file)
|
stb_img := stbi.load(file)
|
||||||
mut img := Image{
|
mut img := Image{
|
||||||
|
id: image_cache.get_new_id()
|
||||||
width: stb_img.width
|
width: stb_img.width
|
||||||
height: stb_img.height
|
height: stb_img.height
|
||||||
nr_channels: stb_img.nr_channels
|
nr_channels: stb_img.nr_channels
|
||||||
|
@ -49,18 +74,14 @@ pub fn create_image(file string) Image {
|
||||||
data: stb_img.data
|
data: stb_img.data
|
||||||
ext: stb_img.ext
|
ext: stb_img.ext
|
||||||
path: file
|
path: file
|
||||||
//id: id
|
|
||||||
}
|
}
|
||||||
img.init_sokol_image()
|
|
||||||
//ctx.img_buf << ImageCache {
|
|
||||||
//id: id
|
|
||||||
//}
|
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
|
pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
|
||||||
stb_img := stbi.load_from_memory(buf, bufsize)
|
stb_img := stbi.load_from_memory(buf, bufsize)
|
||||||
mut img := Image{
|
mut img := Image{
|
||||||
|
id: image_cache.get_new_id()
|
||||||
width: stb_img.width
|
width: stb_img.width
|
||||||
height: stb_img.height
|
height: stb_img.height
|
||||||
nr_channels: stb_img.nr_channels
|
nr_channels: stb_img.nr_channels
|
||||||
|
@ -75,8 +96,8 @@ pub fn create_image_from_byte_array(b []byte) Image {
|
||||||
return create_image_from_memory(b.data, b.len)
|
return create_image_from_memory(b.data, b.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut img Image) init_sokol_image() &Image {
|
pub fn (img &Image) new_sokol_image() C.sg_image {
|
||||||
println('\n init sokol image $img.path ok=$img.simg_ok')
|
//eprintln('> new_sokol_image from img: $img')
|
||||||
mut img_desc := C.sg_image_desc{
|
mut img_desc := C.sg_image_desc{
|
||||||
width: img.width
|
width: img.width
|
||||||
height: img.height
|
height: img.height
|
||||||
|
@ -90,41 +111,22 @@ pub fn (mut img Image) init_sokol_image() &Image {
|
||||||
ptr: img.data
|
ptr: img.data
|
||||||
size: img.nr_channels * img.width * img.height
|
size: img.nr_channels * img.width * img.height
|
||||||
}
|
}
|
||||||
img.simg = C.sg_make_image(&img_desc)
|
simg := C.sg_make_image(&img_desc)
|
||||||
img.simg_ok = true
|
return simg
|
||||||
return img
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut ctx Context) cache_sokol_image(img &Image) {
|
pub fn (img &Image) get_sokol_image() C.sg_image {
|
||||||
//println('\ncache sokol image $img.path ok=$img.simg_ok')
|
slot := image_cache.slots[img.id]
|
||||||
mut img_desc := C.sg_image_desc{
|
if slot.simg_ok {
|
||||||
width: img.width
|
return slot.simg
|
||||||
height: img.height
|
|
||||||
num_mipmaps: 0
|
|
||||||
wrap_u: .clamp_to_edge
|
|
||||||
wrap_v: .clamp_to_edge
|
|
||||||
label: &byte(0)
|
|
||||||
d3d11_texture: 0
|
|
||||||
}
|
}
|
||||||
img_desc.content.subimage[0][0] = C.sg_subimage_content{
|
simg := img.new_sokol_image()
|
||||||
ptr: img.data
|
image_cache.set_simg(img.id, simg)
|
||||||
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) {
|
pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) {
|
||||||
if !img.simg_ok {
|
simg := img.get_sokol_image()
|
||||||
return
|
|
||||||
//ctx.cache_sokol_image(img)
|
|
||||||
/*
|
|
||||||
unsafe {
|
|
||||||
mut image := img
|
|
||||||
image.init_sokol_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)
|
||||||
|
@ -136,7 +138,7 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) {
|
||||||
//
|
//
|
||||||
sgl.load_pipeline(ctx.timage_pip)
|
sgl.load_pipeline(ctx.timage_pip)
|
||||||
sgl.enable_texture()
|
sgl.enable_texture()
|
||||||
sgl.texture(img.simg)
|
sgl.texture(simg)
|
||||||
sgl.begin_quads()
|
sgl.begin_quads()
|
||||||
sgl.c4b(255, 255, 255, 255)
|
sgl.c4b(255, 255, 255, 255)
|
||||||
sgl.v2f_t2f(x0, y0, u0, v0)
|
sgl.v2f_t2f(x0, y0, u0, v0)
|
||||||
|
@ -146,5 +148,3 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) {
|
||||||
sgl.end()
|
sgl.end()
|
||||||
sgl.disable_texture()
|
sgl.disable_texture()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue