ebitenutil: use bitmapfont for the debug print

This removes ebitenutil/internal/assets.
This commit is contained in:
Hajime Hoshi 2022-08-01 01:13:23 +09:00
parent caa04c45c3
commit dad72b874f
8 changed files with 101 additions and 81 deletions

View File

@ -12,20 +12,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate go run gen.go
//go:generate go run github.com/hajimehoshi/file2byteslice/cmd/file2byteslice -input text.png -output text.png.go -package ebitenutil -var textPng
package ebitenutil
import (
"bytes"
"image"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil/internal/assets"
)
var (
debugPrintTextImage = ebiten.NewImageFromImage(assets.CreateTextImage())
debugPrintTextImage *ebiten.Image
debugPrintTextSubImages = map[rune]*ebiten.Image{}
)
func init() {
img, _, err := image.Decode(bytes.NewReader(textPng))
if err != nil {
panic(err)
}
debugPrintTextImage = ebiten.NewImageFromImage(img)
}
// DebugPrint draws the string str on the image on left top corner.
//
// The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
@ -51,8 +63,8 @@ func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) {
w, _ := debugPrintTextImage.Size()
for _, c := range str {
const (
cw = assets.CharWidth
ch = assets.CharHeight
cw = 6
ch = 16
)
if c == '\n' {
x = 0

80
ebitenutil/gen.go Normal file
View File

@ -0,0 +1,80 @@
// Copyright 2022 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ignore
// +build ignore
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
"github.com/hajimehoshi/bitmapfont/v2"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
func run() error {
// These values are copied from an example in github.com/hajimehoshi/bitmapfont.
const (
charWidth = 6
lineHeight = 16
dotX = 4
dotY = 12
)
var lines []string
for j := 0; j < 8; j++ {
var line string
for i := 0; i < 32; i++ {
line += string(rune(i + j*32))
}
lines = append(lines, line)
}
d := font.Drawer{
Dst: image.NewRGBA(image.Rect(0, 0, charWidth*32, lineHeight*8)),
Src: image.NewUniform(color.White),
Face: bitmapfont.Face,
Dot: fixed.P(dotX, dotY),
}
for _, line := range lines {
d.Dot.X = fixed.I(dotX)
d.DrawString(line)
d.Dot.Y += fixed.I(lineHeight)
}
f, err := os.Create("text.png")
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, d.Dst); err != nil {
return err
}
return nil
}

View File

@ -1,53 +0,0 @@
// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate png2compressedrgba -input text.png -output /tmp/compressedTextRGBA
//go:generate go run github.com/hajimehoshi/file2byteslice/cmd/file2byteslice -input /tmp/compressedTextRGBA -output textrgba.go -package assets -var compressedTextRGBA
//go:generate gofmt -s -w .
package assets
import (
"bytes"
"compress/gzip"
"fmt"
"image"
"io/ioutil"
)
const (
imgWidth = 192
imgHeight = 128
CharWidth = 6
CharHeight = 16
)
func CreateTextImage() *image.RGBA {
s, err := gzip.NewReader(bytes.NewReader(compressedTextRGBA))
if err != nil {
panic(fmt.Sprintf("assets: gzip.NewReader failed: %v", err))
}
defer s.Close()
pix, err := ioutil.ReadAll(s)
if err != nil {
panic(fmt.Sprintf("assets: ioutil.ReadAll failed: %v", err))
}
return &image.RGBA{
Pix: pix,
Stride: 4 * imgWidth,
Rect: image.Rect(0, 0, imgWidth, imgHeight),
}
}

View File

@ -1,19 +0,0 @@
# License
## text.png
```
-
M+ BITMAP FONTS Copyright 2002-2005 COZ <coz@users.sourceforge.jp>
-
LICENSE
These fonts are free softwares.
Unlimited permission is granted to use, copy, and distribute it, with
or without modification, either commercially and noncommercially.
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

File diff suppressed because one or more lines are too long

BIN
ebitenutil/text.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

5
ebitenutil/text.png.go Normal file

File diff suppressed because one or more lines are too long