-
Notifications
You must be signed in to change notification settings - Fork 4
/
spritesheet.go
67 lines (51 loc) · 1.41 KB
/
spritesheet.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
package spriteplus
import (
"github.com/dusk125/pixelutils/packer"
"github.com/faiface/pixel"
)
//NewSpriteSheet creates a new instatiated sprite sheet
func NewSpriteSheet(debugDraw bool) *SpriteSheet {
var flags uint8 = 0
flags |= packer.AllowGrowth
if debugDraw {
flags |= packer.DebugDraw
}
return &SpriteSheet{
Packr: packer.NewPacker(256, 256, flags),
Alias: make(map[string]int),
Cache: make(map[string]*pixel.Sprite),
}
}
//SpriteSheet
type SpriteSheet struct {
Cache map[string]*pixel.Sprite
Alias map[string]int
Packr *packer.Packer
}
func (ss *SpriteSheet) AddSprite(pic pixel.Picture, id string) error {
intId := ss.Packr.GenerateId()
ss.Alias[id] = intId
err := ss.Packr.InsertPictureDataV(intId, pic.(*pixel.PictureData), 0)
if err != nil {
return err
}
ss.Cache[id] = ss.Packr.SpriteFrom(ss.Alias[id])
return nil
}
//Optimize the underlying texture
func (ss *SpriteSheet) Optimize() {
ss.Packr.Optimize()
}
//GetSprite will return the sprite in the Cache (or create&add it to the Cache) from the given int id
func (ss *SpriteSheet) GetSprite(id string) *pixel.Sprite {
sprite := ss.Cache[id]
if sprite == nil {
sprite = ss.Packr.SpriteFrom(ss.Alias[id])
ss.Cache[id] = sprite
}
return sprite
}
//SourcePic returns the underlying pixel.Picture of the spritesheet (for sure with Batch rendering)
func (ss SpriteSheet) SourcePic() pixel.Picture {
return ss.Packr.Picture()
}