From 3275ae4948fd2c1bb8da780214cbb741dc3178be Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 12 May 2023 01:16:46 -0500 Subject: begin refactor --- honey/asset/image.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 honey/asset/image.lua (limited to 'honey/asset/image.lua') 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 -- cgit v1.2.1