From b51b0f3c0e21975c33f6179560eb278b082f965d Mon Sep 17 00:00:00 2001 From: temidaradev Date: Thu, 16 Jan 2025 14:35:39 +0300 Subject: [PATCH] new font implementation --- esset.go | 17 ++++++++--------- example/assets/assets.go | 4 ++++ example/main.go | 6 +++++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/esset.go b/esset.go index 88d14fc..9df0727 100644 --- a/esset.go +++ b/esset.go @@ -12,6 +12,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/text/v2" ) +// Assets func GetAsset(efs embed.FS, path string) *ebiten.Image { file, err := efs.Open(path) if err != nil { @@ -40,26 +41,24 @@ func GetMultiAssets(efs embed.FS, path string) []*ebiten.Image { return images } -var ( - fontFaceSource *text.GoTextFaceSource -) - -func DrawText(screen *ebiten.Image, data []byte, str string, fontSize int, posX, posY float64, color color.Color) { +// Fonts +func GetFont(data []byte, fontSize int) (text.Face, error) { s, err := text.NewGoTextFaceSource(bytes.NewReader(data)) if err != nil { panic(err) } - fontFaceSource = s - ffs := &text.GoTextFace{ + return &text.GoTextFace{ Source: s, Size: float64(fontSize), - } + }, nil +} +func DrawText(screen *ebiten.Image, str string, fontSize int, posX, posY float64, textFace text.Face, color color.Color) { op := &text.DrawOptions{} op.GeoM.Translate(posX, posY) op.ColorScale.ScaleWithColor(color) op.LayoutOptions.LineSpacing = float64(fontSize) - text.Draw(screen, str, ffs, op) + text.Draw(screen, str, textFace, op) } diff --git a/example/assets/assets.go b/example/assets/assets.go index 2d8e38a..3f2bc50 100644 --- a/example/assets/assets.go +++ b/example/assets/assets.go @@ -3,6 +3,7 @@ package assets import ( "embed" + "github.com/hajimehoshi/ebiten/v2/text/v2" "github.com/temidaradev/esset/v2" ) @@ -11,6 +12,9 @@ var assets embed.FS //go:embed font/OpenSans-Medium.ttf var MyFont []byte +var FontFaceS text.Face +var FontFaceM text.Face +var FontFaceL text.Face var Idle = esset.GetAsset(assets, "mainchar.png") var Left = esset.GetAsset(assets, "left.png") diff --git a/example/main.go b/example/main.go index fad006d..444cfa9 100644 --- a/example/main.go +++ b/example/main.go @@ -10,6 +10,10 @@ import ( "github.com/temidaradev/esset/v2/example/assets" ) +func init() { + assets.FontFaceS, _ = esset.GetFont(assets.MyFont, 48) +} + type Char struct { x int y int @@ -100,7 +104,7 @@ func (g *Game) Draw(screen *ebiten.Image) { opF := &text.DrawOptions{} opF.GeoM.Translate(245, 75) opF.ColorScale.ScaleWithColor(color.White) - esset.DrawText(screen, assets.MyFont, "ESSET\nbib", 48, 245, 75, color.White) + esset.DrawText(screen, "Esset\nBasic Asset Implementer\nFor Ebitengine!", 48, 0, 75, assets.FontFaceS, color.Black) op := &ebiten.DrawImageOptions{} op.GeoM.Scale(0.3, 0.3)