optimize Text.Write (remove one allocation)

This commit is contained in:
faiface 2017-05-03 00:13:39 +02:00
parent bc145eb1b8
commit 424b6e0f0b
1 changed files with 8 additions and 7 deletions

View File

@ -43,6 +43,7 @@ type Text struct {
prevR rune prevR rune
atlas atlas atlas atlas
glyph pixel.TrianglesData
tris pixel.TrianglesData tris pixel.TrianglesData
d pixel.Drawer d pixel.Drawer
} }
@ -57,6 +58,7 @@ func New(face font.Face, runeSets ...[]rune) *Text {
color: pixel.Alpha(1), color: pixel.Alpha(1),
atlas: makeAtlas(face, runes), atlas: makeAtlas(face, runes),
} }
txt.glyph.SetLen(6)
txt.d.Picture = txt.atlas.pic txt.d.Picture = txt.atlas.pic
txt.d.Triangles = &txt.tris txt.d.Triangles = &txt.tris
@ -82,10 +84,9 @@ func (txt *Text) Write(p []byte) (n int, err error) {
n = len(p) n = len(p)
glyphTris := make(pixel.TrianglesData, 6) for i := range txt.glyph {
for i := range glyphTris { txt.glyph[i].Color = txt.color
glyphTris[i].Color = txt.color txt.glyph[i].Intensity = 1
glyphTris[i].Intensity = 1
} }
for len(p) > 0 { for len(p) > 0 {
@ -120,11 +121,11 @@ func (txt *Text) Write(p []byte) (n int, err error) {
d := pixel.V(glyph.frame.Min.X(), glyph.frame.Max.Y()) d := pixel.V(glyph.frame.Min.X(), glyph.frame.Max.Y())
for i, v := range []pixel.Vec{a, b, c, a, c, d} { for i, v := range []pixel.Vec{a, b, c, a, c, d} {
glyphTris[i].Position = v - glyph.orig + txt.Dot txt.glyph[i].Position = v - glyph.orig + txt.Dot
glyphTris[i].Picture = v txt.glyph[i].Picture = v
} }
txt.tris = append(txt.tris, glyphTris...) txt.tris = append(txt.tris, txt.glyph...)
txt.Dot += pixel.X(glyph.advance) txt.Dot += pixel.X(glyph.advance)
txt.prevR = r txt.prevR = r