diff options
author | sanine <sanine.not@pm.me> | 2023-05-12 01:16:46 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-12 01:16:46 -0500 |
commit | 3275ae4948fd2c1bb8da780214cbb741dc3178be (patch) | |
tree | 69dbf1d5b56896e1212454e5f79daaec1d201ec1 /honey/asset/image.lua | |
parent | 14195dac1eda9140192ca07003258715b8b0abd3 (diff) |
begin refactor
Diffstat (limited to 'honey/asset/image.lua')
-rw-r--r-- | honey/asset/image.lua | 49 |
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 |