feat: implementing text drawing.

This commit is contained in:
Andrey Parhomenko 2024-01-13 18:17:34 +03:00
parent 733c10f6fd
commit ab406b1249
7 changed files with 115 additions and 3 deletions

View file

@ -3,10 +3,10 @@ package main
import (
"vultras.su/core/gg"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
_ "github.com/silbinarywolf/preferdiscretegpu"
"bytes"
"log"
//"strings"
"fmt"
)
@ -60,10 +60,30 @@ func main() {
circle.Antialias = true
circle.Layer = HighestL
circle.Visible = true
e.Spawn(circle)
//e.Spawn(circle)
fmt.Println(rect.GetLayer(), player.GetLayer())
txt := &gg.Text{}
txt.Transform = gg.T()
//txt.Position = gg.V2(400)
txt.Color = gg.Rgba(1, 1, 1, 1)
txt.Layer = HighestL
txt.Visible = true
txt.Text = "Hello, World!\nПривет, Мир!"
txt.Face, err = gg.MakeFaceFromTtf(
bytes.NewReader(fonts.MPlus1pRegular_ttf),
&gg.TtfFaceOptions{
Size: 32,
DPI: 72,
Hinting: gg.FontHintingNone,
},
)
if err != nil {
panic(err)
}
e.Spawn(txt)
//fmt.Println(rect.GetLayer(), player.GetLayer())
fmt.Println("Starting...")
err = e.Run()
if err != nil {
panic(err)

11
cmd/test/text.go Normal file
View file

@ -0,0 +1,11 @@
package main
import (
"vultras.su/core/gg"
)
type Text struct {
gg.Text
}
//func (txt *Text) Update()

5
draw.go Normal file
View file

@ -0,0 +1,5 @@
package gg
// Should implement
// some general structure
// for the returned values for the Draw call.

1
go.mod
View file

@ -19,4 +19,5 @@ require (
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
)

3
go.sum
View file

@ -2,6 +2,8 @@ github.com/ebitengine/purego v0.4.0-alpha.4 h1:Y7yIV06Yo5M2BAdD7EVPhfp6LZ0tEcQo5
github.com/ebitengine/purego v0.4.0-alpha.4/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps=
github.com/hajimehoshi/bitmapfont/v2 v2.2.3/go.mod h1:sWM8ejdkGSXaQGlZcegMRx4DyEPOWYyXqsBKIs+Yhzk=
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b h1:QURPKDDBduvp9Zh5/4NeiPeUfpB4PokrfoH3tx4yNrg=
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI=
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
@ -43,6 +45,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=

69
text.go Normal file
View file

@ -0,0 +1,69 @@
package gg
import (
//"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2"
"io"
//"fmt"
)
//type FontOptions = font.Options
var (
FontHintingNone = font.HintingNone
)
type Face = font.Face
type TtfFont = opentype.Font
type TtfFaceOptions = opentype.FaceOptions
type TtfFace = opentype.Face
func MakeFaceFromTtf(src io.ReaderAt, opts *TtfFaceOptions) (Face, error) {
fnt, err := ParseTtfFont(src)
if err != nil {
return nil, err
}
face, err := NewTtfFace(fnt, opts)
if err != nil {
return nil, err
}
return face, nil
}
func ParseTtfFont(src io.ReaderAt) (*TtfFont, error) {
return opentype.ParseReaderAt(src)
}
func NewTtfFace(fnt *TtfFont, opts *TtfFaceOptions) (Face, error) {
return opentype.NewFace(fnt, opts)
}
// The type implements basic drawable text.
// (Now needs to implement rotation, scaling etc, cause now only position)
type Text struct {
Object
Transform
Text string
Face Face
Colority
Visibility
}
func (txt *Text) Draw(c *Context) []EVertex {
m := txt.Matrix()
m.Concat(c.Camera.RealMatrix())
//x, y := txt.Position.XY()
//text.Draw(c.Image)
text.DrawWithOptions(c.Image, txt.Text, txt.Face,
&ebiten.DrawImageOptions{
GeoM: m,
},
)
return nil
}

View file

@ -12,6 +12,9 @@ var (
type Vector struct {
X, Y Float
}
func (v Vector) XY() (Float, Float) {
return v.X, v.Y
}
type Point = Vector
//type Vertex = Vector