mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
text: Refactoring: Reduce a global variable
This commit is contained in:
parent
86296ea89b
commit
fd57753089
25
text/text.go
25
text/text.go
@ -119,7 +119,6 @@ func (g *glyph) draw(dst *ebiten.Image, x, y fixed.Int26_6, clr color.Color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
glyphs = map[char]*glyph{}
|
|
||||||
atlases = map[int]*atlas{}
|
atlases = map[int]*atlas{}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -134,6 +133,8 @@ type atlas struct {
|
|||||||
// This value is always power of 2.
|
// This value is always power of 2.
|
||||||
size int
|
size int
|
||||||
|
|
||||||
|
charToGlyph map[char]*glyph
|
||||||
|
|
||||||
// glyphs is the set of glyph information.
|
// glyphs is the set of glyph information.
|
||||||
glyphs []*glyph
|
glyphs []*glyph
|
||||||
|
|
||||||
@ -165,10 +166,11 @@ func (a *atlas) append(glyph *glyph) {
|
|||||||
panic("not reached")
|
panic("not reached")
|
||||||
}
|
}
|
||||||
oldest := a.glyphs[idx]
|
oldest := a.glyphs[idx]
|
||||||
delete(glyphs, oldest.char)
|
delete(a.charToGlyph, oldest.char)
|
||||||
|
|
||||||
glyph.index = idx
|
glyph.index = idx
|
||||||
a.glyphs[idx] = glyph
|
a.glyphs[idx] = glyph
|
||||||
|
a.charToGlyph[glyph.char] = glyph
|
||||||
a.draw(glyph)
|
a.draw(glyph)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -185,6 +187,7 @@ func (a *atlas) append(glyph *glyph) {
|
|||||||
a.num++
|
a.num++
|
||||||
glyph.index = idx
|
glyph.index = idx
|
||||||
a.glyphs[idx] = glyph
|
a.glyphs[idx] = glyph
|
||||||
|
a.charToGlyph[glyph.char] = glyph
|
||||||
a.draw(glyph)
|
a.draw(glyph)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,13 +218,16 @@ func (a *atlas) draw(glyph *glyph) {
|
|||||||
|
|
||||||
func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
|
func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
|
||||||
ch := char{face, r}
|
ch := char{face, r}
|
||||||
g, ok := glyphs[ch]
|
a, ok := atlases[ch.atlasGroup()]
|
||||||
if ok {
|
if ok {
|
||||||
g.atime = now
|
g, ok := a.charToGlyph[ch]
|
||||||
return g
|
if ok {
|
||||||
|
g.atime = now
|
||||||
|
return g
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g = &glyph{
|
g := &glyph{
|
||||||
char: ch,
|
char: ch,
|
||||||
atime: now,
|
atime: now,
|
||||||
}
|
}
|
||||||
@ -229,7 +235,6 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
|
|||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
a, ok := atlases[g.char.atlasGroup()]
|
|
||||||
if !ok {
|
if !ok {
|
||||||
// Don't use ebiten.MaxImageSize here.
|
// Don't use ebiten.MaxImageSize here.
|
||||||
// It's because the back-end image pixels will be restored from GPU
|
// It's because the back-end image pixels will be restored from GPU
|
||||||
@ -244,8 +249,9 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
|
|||||||
const size = 1024
|
const size = 1024
|
||||||
i, _ := ebiten.NewImage(size, size, ebiten.FilterNearest)
|
i, _ := ebiten.NewImage(size, size, ebiten.FilterNearest)
|
||||||
a = &atlas{
|
a = &atlas{
|
||||||
image: i,
|
image: i,
|
||||||
size: g.char.atlasGroup(),
|
size: g.char.atlasGroup(),
|
||||||
|
charToGlyph: map[char]*glyph{},
|
||||||
}
|
}
|
||||||
w, h := a.image.Size()
|
w, h := a.image.Size()
|
||||||
xnum := w / a.size
|
xnum := w / a.size
|
||||||
@ -255,7 +261,6 @@ func getGlyphFromCache(face font.Face, r rune, now int64) *glyph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.append(g)
|
a.append(g)
|
||||||
glyphs[g.char] = g
|
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user