examples/keyboard: Generate the image without Ebiten (#210)

This commit is contained in:
Hajime Hoshi 2016-05-08 16:30:48 +09:00
parent acca1c2480
commit 64a66c57c0
3 changed files with 25 additions and 25 deletions

View File

@ -53,7 +53,7 @@ func text_png() (*asset, error) {
return nil, err return nil, err
} }
info := bindata_file_info{name: "text.png", size: 2058, mode: os.FileMode(420), modTime: time.Unix(1455503506, 0)} info := bindata_file_info{name: "text.png", size: 2058, mode: os.FileMode(420), modTime: time.Unix(1455644231, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }

View File

@ -15,7 +15,9 @@
package common package common
import ( import (
"image"
"image/color" "image/color"
"image/draw"
"math" "math"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -31,6 +33,7 @@ var (
type Font struct { type Font struct {
image *ebiten.Image image *ebiten.Image
origImage image.Image
offset int offset int
charNumPerLine int charNumPerLine int
charWidth int charWidth int
@ -52,11 +55,11 @@ func init() {
} }
arcadeFontPath := filepath.Join(dir, "_resources", "images", "arcadefont.png") arcadeFontPath := filepath.Join(dir, "_resources", "images", "arcadefont.png")
arcadeFontImage, _, err := ebitenutil.NewImageFromFile(arcadeFontPath, ebiten.FilterNearest) arcadeFontImage, origImage, err := ebitenutil.NewImageFromFile(arcadeFontPath, ebiten.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
} }
ArcadeFont = &Font{arcadeFontImage, 32, 16, 8, 8} ArcadeFont = &Font{arcadeFontImage, origImage, 32, 16, 8, 8}
} }
type fontImageParts struct { type fontImageParts struct {
@ -112,6 +115,16 @@ func (f *Font) DrawText(rt *ebiten.Image, str string, ox, oy, scale int, c color
return rt.DrawImage(f.image, options) return rt.DrawImage(f.image, options)
} }
func (f *Font) DrawTextOnImage(rt draw.Image, str string, ox, oy int) error {
parts := &fontImageParts{str, f}
for i := 0; i < parts.Len(); i++ {
dx0, dy0, dx1, dy1 := parts.Dst(i)
sx0, sy0, _, _ := parts.Src(i)
draw.Draw(rt, image.Rect(dx0+ox, dy0+oy, dx1+ox, dy1+oy), f.origImage, image.Pt(sx0, sy0), draw.Over)
}
return nil
}
func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error { func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error {
if err := f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil { if err := f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
return err return err

View File

@ -17,8 +17,6 @@
package main package main
import ( import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/common"
"image" "image"
"image/color" "image/color"
"image/draw" "image/draw"
@ -30,6 +28,8 @@ import (
"runtime" "runtime"
"strings" "strings"
"text/template" "text/template"
"github.com/hajimehoshi/ebiten/examples/common"
) )
func licenseComment() (string, error) { func licenseComment() (string, error) {
@ -55,14 +55,11 @@ var keyboardKeys = [][]string{
{"Left", "Down", "Right"}, {"Left", "Down", "Right"},
} }
func drawKey(t *ebiten.Image, name string, x, y, width int) error { func drawKey(t *image.NRGBA, name string, x, y, width int) error {
const height = 16 const height = 16
width-- width--
shape, err := ebiten.NewImage(width, height, ebiten.FilterNearest) shape := image.NewNRGBA(image.Rect(0, 0, width, height))
if err != nil { p := shape.Pix
return err
}
p := make([]uint8, width*height*4)
for j := 0; j < height; j++ { for j := 0; j < height; j++ {
for i := 0; i < width; i++ { for i := 0; i < width; i++ {
x := (i + j*width) * 4 x := (i + j*width) * 4
@ -98,15 +95,8 @@ func drawKey(t *ebiten.Image, name string, x, y, width int) error {
} }
} }
} }
if err := shape.ReplacePixels(p); err != nil { draw.Draw(t, image.Rect(x, y, x+width, y+height), shape, image.ZP, draw.Over)
return err if err := common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5); err != nil {
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
if err := t.DrawImage(shape, op); err != nil {
return err
}
if err := common.ArcadeFont.DrawText(t, name, x+4, y+5, 1, color.White); err != nil {
return err return err
} }
return nil return nil
@ -114,10 +104,7 @@ func drawKey(t *ebiten.Image, name string, x, y, width int) error {
func outputKeyboardImage() (map[string]image.Rectangle, error) { func outputKeyboardImage() (map[string]image.Rectangle, error) {
keyMap := map[string]image.Rectangle{} keyMap := map[string]image.Rectangle{}
img, err := ebiten.NewImage(320, 240, ebiten.FilterNearest) img := image.NewNRGBA(image.Rect(0, 0, 320, 240))
if err != nil {
return nil, err
}
x, y := 0, 0 x, y := 0, 0
for j, line := range keyboardKeys { for j, line := range keyboardKeys {
x = 0 x = 0
@ -167,7 +154,7 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) {
palettedImg := image.NewPaletted(img.Bounds(), palette) palettedImg := image.NewPaletted(img.Bounds(), palette)
draw.Draw(palettedImg, palettedImg.Bounds(), img, image.ZP, draw.Src) draw.Draw(palettedImg, palettedImg.Bounds(), img, image.ZP, draw.Src)
f, err := os.Create("images/keyboard/keyboard.png") f, err := os.Create("_resources/images/keyboard/keyboard.png")
if err != nil { if err != nil {
return nil, err return nil, err
} }