-
Notifications
You must be signed in to change notification settings - Fork 11
/
text.go
207 lines (184 loc) · 5.21 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2021 Google LLC
//
// 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 misc
import (
"fmt"
"image"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/divVerent/aaaaxy/internal/engine"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/font"
"github.com/divVerent/aaaaxy/internal/fun"
"github.com/divVerent/aaaaxy/internal/level"
"github.com/divVerent/aaaaxy/internal/log"
m "github.com/divVerent/aaaaxy/internal/math"
"github.com/divVerent/aaaaxy/internal/palette"
"github.com/divVerent/aaaaxy/internal/playerstate"
)
var (
precacheText = flag.Bool("precache_text", true, "preload all text objects at startup (VERY recommended)")
memImagesForStaticText = flag.Bool("mem_images_for_static_text", true, "use in-memory images for static text objects (faster startup)")
memImagesForDynamicText = flag.Bool("mem_images_for_dynamic_text", false, "use in-memory images for dynamic text objects (seems to update slower in-game)")
)
// Text is a simple entity type that renders text.
type Text struct {
SpriteBase
World *engine.World
Entity *engine.Entity
Key textCacheKey
MyImage bool
}
var _ engine.Precacher = &Text{}
type textCacheKey struct {
font string
fg, bg string
text string
}
var textCache = map[textCacheKey]*ebiten.Image{}
func cacheKey(sp *level.SpawnableProps) textCacheKey {
return textCacheKey{
font: sp.Properties["text_font"],
fg: sp.Properties["text_fg"],
bg: sp.Properties["text_bg"],
text: sp.Properties["text"],
}
}
func (key textCacheKey) load(ps *playerstate.PlayerState) (*ebiten.Image, error) {
fnt := font.ByName[key.font]
if fnt.Face == nil {
return nil, fmt.Errorf("could not find font %q", key.font)
}
fg, err := palette.Parse(key.fg, "text_fg")
if err != nil {
return nil, fmt.Errorf("could not decode color %q: %w", key.fg, err)
}
bg, err := palette.Parse(key.bg, "text_bg")
if err != nil {
return nil, fmt.Errorf("could not decode color %q: %w", key.bg, err)
}
txt, err := fun.TryFormatText(ps, key.text)
if err != nil {
if ps == nil {
// On template execution failure, we do not fail precaching.
// However later rendering may fail too then.
return nil, nil
}
return nil, err
}
txt = strings.ReplaceAll(txt, " ", "\n")
bounds := fnt.BoundString(txt)
useMemImages := *memImagesForStaticText
if ps != nil {
useMemImages = *memImagesForDynamicText
}
if useMemImages {
img := image.NewRGBA( // image.RGBA is Ebitengine's fast path.
image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: bounds.Size.DX,
Y: bounds.Size.DY,
},
})
fnt.Draw(img, txt, bounds.Origin.Mul(-1), false, fg, bg)
img2 := ebiten.NewImageFromImage(img)
return img2, nil
} else {
img := ebiten.NewImage(bounds.Size.DX, bounds.Size.DY)
fnt.Draw(img, txt, bounds.Origin.Mul(-1), false, fg, bg)
return img, nil
}
}
func ClearPrecache() {
/*
// Can't dispose them right away, as the images are still referenced.
// Instead, the GC will eventually do this.
for _, img := range textCache {
img.Dispose()
}
*/
textCache = map[textCacheKey]*ebiten.Image{}
}
func (t *Text) Precache(id level.EntityID, sp *level.SpawnableProps) error {
if !*precacheText {
return nil
}
log.Debugf("precaching text for entity %v", id)
key := cacheKey(sp)
if textCache[key] != nil {
return nil
}
img, err := key.load(nil)
if err != nil {
return fmt.Errorf("could not precache text image for entity %v: %w", sp, err)
}
textCache[key] = img
return nil
}
func (t *Text) Spawn(w *engine.World, sp *level.SpawnableProps, e *engine.Entity) error {
if sp.Properties["no_flip"] == "" {
sp.Properties["no_flip"] = "x"
}
t.World = w
t.Entity = e
t.Key = cacheKey(sp)
err := t.updateText()
if err != nil {
return err
}
e.ResizeImage = false
return t.SpriteBase.Spawn(w, sp, e)
}
func (t *Text) updateText() error {
if t.MyImage {
t.Entity.Image.Dispose()
}
t.Entity.Image = nil
if *precacheText {
var found bool
t.Entity.Image, found = textCache[t.Key]
t.MyImage = false
if !found {
return fmt.Errorf("could not find precached text image for entity %v", t.Key)
}
}
if t.Entity.Image == nil {
var err error
t.Entity.Image, err = t.Key.load(&t.World.PlayerState)
t.MyImage = true
if err != nil {
return fmt.Errorf("could not render text image for entity %v: %w", t.Key, err)
}
}
dx, dy := t.Entity.Image.Size()
if t.Entity.Orientation.Right.DX == 0 {
dx, dy = dy, dx
}
centerOffset := t.Entity.Rect.Size.Sub(m.Delta{DX: dx, DY: dy}).Div(2)
t.Entity.RenderOffset = centerOffset
return nil
}
func (t *Text) Despawn() {
if t.MyImage {
t.Entity.Image.Dispose()
}
t.Entity.Image = nil
}
func init() {
engine.RegisterEntityType(&Text{})
}