-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.lua
77 lines (67 loc) · 2.11 KB
/
cache.lua
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local quadcache = {}
local function CacheQuad(x, y, width, height, sw, sh)
local quadid = tostring(x).."-"..y.."-"..width.."-"..height.."-"..sw.."-"..sh
if not quadcache[quadid] then quadcache[quadid] = love.graphics.newQuad(x, y, width, height, sw, sh) end
return quadcache[quadid]
end
function GetDirectionalQuads(image)
local quads = {}
image = GetImage(image)
local imagewidth = image:getWidth()
local imageheight = image:getHeight()
quads[1] = CacheQuad(1, 1, 32, 32, imagewidth, imageheight)
quads[2] = CacheQuad(35, 1, 32, 32, imagewidth, imageheight)
quads[3] = CacheQuad(1, 35, 32, 32, imagewidth, imageheight)
quads[4] = CacheQuad(35, 35, 32, 32, imagewidth, imageheight)
quads[5] = CacheQuad(1, 69, 32, 32, imagewidth, imageheight)
quads[6] = CacheQuad(35, 69, 32, 32, imagewidth, imageheight)
quads[7] = CacheQuad(1, 103, 32, 32, imagewidth, imageheight)
quads[8] = CacheQuad(35, 103, 32, 32, imagewidth, imageheight)
return quads
end
function GetQuads(neededquads, image)
local quads = {}
if type(image) == "string" then
image = GetImage(image)
end
local imagewidth = image:getWidth()
local imageheight = image:getHeight()
for i = 0,neededquads-1 do
table.insert(quads, CacheQuad(1+(34*i), 1, 32, 32, imagewidth, imageheight))
end
return quads
end
function GetExtraQuad(image)
image = GetImage(image)
return {CacheQuad(69, 1, 32, 32, image:getWidth(), image:getHeight())}
end
function CacheQuadArray(quads)
local key = tonumber(tostring(quads):sub(10), 16)
quadcache[key] = quads
return key
end
function GetQuadArray(key)
return quadcache[key]
end
local imagecache = {}
local ffiString = require("ffi").string
function GetImage(path)
path = ffiString(path)
if not imagecache[path] then
local newImage = love.graphics.newImage(path)
newImage:setWrap("repeat", "repeat")
imagecache[path] = newImage
end
return imagecache[path]
end
function GetCacheInfo()
local cacheinfo = ""
for k, _ in pairs(imagecache) do
cacheinfo = cacheinfo .. ffiString(k) .. "\n"
end
local scale = 1.1
for _ in cacheinfo:gmatch("\n") do
scale = math.max(scale * 0.985, 0.4)
end
return cacheinfo, scale
end