From 81ed9903ce4a1c07e38cf69815f9c355e4106635 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 23 Jan 2018 00:55:47 +0900 Subject: [PATCH] examples/piano: Use TTF font directly instead of examples/common --- examples/piano/main.go | 43 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/examples/piano/main.go b/examples/piano/main.go index 63e549d48..88ef1ae53 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -19,15 +19,52 @@ package main import ( "fmt" "image/color" + "io/ioutil" "log" "math" + "github.com/golang/freetype/truetype" + "golang.org/x/image/font" + "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/ebitenutil" - "github.com/hajimehoshi/ebiten/examples/common" + "github.com/hajimehoshi/ebiten/text" ) +const ( + arcadeFontSize = 8 +) + +var ( + arcadeFont font.Face +) + +func init() { + f, err := ebitenutil.OpenFile(ebitenutil.JoinStringsIntoFilePath("_resources", "fonts", "arcade_n.ttf")) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + b, err := ioutil.ReadAll(f) + if err != nil { + log.Fatal(err) + } + + tt, err := truetype.Parse(b) + if err != nil { + log.Fatal(err) + } + + const dpi = 72 + arcadeFont = truetype.NewFace(tt, &truetype.Options{ + Size: arcadeFontSize, + DPI: dpi, + Hinting: font.HintingFull, + }) +} + const ( screenWidth = 320 screenHeight = 240 @@ -158,7 +195,7 @@ func init() { x := i*width + 36 height := 112 ebitenutil.DrawRect(imagePiano, float64(x), float64(y), float64(width-1), float64(height), color.White) - common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black) + text.Draw(imagePiano, k, arcadeFont, x+8, y+height-8, color.Black) } blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"} @@ -169,7 +206,7 @@ func init() { x := i*width + 24 height := 64 ebitenutil.DrawRect(imagePiano, float64(x), float64(y), float64(width-1), float64(height), color.Black) - common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White) + text.Draw(imagePiano, k, arcadeFont, x+8, y+height-8, color.White) } }