2020-07-29 17:19:08 +02:00
|
|
|
// Copyright 2020 The Ebiten 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.
|
|
|
|
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2020-07-29 17:19:08 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
2020-10-03 16:12:39 +02:00
|
|
|
"golang.org/x/image/font/opentype"
|
2020-07-29 17:19:08 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
2020-07-29 17:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
)
|
|
|
|
|
|
|
|
const sampleText = `The quick brown fox jumps
|
|
|
|
over the lazy dog.`
|
|
|
|
|
|
|
|
var (
|
|
|
|
mplusNormalFont font.Face
|
|
|
|
mplusBigFont font.Face
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-03 16:12:39 +02:00
|
|
|
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
|
2020-07-29 17:19:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const dpi = 72
|
2020-10-03 16:12:39 +02:00
|
|
|
mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
2020-07-29 17:19:08 +02:00
|
|
|
Size: 24,
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
2020-07-29 17:19:08 +02:00
|
|
|
Size: 32,
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
counter int
|
|
|
|
kanjiText []rune
|
|
|
|
kanjiTextColor color.RGBA
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-07-29 17:19:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
gray := color.RGBA{0x80, 0x80, 0x80, 0xff}
|
|
|
|
|
|
|
|
{
|
|
|
|
const x, y = 20, 40
|
|
|
|
b := text.BoundString(mplusNormalFont, sampleText)
|
|
|
|
ebitenutil.DrawRect(screen, float64(b.Min.X+x), float64(b.Min.Y+y), float64(b.Dx()), float64(b.Dy()), gray)
|
|
|
|
text.Draw(screen, sampleText, mplusNormalFont, x, y, color.White)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const x, y = 20, 140
|
|
|
|
b := text.BoundString(mplusBigFont, sampleText)
|
|
|
|
ebitenutil.DrawRect(screen, float64(b.Min.X+x), float64(b.Min.Y+y), float64(b.Dx()), float64(b.Dy()), gray)
|
|
|
|
text.Draw(screen, sampleText, mplusBigFont, x, y, color.White)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2020-07-29 20:11:06 +02:00
|
|
|
ebiten.SetWindowTitle("Text (Ebiten Demo)")
|
2020-07-29 17:19:08 +02:00
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|