forked from elliotmr/tmx
/
imagedrawer.go
56 lines (47 loc) · 1.21 KB
/
imagedrawer.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
package pixeltmx
import (
"path/filepath"
"github.com/faiface/pixel"
"github.com/pkg/errors"
)
type imageLayerDrawer struct {
resources *Resources
source string
info *LayerInfo
sprite *pixel.Sprite
}
func newImageLayerDriver(resources *Resources, info *LayerInfo) (*imageLayerDrawer, error) {
if info.layer.Image == nil {
return nil, errors.New("image not set for image layer")
}
ild := &imageLayerDrawer{
resources: resources,
info: info,
}
if filepath.IsAbs(info.layer.Image.Source) {
ild.source = filepath.Clean(info.layer.Image.Source)
} else {
ild.source = filepath.Join(resources.path, info.layer.Image.Source)
}
return ild, ild.Update()
}
func (ild *imageLayerDrawer) Type() int {
return ImageLayerDrawer
}
func (ild *imageLayerDrawer) Info() *LayerInfo {
return ild.info
}
func (ild *imageLayerDrawer) Update() error {
pic := ild.resources.images[ild.source]
ild.sprite = pixel.NewSprite(pic, pic.Bounds())
return nil
}
func (ild *imageLayerDrawer) Draw(t pixel.Target) {
vec := ild.info.TMXToPixelRect(
ild.info.offX,
ild.info.offY,
ild.sprite.Frame().W(),
ild.sprite.Frame().H(),
).Center()
ild.sprite.DrawColorMask(t, pixel.IM.Moved(vec), ild.info.color)
}