2022-11-25 10:04:41 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-19 18:50:27 +01:00
|
|
|
"bytes"
|
2022-11-25 10:04:41 +01:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
2023-11-19 18:50:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2022-11-25 10:04:41 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
whiteImage = ebiten.NewImage(3, 3)
|
|
|
|
|
|
|
|
// whiteSubImage is an internal sub image of whiteImage.
|
|
|
|
// Use whiteSubImage at DrawTriangles instead of whiteImage in order to avoid bleeding edges.
|
|
|
|
whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
whiteImage.Fill(color.White)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
2023-11-19 18:50:27 +01:00
|
|
|
path vector.Path
|
2022-11-25 10:04:41 +01:00
|
|
|
vertices []ebiten.Vertex
|
|
|
|
indices []uint16
|
|
|
|
|
|
|
|
tick int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
2023-11-19 18:50:27 +01:00
|
|
|
if g.tick == 0 {
|
|
|
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
|
2022-11-25 10:04:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-19 18:50:27 +01:00
|
|
|
op := &text.LayoutOptions{}
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = 110
|
2023-11-19 19:50:41 +01:00
|
|
|
text.AppendVectorPath(&g.path, "ABCEDFG\nabcdefg\nあいうえお\nかきくけこ", &text.GoTextFace{
|
2023-11-19 18:50:27 +01:00
|
|
|
Source: s,
|
2023-11-19 19:50:41 +01:00
|
|
|
Size: 90,
|
2023-11-19 18:50:27 +01:00
|
|
|
}, op)
|
|
|
|
}
|
2022-11-25 10:04:41 +01:00
|
|
|
|
2023-11-19 18:50:27 +01:00
|
|
|
g.tick++
|
2022-11-25 10:04:41 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
g.vertices = g.vertices[:0]
|
|
|
|
g.indices = g.indices[:0]
|
|
|
|
|
|
|
|
op := &vector.StrokeOptions{}
|
2023-11-19 18:50:27 +01:00
|
|
|
op.Width = 2*(float32(math.Sin(float64(g.tick)*2*math.Pi/180))+1) + 1
|
2022-11-25 10:04:41 +01:00
|
|
|
op.LineJoin = vector.LineJoinRound
|
|
|
|
op.LineCap = vector.LineCapRound
|
2023-11-19 18:50:27 +01:00
|
|
|
g.vertices, g.indices = g.path.AppendVerticesAndIndicesForStroke(g.vertices, g.indices, op)
|
2022-11-25 10:04:41 +01:00
|
|
|
|
|
|
|
for i := range g.vertices {
|
2023-11-19 18:50:27 +01:00
|
|
|
g.vertices[i].DstX += 50
|
2023-11-19 19:50:41 +01:00
|
|
|
g.vertices[i].DstY += 0
|
2022-11-25 10:04:41 +01:00
|
|
|
g.vertices[i].SrcX = 1
|
|
|
|
g.vertices[i].SrcY = 1
|
|
|
|
}
|
|
|
|
|
2022-11-25 15:44:48 +01:00
|
|
|
screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, &ebiten.DrawTrianglesOptions{
|
|
|
|
AntiAlias: true,
|
|
|
|
})
|
2022-11-25 10:04:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Game) Layout(width, height int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ebiten.SetWindowTitle("Font Vector (Ebitengine Demo)")
|
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|