examples/keyboard: Embed keyboard.png (#552)

This commit is contained in:
Hajime Hoshi 2018-03-14 04:04:29 +09:00
parent b08f259674
commit 420c4306d0
3 changed files with 25 additions and 5 deletions

View File

@ -24,6 +24,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"text/template" "text/template"
@ -174,7 +175,7 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) {
y += height y += height
} }
f, err := os.Create(filepath.Join("..", "..", "_resources", "images", "keyboard", "keyboard.png")) f, err := ioutil.TempFile("", "ebiten")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -183,6 +184,17 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) {
if err := png.Encode(f, img); err != nil { if err := png.Encode(f, img); err != nil {
return nil, err return nil, err
} }
args := []string{
"-package=keyboard",
"-input=" + f.Name(),
"-output=../../resources/images/keyboard/keyboard.go",
"-var=Keyboard_png",
}
if err := exec.Command("file2byteslice", args...).Run(); err != nil {
return nil, err
}
return keyMap, nil return keyMap, nil
} }

View File

@ -17,13 +17,14 @@
package main package main
import ( import (
_ "image/png" "bytes"
"image/png"
"log" "log"
"strconv" "strconv"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/keyboard/keyboard" "github.com/hajimehoshi/ebiten/examples/keyboard/keyboard"
rkeyabord "github.com/hajimehoshi/ebiten/examples/resources/images/keyboard"
) )
const ( const (
@ -34,11 +35,12 @@ const (
var keyboardImage *ebiten.Image var keyboardImage *ebiten.Image
func init() { func init() {
var err error img, err := png.Decode(bytes.NewReader(rkeyabord.Keyboard_png))
keyboardImage, _, err = ebitenutil.NewImageFromFile("_resources/images/keyboard/keyboard.png", ebiten.FilterDefault)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
keyboardImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
} }
var keyNames = map[ebiten.Key]string{ var keyNames = map[ebiten.Key]string{

File diff suppressed because one or more lines are too long