-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathimageLoader.lua
More file actions
63 lines (58 loc) · 1.85 KB
/
imageLoader.lua
File metadata and controls
63 lines (58 loc) · 1.85 KB
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
if not love.graphics then
LOG("IMG lib is not loaded (need love.graphics)")
return setmetatable({},{
__index=function(_)
error("attempt to use IMG lib, but IMG lib is not loaded (need love.graphics)")
end,
})
end
local IMG={}
function IMG._loader(path)
-- Non-string: Just keep the value as it is
if type(path)~='string' then return path end
-- string: Load image with path
local suc,res=pcall(love.graphics.newImage,path)
if not suc then
MSG.log('error',("Cannot load image '%s': %s"):format(path,res))
return PAPER
end
return res
end
---Initialize IMG lib (only once)
---
---### Example
---```
----- Initialize the IMG lib with a index table
---IMG.init{
--- img1='.../img1.jpg',
--- img2='.../img2.png',
--- imgPack={
--- img3_1='.../img3/1.jpg',
--- img3_2='.../img3/2.jpg',
--- img4={
--- '.../img4/1.png',
--- '.../img4/2.png',
--- },
--- },
---}
----- Now you can get image objects same as with get things from the index table:
---local img1=IMG.img1
---local img3_1=IMG.imgPack.img3_1
---local img4_1=IMG.imgPack.img4[1]
---```
--- By the way, the index table **CAN** include non-string value, they won't be loaded as a path string, but just keep the value as it is
---
---Advanced usage: `IMG.init(index)` is overload of `IMG.init(index,false)`. You can create your own lib with `lib=IMG.init(index,true)`, in this way you can help language server doing auto-completion for you
---
---Interesting fact: This is actually a simple wrapping of `TABLE.linkSource` + `IMG._loader`
---@overload fun(index: Map<string | table>)
---@generic T
---@param index T
---@param export? true
---@return T
function IMG.init(index,export)
local lib=export and {} or IMG
TABLE.linkSource(lib,index,IMG._loader)
return lib
end
return IMG