-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.go
71 lines (57 loc) · 1.52 KB
/
resource.go
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
package tmx
import (
"image"
"os"
//import for gif support
_ "image/gif"
//import for jpeg support
_ "image/jpeg"
//import for png support
_ "image/png"
)
//ResourceLocator can be implemented to
//load resources differently than from filesystem
type ResourceLocator interface {
LocateResource(filepath string) (image.Image, error)
}
//ResourceManager allows better memory handling
//and cleanup of resources
type ResourceManager interface {
UnsetResource(filepath string)
}
//FilesystemLocator loads files simply from the filesystem
//it supports png, jpeg and non animated gifs
type FilesystemLocator struct {
}
//LocateResource to implement ResourceLocator interface
func (f FilesystemLocator) LocateResource(filepath string) (image.Image, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
data, _, err := image.Decode(file)
return data, err
}
type lazyLocator struct {
parent ResourceLocator
tilesets map[string]image.Image
}
func (l *lazyLocator) LocateResource(filepath string) (image.Image, error) {
cached, ok := l.tilesets[filepath]
if ok {
return cached, nil
}
data, err := l.parent.LocateResource(filepath)
if err != nil {
return nil, err
}
l.tilesets[filepath] = data
return data, nil
}
func (l *lazyLocator) UnsetResource(filepath string) {
delete(l.tilesets, filepath)
}
//NewLazyResourceLocator wraps a ResourceLocator and caches results
func NewLazyResourceLocator(l ResourceLocator) ResourceLocator {
return &lazyLocator{parent: l, tilesets: map[string]image.Image{}}
}