add TrianglesDrawer.Dirty and simplify code

This commit is contained in:
faiface 2017-01-25 22:02:53 +01:00
parent 2b300b48b7
commit e9b982cb87
1 changed files with 36 additions and 33 deletions

View File

@ -156,29 +156,35 @@ func (td *TrianglesDrawer) Append(t Triangles) {
td.Triangles.Append(t) td.Triangles.Append(t)
} }
// Dirty marks the underlying container as changed (dirty). If you, despite all warnings, updated
// the underlying container in a way different from td.Update or td.Append, call Dirty and
// everything will be fine :)
func (td *TrianglesDrawer) Dirty() {
td.dirty = true
}
// Sprite is a picture that can be drawn onto a Target. To change the position/rotation/scale of // Sprite is a picture that can be drawn onto a Target. To change the position/rotation/scale of
// the Sprite, use Target's SetTransform method. // the Sprite, use Target's SetTransform method.
type Sprite struct { type Sprite struct {
td TrianglesDrawer td TrianglesDrawer
data *TrianglesData data TrianglesData
pic *Picture pic *Picture
} }
// NewSprite creates a Sprite with the supplied Picture. The dimensions of the returned Sprite match // NewSprite creates a Sprite with the supplied Picture. The dimensions of the returned Sprite match
// the dimensions of the Picture. // the dimensions of the Picture.
func NewSprite(pic *Picture) *Sprite { func NewSprite(pic *Picture) *Sprite {
data := TrianglesData{ s := &Sprite{
data: TrianglesData{
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 0)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 0)},
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 1)}, {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 1)},
},
} }
s := &Sprite{ s.td = TrianglesDrawer{Triangles: &s.data}
td: TrianglesDrawer{Triangles: &data},
data: &data,
}
s.SetPicture(pic) s.SetPicture(pic)
return s return s
} }
@ -191,13 +197,13 @@ func (s *Sprite) SetPicture(pic *Picture) {
return return
} }
w, h := pic.Bounds().Size.XY() w, h := pic.Bounds().Size.XY()
(*s.data)[0].Position = V(0, 0) s.data[0].Position = V(0, 0)
(*s.data)[2].Position = V(w, h) s.data[2].Position = V(w, h)
(*s.data)[1].Position = V(w, 0) s.data[1].Position = V(w, 0)
(*s.data)[3].Position = V(0, 0) s.data[3].Position = V(0, 0)
(*s.data)[4].Position = V(w, h) s.data[4].Position = V(w, h)
(*s.data)[5].Position = V(0, h) s.data[5].Position = V(0, h)
s.td.dirty = true s.td.Dirty()
} }
// Picture returns the current Picture of the Sprite. // Picture returns the current Picture of the Sprite.
@ -214,18 +220,17 @@ func (s *Sprite) Draw(t Target) {
// Polygon is a convex polygon shape filled with a single color. // Polygon is a convex polygon shape filled with a single color.
type Polygon struct { type Polygon struct {
td TrianglesDrawer td TrianglesDrawer
data *TrianglesData data TrianglesData
col NRGBA col NRGBA
} }
// NewPolygon creates a Polygon with specified color and points. Points can be in clock-wise or // NewPolygon creates a Polygon with specified color and points. Points can be in clock-wise or
// counter-clock-wise order, it doesn't matter. They should however form a convex polygon. // counter-clock-wise order, it doesn't matter. They should however form a convex polygon.
func NewPolygon(c color.Color, points ...Vec) *Polygon { func NewPolygon(c color.Color, points ...Vec) *Polygon {
data := make(TrianglesData, len(points))
p := &Polygon{ p := &Polygon{
td: TrianglesDrawer{Triangles: &data}, data: make(TrianglesData, len(points)),
data: &data,
} }
p.td = TrianglesDrawer{Triangles: &p.data}
p.SetColor(c) p.SetColor(c)
p.SetPoints(points...) p.SetPoints(points...)
return p return p
@ -237,11 +242,10 @@ func NewPolygon(c color.Color, points ...Vec) *Polygon {
// a color mask on a Target, in such a case. // a color mask on a Target, in such a case.
func (p *Polygon) SetColor(c color.Color) { func (p *Polygon) SetColor(c color.Color) {
p.col = NRGBAModel.Convert(c).(NRGBA) p.col = NRGBAModel.Convert(c).(NRGBA)
for i := range *p.data { for i := range p.data {
(*p.data)[i].Color = p.col p.data[i].Color = p.col
} }
// dirty stuff, need to update manually p.td.Dirty()
p.td.dirty = true
} }
// Color returns the current color of the Polygon. // Color returns the current color of the Polygon.
@ -258,19 +262,18 @@ func (p *Polygon) Color() NRGBA {
func (p *Polygon) SetPoints(points ...Vec) { func (p *Polygon) SetPoints(points ...Vec) {
p.data.resize(len(points)) p.data.resize(len(points))
for i, pt := range points { for i, pt := range points {
(*p.data)[i].Position = pt p.data[i].Position = pt
(*p.data)[i].Color = p.col p.data[i].Color = p.col
(*p.data)[i].Texture = V(-1, -1) p.data[i].Texture = V(-1, -1)
} }
// dirty stuff p.td.Dirty()
p.td.dirty = true
} }
// Points returns a slice of points of the Polygon in the order they where supplied. // Points returns a slice of points of the Polygon in the order they where supplied.
func (p *Polygon) Points() []Vec { func (p *Polygon) Points() []Vec {
points := make([]Vec, p.data.Len()) points := make([]Vec, p.data.Len())
for i := range *p.data { for i := range p.data {
points[i] = (*p.data)[i].Position points[i] = p.data[i].Position
} }
return points return points
} }