summaryrefslogtreecommitdiff
path: root/honey/asset/image.lua
diff options
context:
space:
mode:
Diffstat (limited to 'honey/asset/image.lua')
-rw-r--r--honey/asset/image.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/honey/asset/image.lua b/honey/asset/image.lua
new file mode 100644
index 0000000..c5bd145
--- /dev/null
+++ b/honey/asset/image.lua
@@ -0,0 +1,49 @@
+local gl = honey.gl
+
+local module = {}
+setmetatable(module, {__index=_G})
+setfenv(1, module)
+
+
+local cache = {}
+
+-- load an image into a gl texture
+local function loadImage(filename)
+ local data, width, height = honey.image.load(filename, 4)
+ local texture = gl.GenTextures()
+ gl.BindTexture(gl.TEXTURE_2D, texture)
+ 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)
+ return texture
+end
+
+-- cached get a texture
+get = function(filename)
+ if not cache[filename] then
+ cache[filename] = loadImage(filename)
+ end
+ return cache[filename]
+end
+
+-- remove a texture from the cache
+forget = function(filename)
+ local texture = cache[filename]
+ if texture then
+ gl.DeleteTextures(texture)
+ end
+ cache[filename] = nil
+end
+
+-- remove all textures from the cache
+clearCache = function(filename)
+ for key in pairs(cache) do
+ forget(key)
+ end
+end
+
+return module