gg: image cache

pull/6074/head
Alexander Medvednikov 2020-08-05 16:00:24 +02:00
parent 67aafd03c6
commit ecaccafd31
3 changed files with 46 additions and 46 deletions

View File

@ -28,11 +28,12 @@ fn main() {
user_data: app
init_fn: init_images
)
app.image = app.gg.create_image('logo.png')
app.gg.run()
}
fn init_images(mut app App) {
app.image = gg.create_image('logo.png')
//app.image = gg.create_image('logo.png')
}
fn frame(app &App) {

View File

@ -51,7 +51,10 @@ pub:
pub struct Context {
render_text bool
//img_cache []ImageCache
mut:
// a cache with all images created by the user. used for sokol image init and to save space
// (so that the user can store image ids, not entire Image objects)
image_cache []Image
pub mut:
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
width int
@ -117,6 +120,10 @@ fn gg_init_sokol_window(user_data voidptr) {
if g.config.init_fn != voidptr(0) {
g.config.init_fn(g.config.user_data)
}
// Create images now that we can do that after sg is inited
for i in 0..g.image_cache.len {
g.image_cache[i].init_sokol_image()
}
}
fn gg_frame_fn(user_data voidptr) {

View File

@ -12,7 +12,7 @@ import stbi
pub struct Image {
pub mut:
//id int
id int
width int
height int
nr_channels int
@ -24,22 +24,37 @@ pub mut:
path string
}
/*
struct ImageCache {
id int
mut:
simg C.sg_image
ok bool
}
*/
fn C.sg_isvalid() bool
//pub fn (mut ctx Context) create_image(file string) Image {
pub fn create_image(file string) Image {
pub fn (mut ctx Context) create_image(file string) Image {
if !C.sg_isvalid() {
//ctx.image_queue << file
stb_img := stbi.load(file)
img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: false
data: stb_img.data
ext: stb_img.ext
path: file
id: ctx.image_cache.len
}
ctx.image_cache << img
return img
}
mut img := create_image(file)
img.id = ctx.image_cache.len
ctx.image_cache << img
return img
}
// TODO remove this
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
@ -49,12 +64,8 @@ pub fn create_image(file string) Image {
data: stb_img.data
ext: stb_img.ext
path: file
//id: id
}
img.init_sokol_image()
//ctx.img_buf << ImageCache {
//id: id
//}
return img
}
@ -76,7 +87,7 @@ pub fn create_image_from_byte_array(b []byte) Image {
}
pub fn (mut img Image) init_sokol_image() &Image {
println('\n init sokol image $img.path ok=$img.simg_ok')
//println('\n init sokol image $img.path ok=$img.simg_ok')
mut img_desc := C.sg_image_desc{
width: img.width
height: img.height
@ -92,38 +103,14 @@ pub fn (mut img Image) init_sokol_image() &Image {
}
img.simg = C.sg_make_image(&img_desc)
img.simg_ok = true
img.ok = true
return img
}
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
}
img_desc.content.subimage[0][0] = C.sg_subimage_content{
ptr: img.data
size: img.nr_channels * img.width * img.height
}
//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) {
img := ctx.image_cache[img_.id] // fetch the image from cache
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)
@ -147,4 +134,9 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) {
sgl.disable_texture()
}
pub fn (ctx &Context) draw_image_by_id(x, y, width, height f32, id int) {
img := ctx.image_cache[id]
ctx.draw_image(x,y,width,height,img)
}