blob: c5bd14594704fed58dbfe79aac69119062bb169f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
|