Skip to content

Commit

Permalink
new font implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
temidaradev committed Jan 16, 2025
1 parent b835de9 commit b51b0f3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 8 additions & 9 deletions esset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions example/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assets
import (
"embed"

"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/temidaradev/esset/v2"
)

Expand All @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b51b0f3

Please sign in to comment.