summaryrefslogtreecommitdiff
path: root/honey/image.lua
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-03-29 00:01:54 -0500
committersanine <sanine.not@pm.me>2023-03-29 00:01:54 -0500
commit5a93fb6f6a8fb6f1dad821b407b4f4531bc23783 (patch)
treed7b7fb60868e2381bdd14efa357188ba3a27b804 /honey/image.lua
parent45dbe47d17303050cbea7c2c51e838acfe21c2fb (diff)
add image caching
Diffstat (limited to 'honey/image.lua')
-rw-r--r--honey/image.lua50
1 files changed, 50 insertions, 0 deletions
diff --git a/honey/image.lua b/honey/image.lua
new file mode 100644
index 0000000..f074272
--- /dev/null
+++ b/honey/image.lua
@@ -0,0 +1,50 @@
+local gl = honey.gl
+
+local module = {}
+setmetatable(module, {__index=_G})
+setfenv(1, module)
+
+function Image(filename, params)
+ local params = params or {}
+ local data, width, height = honey.image.load(filename, 4)
+ local self = {}
+ self.width = width
+ self.height = height
+
+ self.texture = gl.GenTextures()
+ gl.BindTexture(gl.TEXTURE_2D, self.texture)
+ for param, value in pairs(params) do
+ gl.TexParameteri(gl.TEXTURE_2D, gl[param], value)
+ end
+ gl.TexImage2D(
+ gl.TEXTURE_2D, 0,
+ gl.RGBA, width, height,
+ gl.RGBA, gl.UNSIGNED_BYTE, data
+ )
+ gl.GenerateMipmap(gl.TEXTURE_2D)
+ honey.image.destroy(data)
+
+ self.__gc = honey.util.gc_canary(function()
+ gl.DeleteTextures(self.texture)
+ end)
+
+ return self
+end
+
+
+local cache = {}
+function loadImage(filename, params)
+ if not cache[filename] then
+ local img = Image(filename, params)
+ cache[filename] = img
+ return img
+ end
+
+ return cache[filename]
+end
+
+function clearImageCache()
+ cache = {}
+end
+
+return module