From 576396cf208a613f75480bf979d0573a82ece8bb Mon Sep 17 00:00:00 2001
From: Alexander Medvednikov <alexander@vlang.io>
Date: Sat, 12 Dec 2020 23:30:31 +0100
Subject: [PATCH] gg: fix cached images loaded from memory

---
 thirdparty/sokol/sokol_gfx.h |  2 +-
 vlib/gg/image.v              | 20 ++++++++------------
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/thirdparty/sokol/sokol_gfx.h b/thirdparty/sokol/sokol_gfx.h
index 57cde6cb05..35385dd526 100644
--- a/thirdparty/sokol/sokol_gfx.h
+++ b/thirdparty/sokol/sokol_gfx.h
@@ -9490,7 +9490,7 @@ _SOKOL_PRIVATE void _sg_mtl_copy_image_content(const _sg_image_t* img, __unsafe_
             for (int slice_index = 0; slice_index < num_slices; slice_index++) {
                 const int mtl_slice_index = (img->cmn.type == SG_IMAGETYPE_CUBE) ? face_index : slice_index;
                 const int slice_offset = slice_index * bytes_per_slice;
-                SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size);
+///                SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size);
                 [mtl_tex replaceRegion:region
                     mipmapLevel:mip_index
                     slice:mtl_slice_index
diff --git a/vlib/gg/image.v b/vlib/gg/image.v
index a2962d3188..250dd94f5e 100644
--- a/vlib/gg/image.v
+++ b/vlib/gg/image.v
@@ -31,9 +31,7 @@ pub fn (mut ctx Context) create_image(file string) Image {
 	if !C.sg_isvalid() {
 		// Sokol is not initialized yet, add stbi object to a queue/cache
 		// ctx.image_queue << file
-		stb_img := stbi.load(file) or {
-			return Image{}
-		}
+		stb_img := stbi.load(file) or { return Image{} }
 		img := Image{
 			width: stb_img.width
 			height: stb_img.height
@@ -59,9 +57,7 @@ fn create_image(file string) Image {
 		println('gg.create_image(): file not found: $file')
 		return Image{} // none
 	}
-	stb_img := stbi.load(file) or {
-		return Image{}
-	}
+	stb_img := stbi.load(file) or { return Image{} }
 	mut img := Image{
 		width: stb_img.width
 		height: stb_img.height
@@ -75,10 +71,8 @@ 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) or {
-		return Image{}
-	}
+pub fn (mut ctx Context) create_image_from_memory(buf byteptr, bufsize int) Image {
+	stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
 	mut img := Image{
 		width: stb_img.width
 		height: stb_img.height
@@ -86,12 +80,14 @@ pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
 		ok: stb_img.ok
 		data: stb_img.data
 		ext: stb_img.ext
+		id: ctx.image_cache.len
 	}
+	ctx.image_cache << img
 	return img
 }
 
-pub fn create_image_from_byte_array(b []byte) Image {
-	return create_image_from_memory(b.data, b.len)
+pub fn (mut ctx Context) create_image_from_byte_array(b []byte) Image {
+	return ctx.create_image_from_memory(b.data, b.len)
 }
 
 pub fn (mut img Image) init_sokol_image() &Image {