From 399d29b1a0659594eed611324df49c20f9d6b4fd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 29 Jul 2016 01:52:21 +0900 Subject: [PATCH] examples/2048: Draw texts --- examples/2048/2048/board.go | 26 +++++++++++++++++++++----- examples/2048/2048/game.go | 4 ++-- examples/2048/main.go | 2 +- examples/common/font.go | 5 +++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/2048/2048/board.go b/examples/2048/2048/board.go index 823a35bc7..1207f4669 100644 --- a/examples/2048/2048/board.go +++ b/examples/2048/2048/board.go @@ -18,8 +18,10 @@ import ( "image/color" "math/rand" "sort" + "strconv" "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/examples/common" ) type Board struct { @@ -144,8 +146,8 @@ func (b *Board) Move(dir Dir) { } const ( - tileSize = 80 - tileMargin = 4 + tileSize = 40 + tileMargin = 2 ) var ( @@ -170,9 +172,11 @@ func colorToScale(clr color.Color) (float64, float64, float64, float64) { bf := float64(b) / 0xffff af := float64(a) / 0xffff // Convert to non-premultiplied alpha components. - rf /= af - gf /= af - bf /= af + if 0 < af { + rf /= af + gf /= af + bf /= af + } return rf, gf, bf, af } @@ -202,6 +206,18 @@ func (b *Board) Draw(screen *ebiten.Image) error { if err := screen.DrawImage(tileImage, op); err != nil { return err } + if t != nil { + str := strconv.Itoa(t.value) + scale := 2 + if 2 < len(str) { + scale = 1 + } + w := common.ArcadeFont.TextWidth(str) * scale + h := common.ArcadeFont.TextHeight(str) * scale + x := x + (tileSize-w)/2 + y := y + (tileSize-h)/2 + common.ArcadeFont.DrawText(screen, str, x, y, scale, tileColor(t.value)) + } } } return nil diff --git a/examples/2048/2048/game.go b/examples/2048/2048/game.go index 321e47b1e..073595b7d 100644 --- a/examples/2048/2048/game.go +++ b/examples/2048/2048/game.go @@ -26,8 +26,8 @@ func init() { } const ( - ScreenWidth = 420 - ScreenHeight = 600 + ScreenWidth = 210 + ScreenHeight = 300 boardSize = 4 ) diff --git a/examples/2048/main.go b/examples/2048/main.go index 3e46a499b..880d208d8 100644 --- a/examples/2048/main.go +++ b/examples/2048/main.go @@ -39,7 +39,7 @@ func update(screen *ebiten.Image) error { } func main() { - if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 1, "2048 (Ebiten Demo)"); err != nil { + if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 2, "2048 (Ebiten Demo)"); err != nil { log.Fatal(err) } } diff --git a/examples/common/font.go b/examples/common/font.go index 468f5f526..adde74a32 100644 --- a/examples/common/font.go +++ b/examples/common/font.go @@ -45,6 +45,11 @@ func (f *Font) TextWidth(str string) int { return f.charWidth * len(str) } +func (f *Font) TextHeight(str string) int { + // TODO: Take care about '\n' + return f.charHeight +} + func init() { dir := "" if runtime.GOARCH != "js" {