go-opengl-pixel/drawer.go

103 lines
2.4 KiB
Go
Raw Permalink Normal View History

2017-02-23 06:07:40 -06:00
package pixel
// Drawer glues all the fundamental interfaces (Target, Triangles, Picture) into a coherent and the
// only intended usage pattern.
//
// Drawer makes it possible to draw any combination of Triangles and Picture onto any Target
// efficiently.
//
// To create a Drawer, just assign it's Triangles and Picture fields:
2017-03-15 18:40:52 -05:00
//
2017-02-23 06:07:40 -06:00
// d := pixel.Drawer{Triangles: t, Picture: p}
//
// If Triangles is nil, nothing will be drawn. If Picture is nil, Triangles will be drawn without a
// Picture.
//
// Whenever you change the Triangles, call Dirty to notify Drawer that Triangles changed. You don't
2017-04-13 08:15:17 -05:00
// need to notify Drawer about a change of the Picture.
//
// Note, that Drawer caches the results of MakePicture from Targets it's drawn to for each Picture
// it's set to. What it means is that using a Drawer with an unbounded number of Pictures leads to a
// memory leak, since Drawer caches them and never forgets. In such a situation, create a new Drawer
// for each Picture.
2017-02-23 06:07:40 -06:00
type Drawer struct {
Triangles Triangles
Picture Picture
2021-10-01 13:31:18 -05:00
Cached bool
2017-02-23 06:07:40 -06:00
targets map[Target]*drawerTarget
allTargets []*drawerTarget
inited bool
2017-02-23 06:07:40 -06:00
}
2017-07-02 16:23:27 -05:00
type drawerTarget struct {
tris TargetTriangles
pics map[Picture]TargetPicture
clean bool
2017-02-23 06:07:40 -06:00
}
func (d *Drawer) lazyInit() {
if !d.inited {
2017-07-02 16:23:27 -05:00
d.targets = make(map[Target]*drawerTarget)
2017-02-23 06:07:40 -06:00
d.inited = true
}
}
// Dirty marks the Triangles of this Drawer as changed. If not called, changes will not be visible
// when drawing.
func (d *Drawer) Dirty() {
d.lazyInit()
for _, t := range d.allTargets {
2017-07-02 16:23:27 -05:00
t.clean = false
}
2017-02-23 06:07:40 -06:00
}
// Draw efficiently draws Triangles with Picture onto the provided Target.
//
// If Triangles is nil, nothing will be drawn. If Picture is nil, Triangles will be drawn without a
// Picture.
func (d *Drawer) Draw(t Target) {
d.lazyInit()
if d.Triangles == nil {
return
}
2017-07-02 16:23:27 -05:00
dt := d.targets[t]
if dt == nil {
dt = &drawerTarget{
pics: make(map[Picture]TargetPicture),
}
d.targets[t] = dt
d.allTargets = append(d.allTargets, dt)
2017-07-02 16:23:27 -05:00
}
if dt.tris == nil {
dt.tris = t.MakeTriangles(d.Triangles)
dt.clean = true
2017-02-23 06:07:40 -06:00
}
2017-07-02 16:23:27 -05:00
if !dt.clean {
dt.tris.SetLen(d.Triangles.Len())
dt.tris.Update(d.Triangles)
dt.clean = true
2017-02-23 06:07:40 -06:00
}
if d.Picture == nil {
2017-07-02 16:23:27 -05:00
dt.tris.Draw()
2017-02-23 06:07:40 -06:00
return
}
2017-07-02 16:23:27 -05:00
pic := dt.pics[d.Picture]
2017-02-23 06:07:40 -06:00
if pic == nil {
pic = t.MakePicture(d.Picture)
2021-10-01 13:31:18 -05:00
if d.Cached {
dt.pics[d.Picture] = pic
}
2017-02-23 06:07:40 -06:00
}
2017-07-02 16:23:27 -05:00
pic.Draw(dt.tris)
2017-02-23 06:07:40 -06:00
}