optimize Sprite
This commit is contained in:
parent
bc97fa00b8
commit
16c1672c61
26
graphics.go
26
graphics.go
|
@ -157,14 +157,24 @@ func (td *TrianglesDrawer) Append(t Triangles) {
|
||||||
// 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
|
||||||
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{
|
||||||
|
{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, 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(1, 1)},
|
||||||
|
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 1)},
|
||||||
|
}
|
||||||
s := &Sprite{
|
s := &Sprite{
|
||||||
td: TrianglesDrawer{Triangles: &TrianglesData{}},
|
td: TrianglesDrawer{Triangles: data},
|
||||||
|
data: data,
|
||||||
}
|
}
|
||||||
s.SetPicture(pic)
|
s.SetPicture(pic)
|
||||||
return s
|
return s
|
||||||
|
@ -173,14 +183,12 @@ func NewSprite(pic *Picture) *Sprite {
|
||||||
// SetPicture changes the Picture of the Sprite and resizes it accordingly.
|
// SetPicture changes the Picture of the Sprite and resizes it accordingly.
|
||||||
func (s *Sprite) SetPicture(pic *Picture) {
|
func (s *Sprite) SetPicture(pic *Picture) {
|
||||||
w, h := pic.Bounds().Size.XY()
|
w, h := pic.Bounds().Size.XY()
|
||||||
s.td.Update(&TrianglesData{
|
(*s.data)[0].Position = V(0, 0)
|
||||||
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
|
(*s.data)[2].Position = V(w, h)
|
||||||
{Position: V(w, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 0)},
|
(*s.data)[1].Position = V(w, 0)
|
||||||
{Position: V(w, h), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
|
(*s.data)[3].Position = V(0, 0)
|
||||||
{Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
|
(*s.data)[4].Position = V(w, h)
|
||||||
{Position: V(w, h), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
|
(*s.data)[5].Position = V(0, h)
|
||||||
{Position: V(0, h), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 1)},
|
|
||||||
})
|
|
||||||
s.pic = pic
|
s.pic = pic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue