Skip to content

Commit

Permalink
multi asset and font
Browse files Browse the repository at this point in the history
  • Loading branch information
temidaradev authored Nov 13, 2024
1 parent 93a4831 commit 47b6ac0
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions esset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"embed"
"image"
_ "image/png"
"io/fs"
"log"

"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)

func GetSingleAsset(fs embed.FS, name string) *ebiten.Image {
file, err := fs.Open(name)
func GetAsset(efs embed.FS, path string) *ebiten.Image {
file, err := efs.Open(path)
if err != nil {
log.Fatal(err)
}
Expand All @@ -22,3 +25,40 @@ func GetSingleAsset(fs embed.FS, name string) *ebiten.Image {

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
}

func GetFont(efs embed.FS, name string) font.Face {
f, err := efs.ReadFile(name)
if err != nil {
panic(err)
}

tt, err := opentype.Parse(f)
if err != nil {
panic(err)
}

face, err := opentype.NewFace(tt, &opentype.FaceOptions{
Size: 48,
DPI: 72,
Hinting: font.HintingVertical,
})
if err != nil {
panic(err)
}

return face
}

0 comments on commit 47b6ac0

Please sign in to comment.