forked from temidaradev/esset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesset.go
66 lines (53 loc) · 1.2 KB
/
esset.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
package esset
import (
"bytes"
"embed"
"image"
"image/color"
_ "image/png"
"io/fs"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
func GetAsset(efs embed.FS, path string) *ebiten.Image {
file, err := efs.Open(path)
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
return ebiten.NewImageFromImage(img)
}
func GetMultiAssets(efs embed.FS, path string) []*ebiten.Image {
matches, err := fs.Glob(efs, path)
if err != nil {
panic(err)
}
images := make([]*ebiten.Image, len(matches))
for i, match := range matches {
images[i] = GetAsset(efs, match)
}
return images
}
var (
fontFaceSource *text.GoTextFaceSource
)
func DrawText(screen *ebiten.Image, data []byte, str string, fontSize int, posX, posY float64, color color.Color) {
s, err := text.NewGoTextFaceSource(bytes.NewReader(data))
if err != nil {
log.Fatal(err)
}
fontFaceSource = s
ffs := &text.GoTextFace{
Source: s,
Size: float64(fontSize),
}
op := &text.DrawOptions{}
op.GeoM.Translate(posX, posY)
op.ColorScale.ScaleWithColor(color)
op.LayoutOptions.LineSpacing = float64(fontSize)
text.Draw(screen, str, ffs, op)
}