gg,stbi: implement gg.create_image_from_memory/2

pull/6063/head
Delyan Angelov 2020-08-04 13:18:08 +03:00
parent 216b6bf285
commit f9d241ae27
2 changed files with 21 additions and 14 deletions

View File

@ -272,6 +272,23 @@ pub fn create_image(file string) Image {
return img
}
pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
stb_img := stbi.load_from_memory(buf, bufsize)
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
}
return img
}
pub fn create_image_from_byte_array(b []byte) Image {
return create_image_from_memory(b.data, b.len)
}
pub fn (mut img Image) init_sokol_image() &Image {
mut img_desc := C.sg_image_desc{
width: img.width
@ -291,13 +308,6 @@ pub fn (mut img Image) init_sokol_image() &Image {
return img
}
pub fn create_image_from_memory(buf byteptr) u32 {
// texture := gl.gen_texture()
// img := stbi.load_from_memory(buf)
// img.free()
return 0 // texture
}
pub fn (gg &Context) begin() {
if gg.render_text && gg.font_inited {
gg.ft.flush()

View File

@ -38,23 +38,20 @@ pub fn load(path string) Image {
flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag)
if isnil(res.data) {
println('stbi image failed to load')
exit(1)
panic('stbi image failed to load')
}
return res
}
//pub fn load_from_memory(buf []byte) Image {
pub fn load_from_memory(buf byteptr) Image {
pub fn load_from_memory(buf byteptr, bufsize int) Image {
mut res := Image {
ok: true
data: 0
}
flag := C.STBI_rgb_alpha
res.data = C.stbi_load_from_memory(buf, 3812, &res.width, &res.height, &res.nr_channels, flag)
res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels, flag)
if isnil(res.data) {
println('stbi image failed to load from memory')
exit(1)
panic('stbi image failed to load from memory')
}
return res
}