optimize Drawer (reduce map access)
This commit is contained in:
parent
2c9c36ba28
commit
e41c8b585b
61
drawer.go
61
drawer.go
|
@ -24,23 +24,19 @@ type Drawer struct {
|
||||||
Triangles Triangles
|
Triangles Triangles
|
||||||
Picture Picture
|
Picture Picture
|
||||||
|
|
||||||
tris map[Target]TargetTriangles
|
targets map[Target]*drawerTarget
|
||||||
clean map[Target]bool
|
inited bool
|
||||||
pics map[targetPicturePair]TargetPicture
|
|
||||||
dirty bool
|
|
||||||
inited bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type targetPicturePair struct {
|
type drawerTarget struct {
|
||||||
Target Target
|
tris TargetTriangles
|
||||||
Picture Picture
|
pics map[Picture]TargetPicture
|
||||||
|
clean bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Drawer) lazyInit() {
|
func (d *Drawer) lazyInit() {
|
||||||
if !d.inited {
|
if !d.inited {
|
||||||
d.tris = make(map[Target]TargetTriangles)
|
d.targets = make(map[Target]*drawerTarget)
|
||||||
d.clean = make(map[Target]bool)
|
|
||||||
d.pics = make(map[targetPicturePair]TargetPicture)
|
|
||||||
d.inited = true
|
d.inited = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +46,9 @@ func (d *Drawer) lazyInit() {
|
||||||
func (d *Drawer) Dirty() {
|
func (d *Drawer) Dirty() {
|
||||||
d.lazyInit()
|
d.lazyInit()
|
||||||
|
|
||||||
d.dirty = true
|
for _, t := range d.targets {
|
||||||
|
t.clean = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw efficiently draws Triangles with Picture onto the provided Target.
|
// Draw efficiently draws Triangles with Picture onto the provided Target.
|
||||||
|
@ -60,40 +58,39 @@ func (d *Drawer) Dirty() {
|
||||||
func (d *Drawer) Draw(t Target) {
|
func (d *Drawer) Draw(t Target) {
|
||||||
d.lazyInit()
|
d.lazyInit()
|
||||||
|
|
||||||
if d.dirty {
|
|
||||||
for t := range d.clean {
|
|
||||||
d.clean[t] = false
|
|
||||||
}
|
|
||||||
d.dirty = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.Triangles == nil {
|
if d.Triangles == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tri := d.tris[t]
|
dt := d.targets[t]
|
||||||
if tri == nil {
|
if dt == nil {
|
||||||
tri = t.MakeTriangles(d.Triangles)
|
dt = &drawerTarget{
|
||||||
d.tris[t] = tri
|
pics: make(map[Picture]TargetPicture),
|
||||||
d.clean[t] = true
|
}
|
||||||
|
d.targets[t] = dt
|
||||||
}
|
}
|
||||||
|
|
||||||
if !d.clean[t] {
|
if dt.tris == nil {
|
||||||
tri.SetLen(d.Triangles.Len())
|
dt.tris = t.MakeTriangles(d.Triangles)
|
||||||
tri.Update(d.Triangles)
|
dt.clean = true
|
||||||
d.clean[t] = true
|
}
|
||||||
|
|
||||||
|
if !dt.clean {
|
||||||
|
dt.tris.SetLen(d.Triangles.Len())
|
||||||
|
dt.tris.Update(d.Triangles)
|
||||||
|
dt.clean = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.Picture == nil {
|
if d.Picture == nil {
|
||||||
tri.Draw()
|
dt.tris.Draw()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pic := d.pics[targetPicturePair{t, d.Picture}]
|
pic := dt.pics[d.Picture]
|
||||||
if pic == nil {
|
if pic == nil {
|
||||||
pic = t.MakePicture(d.Picture)
|
pic = t.MakePicture(d.Picture)
|
||||||
d.pics[targetPicturePair{t, d.Picture}] = pic
|
dt.pics[d.Picture] = pic
|
||||||
}
|
}
|
||||||
|
|
||||||
pic.Draw(tri)
|
pic.Draw(dt.tris)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue