add PictureFromTexture function

This commit is contained in:
faiface 2017-01-26 23:19:50 +01:00
parent e670735858
commit fc8dad2fc9
2 changed files with 18 additions and 17 deletions

View File

@ -108,11 +108,7 @@ func (c *Canvas) Size() (width, height float64) {
// as you draw onto the Canvas, so there is no real need to call this method more than once (but it // as you draw onto the Canvas, so there is no real need to call this method more than once (but it
// might be beneficial to your code to do so). // might be beneficial to your code to do so).
func (c *Canvas) Content() *Picture { func (c *Canvas) Content() *Picture {
tex := c.f.Texture() return PictureFromTexture(c.f.Texture())
return &Picture{
texture: tex,
bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())),
}
} }
// Clear fills the whole Canvas with one specified color. // Clear fills the whole Canvas with one specified color.

View File

@ -14,8 +14,8 @@ import (
// generated. After the creation, Pictures can be sliced (slicing creates a "sub-Picture" // generated. After the creation, Pictures can be sliced (slicing creates a "sub-Picture"
// from a Picture) into smaller Pictures. // from a Picture) into smaller Pictures.
type Picture struct { type Picture struct {
texture *pixelgl.Texture tex *pixelgl.Texture
bounds Rect bounds Rect
} }
// NewPicture creates a new Picture from an image.Image. // NewPicture creates a new Picture from an image.Image.
@ -35,9 +35,9 @@ func NewPicture(img image.Image, smooth bool) *Picture {
copy(jSlice, tmp) copy(jSlice, tmp)
} }
var texture *pixelgl.Texture var tex *pixelgl.Texture
mainthread.Call(func() { mainthread.Call(func() {
texture = pixelgl.NewTexture( tex = pixelgl.NewTexture(
img.Bounds().Dx(), img.Bounds().Dx(),
img.Bounds().Dy(), img.Bounds().Dy(),
smooth, smooth,
@ -45,9 +45,14 @@ func NewPicture(img image.Image, smooth bool) *Picture {
) )
}) })
return PictureFromTexture(tex)
}
// PictureFromTexture returns a new Picture that spans the whole supplied Texture.
func PictureFromTexture(tex *pixelgl.Texture) *Picture {
return &Picture{ return &Picture{
texture: texture, tex: tex,
bounds: R(0, 0, float64(texture.Width()), float64(texture.Height())), bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())),
} }
} }
@ -57,14 +62,14 @@ func (p *Picture) Image() *image.NRGBA {
nrgba := image.NewNRGBA(image.Rect(0, 0, int(bounds.W()), int(bounds.H()))) nrgba := image.NewNRGBA(image.Rect(0, 0, int(bounds.W()), int(bounds.H())))
mainthread.Call(func() { mainthread.Call(func() {
p.texture.Begin() p.tex.Begin()
nrgba.Pix = p.texture.Pixels( nrgba.Pix = p.tex.Pixels(
int(bounds.X()), int(bounds.X()),
int(bounds.Y()), int(bounds.Y()),
int(bounds.W()), int(bounds.W()),
int(bounds.H()), int(bounds.H()),
) )
p.texture.End() p.tex.End()
}) })
// flip the image vertically // flip the image vertically
@ -82,7 +87,7 @@ func (p *Picture) Image() *image.NRGBA {
// Texture returns a pointer to the underlying OpenGL texture of the Picture. // Texture returns a pointer to the underlying OpenGL texture of the Picture.
func (p *Picture) Texture() *pixelgl.Texture { func (p *Picture) Texture() *pixelgl.Texture {
return p.texture return p.tex
} }
// Slice returns a Picture within the supplied rectangle of the original picture. The original // Slice returns a Picture within the supplied rectangle of the original picture. The original
@ -92,8 +97,8 @@ func (p *Picture) Texture() *pixelgl.Texture {
// 100, 50, 100), we get the upper-right quadrant of the original Picture. // 100, 50, 100), we get the upper-right quadrant of the original Picture.
func (p *Picture) Slice(slice Rect) *Picture { func (p *Picture) Slice(slice Rect) *Picture {
return &Picture{ return &Picture{
texture: p.texture, tex: p.tex,
bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size}, bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size},
} }
} }