examples/fullscreen: use text/v2

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-19 16:07:13 +09:00
parent 9b9384baa0
commit e4c0fa9d62

View File

@ -24,19 +24,16 @@ import (
"math"
"runtime"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
var (
gophersImage *ebiten.Image
mplusFont font.Face
mplusFaceSource *text.GoTextFaceSource
)
func init() {
@ -48,21 +45,12 @@ func init() {
gophersImage = ebiten.NewImageFromImage(img)
}
func initFont() {
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
const dpi = 72
mplusFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12 * ebiten.DeviceScaleFactor(),
DPI: dpi,
Hinting: font.HintingVertical,
})
func init() {
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
if err != nil {
log.Fatal(err)
}
mplusFaceSource = s
}
type Game struct {
@ -108,7 +96,14 @@ func (g *Game) Draw(screen *ebiten.Image) {
msg += fmt.Sprintf("Game's screen size: %d, %d\n", sw, sh)
msg += fmt.Sprintf("Device scale factor: %0.2f\n", scale)
text.Draw(screen, msg, mplusFont, int(50*scale), 50, color.White)
textOp := &text.DrawOptions{}
textOp.GeoM.Translate(50*scale, 50*scale)
textOp.ColorScale.ScaleWithColor(color.White)
textOp.LineSpacingInPixels = 12 * ebiten.DeviceScaleFactor() * 1.5
text.Draw(screen, msg, &text.GoTextFace{
Source: mplusFaceSource,
Size: 12 * ebiten.DeviceScaleFactor(),
}, textOp)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
@ -117,9 +112,6 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
}
func main() {
// Call initFont here instead of init funcs since ebiten.DeviceScaleFactor is not available in init.
initFont()
ebiten.SetFullscreen(true)
ebiten.SetWindowTitle("Fullscreen (Ebitengine Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {