gg: image cache
parent
67aafd03c6
commit
ecaccafd31
|
@ -28,11 +28,12 @@ fn main() {
|
||||||
user_data: app
|
user_data: app
|
||||||
init_fn: init_images
|
init_fn: init_images
|
||||||
)
|
)
|
||||||
|
app.image = app.gg.create_image('logo.png')
|
||||||
app.gg.run()
|
app.gg.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_images(mut app App) {
|
fn init_images(mut app App) {
|
||||||
app.image = gg.create_image('logo.png')
|
//app.image = gg.create_image('logo.png')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame(app &App) {
|
fn frame(app &App) {
|
||||||
|
|
|
@ -51,7 +51,10 @@ pub:
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
render_text bool
|
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:
|
pub mut:
|
||||||
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
||||||
width int
|
width int
|
||||||
|
@ -117,6 +120,10 @@ fn gg_init_sokol_window(user_data voidptr) {
|
||||||
if g.config.init_fn != voidptr(0) {
|
if g.config.init_fn != voidptr(0) {
|
||||||
g.config.init_fn(g.config.user_data)
|
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) {
|
fn gg_frame_fn(user_data voidptr) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ import stbi
|
||||||
|
|
||||||
pub struct Image {
|
pub struct Image {
|
||||||
pub mut:
|
pub mut:
|
||||||
//id int
|
id int
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
nr_channels int
|
nr_channels int
|
||||||
|
@ -24,22 +24,37 @@ pub mut:
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
fn C.sg_isvalid() bool
|
||||||
struct ImageCache {
|
|
||||||
id int
|
|
||||||
mut:
|
|
||||||
simg C.sg_image
|
|
||||||
ok bool
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//pub fn (mut ctx Context) create_image(file string) Image {
|
pub fn (mut ctx Context) create_image(file string) Image {
|
||||||
pub fn 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) {
|
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{
|
||||||
width: stb_img.width
|
width: stb_img.width
|
||||||
|
@ -49,12 +64,8 @@ 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()
|
img.init_sokol_image()
|
||||||
//ctx.img_buf << ImageCache {
|
|
||||||
//id: id
|
|
||||||
//}
|
|
||||||
return img
|
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 {
|
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{
|
mut img_desc := C.sg_image_desc{
|
||||||
width: img.width
|
width: img.width
|
||||||
height: img.height
|
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 = C.sg_make_image(&img_desc)
|
||||||
img.simg_ok = true
|
img.simg_ok = true
|
||||||
|
img.ok = true
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut ctx Context) cache_sokol_image(img &Image) {
|
pub fn (ctx &Context) draw_image(x, y, width, height f32, img_ &Image) {
|
||||||
//println('\ncache sokol image $img.path ok=$img.simg_ok')
|
img := ctx.image_cache[img_.id] // fetch the image from cache
|
||||||
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) {
|
|
||||||
if !img.simg_ok {
|
if !img.simg_ok {
|
||||||
return
|
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)
|
||||||
|
@ -147,4 +134,9 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img &Image) {
|
||||||
sgl.disable_texture()
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue