mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
examples/keyboard: Use TTF font directly instead of examples/common
This commit is contained in:
parent
81ed9903ce
commit
3b50e1ea54
Binary file not shown.
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 3.6 KiB |
@ -17,18 +17,58 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
|
||||||
"image/png"
|
"image/png"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/examples/common"
|
"github.com/golang/freetype/truetype"
|
||||||
|
"golang.org/x/image/font"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/internal"
|
"github.com/hajimehoshi/ebiten/internal"
|
||||||
|
"github.com/hajimehoshi/ebiten/text"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
arcadeFontSize = 8
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
arcadeFont font.Face
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
f, err := ebitenutil.OpenFile(filepath.Join("..", "..", "_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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var keyboardKeys = [][]string{
|
var keyboardKeys = [][]string{
|
||||||
{"Esc", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "\\", "`", " "},
|
{"Esc", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "\\", "`", " "},
|
||||||
{"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "BS"},
|
{"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "BS"},
|
||||||
@ -40,11 +80,11 @@ var keyboardKeys = [][]string{
|
|||||||
{"Left", "Down", "Right"},
|
{"Left", "Down", "Right"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawKey(t *image.NRGBA, name string, x, y, width int) {
|
func drawKey(t *ebiten.Image, name string, x, y, width int) {
|
||||||
const height = 16
|
const height = 16
|
||||||
width--
|
width--
|
||||||
shape := image.NewNRGBA(image.Rect(0, 0, width, height))
|
img, _ := ebiten.NewImage(width, height, ebiten.FilterNearest)
|
||||||
p := shape.Pix
|
p := make([]byte, 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
|
||||||
@ -80,13 +120,18 @@ func drawKey(t *image.NRGBA, name string, x, y, width int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
draw.Draw(t, image.Rect(x, y, x+width, y+height), shape, image.ZP, draw.Over)
|
img.ReplacePixels(p)
|
||||||
common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5)
|
const offset = 4
|
||||||
|
text.Draw(img, name, arcadeFont, offset, arcadeFontSize+offset, color.White)
|
||||||
|
|
||||||
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
op.GeoM.Translate(float64(x), float64(y))
|
||||||
|
t.DrawImage(img, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := image.NewNRGBA(image.Rect(0, 0, 320, 240))
|
img, _ := ebiten.NewImage(320, 240, ebiten.FilterNearest)
|
||||||
x, y := 0, 0
|
x, y := 0, 0
|
||||||
for j, line := range keyboardKeys {
|
for j, line := range keyboardKeys {
|
||||||
x = 0
|
x = 0
|
||||||
@ -130,18 +175,13 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) {
|
|||||||
y += height
|
y += height
|
||||||
}
|
}
|
||||||
|
|
||||||
palette := color.Palette([]color.Color{
|
f, err := os.Create(filepath.Join("..", "..", "_resources", "images", "keyboard", "keyboard.png"))
|
||||||
color.Transparent, color.Opaque,
|
|
||||||
})
|
|
||||||
palettedImg := image.NewPaletted(img.Bounds(), palette)
|
|
||||||
draw.Draw(palettedImg, palettedImg.Bounds(), img, image.ZP, draw.Src)
|
|
||||||
|
|
||||||
f, err := os.Create("../../_resources/images/keyboard/keyboard.png")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if err := png.Encode(f, palettedImg); err != nil {
|
|
||||||
|
if err := png.Encode(f, img); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return keyMap, nil
|
return keyMap, nil
|
||||||
@ -194,12 +234,21 @@ func outputKeyRectsGo(k map[string]image.Rectangle) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var regularTermination = errors.New("regular termination")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
m, err := outputKeyboardImage()
|
var rects map[string]image.Rectangle
|
||||||
if err != nil {
|
if err := ebiten.Run(func(_ *ebiten.Image) error {
|
||||||
|
var err error
|
||||||
|
rects, err = outputKeyboardImage()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return regularTermination
|
||||||
|
}, 256, 256, 1, ""); err != regularTermination {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := outputKeyRectsGo(m); err != nil {
|
if err := outputKeyRectsGo(rects); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user