mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
examples/keyboard: Generate the image without Ebiten (#210)
This commit is contained in:
parent
acca1c2480
commit
64a66c57c0
@ -53,7 +53,7 @@ func text_png() (*asset, error) {
|
||||
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}
|
||||
return a, nil
|
||||
}
|
||||
|
@ -15,7 +15,9 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"math"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@ -31,6 +33,7 @@ var (
|
||||
|
||||
type Font struct {
|
||||
image *ebiten.Image
|
||||
origImage image.Image
|
||||
offset int
|
||||
charNumPerLine int
|
||||
charWidth int
|
||||
@ -52,11 +55,11 @@ func init() {
|
||||
}
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
ArcadeFont = &Font{arcadeFontImage, 32, 16, 8, 8}
|
||||
ArcadeFont = &Font{arcadeFontImage, origImage, 32, 16, 8, 8}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
if err := f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
|
||||
return err
|
||||
|
@ -17,8 +17,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/examples/common"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
@ -30,6 +28,8 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/examples/common"
|
||||
)
|
||||
|
||||
func licenseComment() (string, error) {
|
||||
@ -55,14 +55,11 @@ var keyboardKeys = [][]string{
|
||||
{"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
|
||||
width--
|
||||
shape, err := ebiten.NewImage(width, height, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := make([]uint8, width*height*4)
|
||||
shape := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
p := shape.Pix
|
||||
for j := 0; j < height; j++ {
|
||||
for i := 0; i < width; i++ {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
draw.Draw(t, image.Rect(x, y, x+width, y+height), shape, image.ZP, draw.Over)
|
||||
if err := common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5); err != nil {
|
||||
return err
|
||||
}
|
||||
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) {
|
||||
keyMap := map[string]image.Rectangle{}
|
||||
img, err := ebiten.NewImage(320, 240, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
img := image.NewNRGBA(image.Rect(0, 0, 320, 240))
|
||||
x, y := 0, 0
|
||||
for j, line := range keyboardKeys {
|
||||
x = 0
|
||||
@ -167,7 +154,7 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) {
|
||||
palettedImg := image.NewPaletted(img.Bounds(), palette)
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user