examples/blocks: Use TTF font directly instead of examples/common

This commit is contained in:
Hajime Hoshi 2018-01-23 00:27:11 +09:00
parent 77ac796b3f
commit 1478850508
6 changed files with 98 additions and 16 deletions

Binary file not shown.

View File

@ -1,5 +1,31 @@
# License # License
## arcade_n.ttf
```
=============================================================
P.Font Series No. : 1013
Type : TrueType
Family Name : Arcade
Style Name : Arcade Normal
Arcade Interlaced
Arcade Rounded
Author : Yuji Adachi
Description : Copyright (C)1997-2003 Yuji Adachi
Support URL : http://www.9031.com/
=============================================================
Attention
印刷物、WEBページ等での御使用は自由ですが、このフォントを、
作者の許可なく販売したり、営利目的の製品に添付することは禁じます。
また、このフォントのデザインは予告なく変更することがあります。
足立 裕司
yuji@9031.com
```
## mplus-1p-regular.ttf ## mplus-1p-regular.ttf
``` ```

View File

@ -18,19 +18,79 @@ package blocks
import ( import (
"image/color" "image/color"
"io/ioutil"
"log"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/common" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/text"
) )
const (
arcadeFontBaseSize = 8
)
var (
arcadeFonts = map[int]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)
}
for i := 1; i <= 4; i++ {
const dpi = 72
arcadeFonts[i] = truetype.NewFace(tt, &truetype.Options{
Size: float64(arcadeFontBaseSize * i),
DPI: dpi,
Hinting: font.HintingFull,
})
}
}
func textWidth(str string) int {
b, _ := font.BoundString(arcadeFonts[1], str)
return (b.Max.X - b.Min.X).Ceil()
}
var (
shadowColor = color.NRGBA{0, 0, 0, 0x80}
)
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
offsetY := arcadeFontBaseSize * scale
for _, line := range strings.Split(str, "\n") {
y += offsetY
text.Draw(rt, line, arcadeFonts[scale], x+1, y+1, shadowColor)
text.Draw(rt, line, arcadeFonts[scale], x, y, clr)
}
}
func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) { func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := common.ArcadeFont.TextWidth(str) * scale w := textWidth(str) * scale
x += (width - w) / 2 x += (width - w) / 2
common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr) drawTextWithShadow(rt, str, x, y, scale, clr)
} }
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) { func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := common.ArcadeFont.TextWidth(str) * scale w := textWidth(str) * scale
x += width - w x += width - w
common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr) drawTextWithShadow(rt, str, x, y, scale, clr)
} }

View File

@ -22,7 +22,6 @@ import (
"strings" "strings"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/common"
) )
type GamepadScene struct { type GamepadScene struct {
@ -103,5 +102,5 @@ ROTATE RIGHT: %s
msg = "OK!" msg = "OK!"
} }
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg) str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
common.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White) drawTextWithShadow(screen, str, 16, 16, 1, color.White)
} }

View File

@ -25,7 +25,6 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/common"
) )
var ( var (
@ -85,7 +84,7 @@ func init() {
// Windows: Next // Windows: Next
x, y = nextWindowLabelPosition() x, y = nextWindowLabelPosition()
common.ArcadeFont.DrawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor) drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor)
x, y = nextWindowPosition() x, y = nextWindowPosition()
drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight) drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight)
@ -115,7 +114,7 @@ func drawWindow(r *ebiten.Image, x, y, width, height int) {
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff} var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
func drawTextBox(r *ebiten.Image, label string, x, y, width int) { func drawTextBox(r *ebiten.Image, label string, x, y, width int) {
common.ArcadeFont.DrawTextWithShadow(r, label, x, y, 1, fontColor) drawTextWithShadow(r, label, x, y, 1, fontColor)
y += blockWidth y += blockWidth
drawWindow(r, x, y, width, 2*blockHeight) drawWindow(r, x, y, width, 2*blockHeight)
} }

View File

@ -21,7 +21,6 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/common"
) )
var imageBackground *ebiten.Image var imageBackground *ebiten.Image
@ -72,9 +71,9 @@ func (s *TitleScene) Draw(r *ebiten.Image) {
drawLogo(r, "BLOCKS") drawLogo(r, "BLOCKS")
message := "PRESS SPACE TO START" message := "PRESS SPACE TO START"
x := (ScreenWidth - common.ArcadeFont.TextWidth(message)) / 2 x := 0
y := ScreenHeight - 48 y := ScreenHeight - 48
common.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}) drawTextWithShadowCenter(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}, ScreenWidth)
} }
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) { func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
@ -93,8 +92,7 @@ func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
func drawLogo(r *ebiten.Image, str string) { func drawLogo(r *ebiten.Image, str string) {
const scale = 4 const scale = 4
textWidth := common.ArcadeFont.TextWidth(str) * scale x := 0
x := (ScreenWidth - textWidth) / 2
y := 32 y := 32
common.ArcadeFont.DrawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}) drawTextWithShadowCenter(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}, ScreenWidth)
} }