change Picture receiver to pointer

This commit is contained in:
faiface 2016-12-19 01:11:34 +01:00
parent 303c66d2b9
commit e616db81fc
5 changed files with 26 additions and 37 deletions

View File

@ -80,14 +80,14 @@ func (g *Group) Do(sub func(pixelgl.Context)) {
// Usually you use this type only indirectly throught other specific shapes (sprites, polygons, ...) embedding it.
type Shape struct {
parent pixelgl.Doer
picture Picture
picture *Picture
color color.Color
transform Transform
va *pixelgl.VertexArray
}
// NewShape creates a new shape with specified parent, picture, color, transform and vertex array.
func NewShape(parent pixelgl.Doer, picture Picture, c color.Color, transform Transform, va *pixelgl.VertexArray) *Shape {
func NewShape(parent pixelgl.Doer, picture *Picture, c color.Color, transform Transform, va *pixelgl.VertexArray) *Shape {
return &Shape{
parent: parent,
picture: picture,
@ -98,12 +98,12 @@ func NewShape(parent pixelgl.Doer, picture Picture, c color.Color, transform Tra
}
// SetPicture changes the picture of a shape.
func (s *Shape) SetPicture(picture Picture) {
func (s *Shape) SetPicture(picture *Picture) {
s.picture = picture
}
// Picture returns the current picture of a shape.
func (s *Shape) Picture() Picture {
func (s *Shape) Picture() *Picture {
return s.picture
}
@ -145,7 +145,7 @@ func (s *Shape) Draw(t ...Transform) {
ctx.Shader().SetUniformAttr(maskColorVec4, mgl32.Vec4{r, g, b, a})
ctx.Shader().SetUniformAttr(transformMat3, mat)
if s.picture.Texture() != nil {
if s.picture != nil {
s.picture.Texture().Do(func(pixelgl.Context) {
s.va.Draw()
})
@ -169,12 +169,12 @@ type MultiShape struct {
//
// If two of the supplied shapes have different pictures, this function panics.
func NewMultiShape(parent pixelgl.Doer, shapes ...*Shape) *MultiShape {
var picture Picture
var picture *Picture
for _, shape := range shapes {
if picture.IsNil() {
if picture == nil {
picture = shape.Picture()
} else {
if shape.Picture().IsNil() && shape.Picture() != picture {
if shape.Picture() == nil && shape.Picture().Texture() != picture.Texture() {
panic(errors.New("failed to create multishape: shapes have different pictures"))
}
}
@ -256,7 +256,7 @@ type Sprite struct {
// NewSprite creates a new sprite with the supplied picture. The sprite's size is the size of the supplied picture.
// If you want to change the sprite's size, change it's transform.
func NewSprite(parent pixelgl.Doer, picture Picture) *Sprite {
func NewSprite(parent pixelgl.Doer, picture *Picture) *Sprite {
var va *pixelgl.VertexArray
parent.Do(func(ctx pixelgl.Context) {
@ -317,7 +317,7 @@ func NewLineColor(parent pixelgl.Doer, c color.Color, a, b Vec, width float64) *
va.SetVertexAttr(i, texCoordVec2, mgl32.Vec2{-1, -1})
}
lc := &LineColor{NewShape(parent, Picture{}, c, Position(0), va), a, b, width}
lc := &LineColor{NewShape(parent, nil, c, Position(0), va), a, b, width}
lc.setPoints()
return lc
}
@ -398,7 +398,7 @@ func NewPolygonColor(parent pixelgl.Doer, c color.Color, points ...Vec) *Polygon
va.SetVertexAttr(i, texCoordVec2, mgl32.Vec2{-1, -1})
}
return &PolygonColor{NewShape(parent, Picture{}, c, Position(0), va), points}
return &PolygonColor{NewShape(parent, nil, c, Position(0), va), points}
}
// PointNum returns the number of points in a polygon.
@ -474,7 +474,7 @@ func NewEllipseColor(parent pixelgl.Doer, c color.Color, radius Vec, fill float6
va.SetVertexAttr(j, texCoordVec2, mgl32.Vec2{-1, -1})
}
return &EllipseColor{NewShape(parent, Picture{}, c, Position(0), va), radius, fill}
return &EllipseColor{NewShape(parent, nil, c, Position(0), va), radius, fill}
}
// Radius returns the radius of an ellipse.

View File

@ -18,7 +18,7 @@ type Picture struct {
}
// NewPicture creates a new picture from an image.Image.
func NewPicture(img image.Image) Picture {
func NewPicture(img image.Image) *Picture {
// convert the image to RGBA format
rgba := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(rgba, rgba.Bounds(), img, img.Bounds().Min, draw.Src)
@ -33,21 +33,16 @@ func NewPicture(img image.Image) Picture {
panic(errors.Wrap(err, "failed to create picture"))
}
return Picture{
return &Picture{
texture: texture,
bounds: R(0, 0, float64(texture.Width()), float64(texture.Height())),
}
}
// IsNil returns true if a picture is no picture.
func (p Picture) IsNil() bool {
return p.texture == nil
}
// Texture returns a pointer to the underlying OpenGL texture of a picture.
//
// Note, that the parent of this texture is pixelgl.NoOpDoer.
func (p Picture) Texture() *pixelgl.Texture {
func (p *Picture) Texture() *pixelgl.Texture {
return p.texture
}
@ -56,8 +51,8 @@ func (p Picture) Texture() *pixelgl.Texture {
//
// For example, suppose we have a 100x200 pixels picture. If we slice it with rectangle (50, 100, 50, 100), we get
// the upper-right quadrant of the original picture.
func (p Picture) Slice(slice Rect) Picture {
return Picture{
func (p *Picture) Slice(slice Rect) *Picture {
return &Picture{
texture: p.texture,
bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size},
}
@ -66,6 +61,6 @@ func (p Picture) Slice(slice Rect) Picture {
// Bounds returns the bounding rectangle of this picture relative to the most original picture.
//
// If the original picture gets sliced with the return value of this method, this picture will be obtained.
func (p Picture) Bounds() Rect {
func (p *Picture) Bounds() Rect {
return p.bounds
}

View File

@ -120,10 +120,8 @@ func NewShader(parent Doer, vertexFmt, uniformFmt AttrFormat, vertexShader, frag
}
func (s *Shader) delete() {
s.parent.Do(func(ctx Context) {
DoNoBlock(func() {
gl.DeleteProgram(s.program.obj)
})
DoNoBlock(func() {
gl.DeleteProgram(s.program.obj)
})
}

View File

@ -52,10 +52,8 @@ func NewTexture(parent Doer, width, height int, pixels []uint8) (*Texture, error
}
func (t *Texture) delete() {
t.parent.Do(func(ctx Context) {
DoNoBlock(func() {
gl.DeleteTextures(1, &t.tex.obj)
})
DoNoBlock(func() {
gl.DeleteTextures(1, &t.tex.obj)
})
}

View File

@ -115,12 +115,10 @@ func NewVertexArray(parent Doer, format AttrFormat, vertexNum int, indices []int
}
func (va *VertexArray) delete() {
va.parent.Do(func(ctx Context) {
DoNoBlock(func() {
gl.DeleteVertexArrays(1, &va.vao.obj)
gl.DeleteBuffers(1, &va.vbo.obj)
gl.DeleteBuffers(1, &va.ebo.obj)
})
DoNoBlock(func() {
gl.DeleteVertexArrays(1, &va.vao.obj)
gl.DeleteBuffers(1, &va.vbo.obj)
gl.DeleteBuffers(1, &va.ebo.obj)
})
}