From 6dc55b8746fcb32293e161142109566adc9ba964 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 22 Feb 2017 20:57:22 +0100 Subject: [PATCH] change fundamental interfaces (add Picture) --- batch.go | 8 ++++---- graphics.go | 8 ++++---- interface.go | 47 ++++++++++++++++++++++++++++++----------------- picture.go | 26 +++++++++++++------------- pixelgl/canvas.go | 6 +++--- pixelgl/util.go | 2 +- pixelgl/window.go | 2 +- util.go | 2 +- 8 files changed, 57 insertions(+), 44 deletions(-) diff --git a/batch.go b/batch.go index 72aa476..32ff464 100644 --- a/batch.go +++ b/batch.go @@ -13,9 +13,9 @@ import ( // object.Draw(batch) type Batch struct { cont TrianglesDrawer - fixpic *Picture + fixpic *GLPicture - pic *Picture + pic *GLPicture mat mgl32.Mat3 col NRGBA } @@ -26,7 +26,7 @@ type Batch struct { // properties, that the supplied container supports. // // Note, that if the container does not support TrianglesColor, color masking will not work. -func NewBatch(pic *Picture, container Triangles) *Batch { +func NewBatch(pic *GLPicture, container Triangles) *Batch { return &Batch{ cont: TrianglesDrawer{Triangles: container}, fixpic: pic, @@ -58,7 +58,7 @@ func (b *Batch) MakeTriangles(t Triangles) TargetTriangles { // SetPicture sets the current Picture that will be used with the following draws. The original // Picture of this Picture (the one from which p was obtained by slicing) must be same as the // original Picture of the Batch's Picture. -func (b *Batch) SetPicture(p *Picture) { +func (b *Batch) SetPicture(p *GLPicture) { if p != nil && p.Texture() != b.fixpic.Texture() { panic("batch: attempted to draw with a different underlying Picture") } diff --git a/graphics.go b/graphics.go index ded74b3..1ac0cf0 100644 --- a/graphics.go +++ b/graphics.go @@ -162,12 +162,12 @@ func (td *TrianglesDrawer) Dirty() { type Sprite struct { data TrianglesData td TrianglesDrawer - pic *Picture + pic *GLPicture } // NewSprite creates a Sprite with the supplied Picture. The dimensions of the returned Sprite match // the dimensions of the Picture. -func NewSprite(pic *Picture) *Sprite { +func NewSprite(pic *GLPicture) *Sprite { s := &Sprite{ data: TrianglesData{ {Position: V(0, 0), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)}, @@ -184,7 +184,7 @@ func NewSprite(pic *Picture) *Sprite { } // SetPicture changes the Picture of the Sprite and resizes it accordingly. -func (s *Sprite) SetPicture(pic *Picture) { +func (s *Sprite) SetPicture(pic *GLPicture) { oldPic := s.pic s.pic = pic if oldPic != nil && oldPic.Bounds().Size == pic.Bounds().Size { @@ -201,7 +201,7 @@ func (s *Sprite) SetPicture(pic *Picture) { } // Picture returns the current Picture of the Sprite. -func (s *Sprite) Picture() *Picture { +func (s *Sprite) Picture() *GLPicture { return s.pic } diff --git a/interface.go b/interface.go index 3c3cb09..059bc3c 100644 --- a/interface.go +++ b/interface.go @@ -19,9 +19,20 @@ type Target interface { // present) when making new TargetTriangles. This varies from Target to Target. MakeTriangles(Triangles) TargetTriangles - // These are the most basic Target "adjustment" methods. - SetPicture(*Picture) + //TODO: doc + MakePicture(Picture) TargetPicture +} + +// BasicTarget is a Target with additional basic "adjustment" methods. +type BasicTarget interface { + Target + + // SetTransform sets a Transform that transforms the TrianglesPosition property of all + // Triangles. SetTransform(...Transform) + + // SetMaskColor sets a color that will be multiplied with the TrianglesColor property of all + // Triangles. SetMaskColor(color.Color) } @@ -58,6 +69,12 @@ type Triangles interface { Copy() Triangles } +//TODO: doc +type Picture interface { + Bounds() Rect + Slice(Rect) Picture +} + // TargetTriangles are Triangles generated by a Target with MakeTriangles method. They can be drawn // onto that Target. type TargetTriangles interface { @@ -67,33 +84,29 @@ type TargetTriangles interface { Draw() } +//TODO: doc +type TargetPicture interface { + Picture + + Draw(TargetTriangles) +} + // TrianglesPosition specifies Triangles with Position property. -// -// Default value for a position is (0, 0). type TrianglesPosition interface { Triangles Position(i int) Vec } // TrianglesColor specifies Triangles with Color property. -// -// Default value for a color is the white color. type TrianglesColor interface { Triangles Color(i int) NRGBA } -// TrianglesTexture specifies Triangles with Texture propery. +// TrianglesPicture specifies Triangles with Picture propery. // -// Note that this represents texture coordinates, not an actual texture. -// -// Default value for a texture is (-1, -1), which means 'no texture'. -type TrianglesTexture interface { +// Note that this represents picture coordinates, not an actual picture. +type TrianglesPicture interface { Triangles - Texture(i int) Vec -} - -// Drawer is something that can be drawn onto any Target. -type Drawer interface { - Draw(Target) + Picture(i int) Vec } diff --git a/picture.go b/picture.go index 2a6129a..cc89f55 100644 --- a/picture.go +++ b/picture.go @@ -8,18 +8,18 @@ import ( "github.com/faiface/mainthread" ) -// Picture is a raster picture. It is usually used with sprites. +// GLPicture is a raster picture. It is usually used with sprites. // -// A Picture is created from an image.Image, that can be either loaded from a file, or -// generated. After the creation, Pictures can be sliced (slicing creates a "sub-Picture" -// from a Picture) into smaller Pictures. -type Picture struct { +// A GLPicture is created from an image.Image, that can be either loaded from a file, or +// generated. After the creation, Pictures can be sliced (slicing creates a "sub-GLPicture" +// from a GLPicture) into smaller Pictures. +type GLPicture struct { tex *glhf.Texture bounds Rect } // NewPicture creates a new Picture from an image.Image. -func NewPicture(img image.Image, smooth bool) *Picture { +func NewPicture(img image.Image, smooth bool) *GLPicture { // convert the image to NRGBA format bounds := img.Bounds() nrgba := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy())) @@ -49,8 +49,8 @@ func NewPicture(img image.Image, smooth bool) *Picture { } // PictureFromTexture returns a new Picture that spans the whole supplied Texture. -func PictureFromTexture(tex *glhf.Texture) *Picture { - return &Picture{ +func PictureFromTexture(tex *glhf.Texture) *GLPicture { + return &GLPicture{ tex: tex, bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())), } @@ -59,7 +59,7 @@ func PictureFromTexture(tex *glhf.Texture) *Picture { // Image returns the content of the Picture as an image.NRGBA. // // Note, that this operation can be rather expensive. -func (p *Picture) Image() *image.NRGBA { +func (p *GLPicture) Image() *image.NRGBA { bounds := p.Bounds() nrgba := image.NewNRGBA(image.Rect(0, 0, int(bounds.W()), int(bounds.H()))) @@ -88,7 +88,7 @@ func (p *Picture) Image() *image.NRGBA { } // Texture returns a pointer to the underlying OpenGL texture of the Picture. -func (p *Picture) Texture() *glhf.Texture { +func (p *GLPicture) Texture() *glhf.Texture { return p.tex } @@ -97,8 +97,8 @@ func (p *Picture) Texture() *glhf.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 *GLPicture) Slice(slice Rect) *GLPicture { + return &GLPicture{ tex: p.tex, bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size}, } @@ -108,6 +108,6 @@ func (p *Picture) Slice(slice Rect) *Picture { // // If the original Picture was sliced with the return value of this method, this Picture would // be obtained. -func (p *Picture) Bounds() Rect { +func (p *GLPicture) Bounds() Rect { return p.bounds } diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go index 84c12d3..00b4802 100644 --- a/pixelgl/canvas.go +++ b/pixelgl/canvas.go @@ -22,7 +22,7 @@ type Canvas struct { drawTd pixel.TrianglesDrawer - pic *pixel.Picture + pic *pixel.GLPicture mat mgl32.Mat3 col mgl32.Vec4 bnd mgl32.Vec4 @@ -109,7 +109,7 @@ func (c *Canvas) Size() (width, height float64) { // Content returns a Picture that contains the content of this Canvas. The returned Picture changes // 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). -func (c *Canvas) Content() *pixel.Picture { +func (c *Canvas) Content() *pixel.GLPicture { return pixel.PictureFromTexture(c.f.Texture()) } @@ -142,7 +142,7 @@ func (c *Canvas) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles { // SetPicture sets a Picture that will be used in further draw operations. // // This does not set the Picture that this Canvas draws onto, don't confuse it. -func (c *Canvas) SetPicture(p *pixel.Picture) { +func (c *Canvas) SetPicture(p *pixel.GLPicture) { if p != nil { min := pictureBounds(p, pixel.V(0, 0)) max := pictureBounds(p, pixel.V(1, 1)) diff --git a/pixelgl/util.go b/pixelgl/util.go index af30bf8..1c760df 100644 --- a/pixelgl/util.go +++ b/pixelgl/util.go @@ -34,7 +34,7 @@ func transformToMat(t ...pixel.Transform) mgl32.Mat3 { return mat } -func pictureBounds(p *pixel.Picture, v pixel.Vec) pixel.Vec { +func pictureBounds(p *pixel.GLPicture, v pixel.Vec) pixel.Vec { w, h := float64(p.Texture().Width()), float64(p.Texture().Height()) a := p.Bounds().Pos b := p.Bounds().Pos + p.Bounds().Size diff --git a/pixelgl/window.go b/pixelgl/window.go index cf5ce99..82d010a 100644 --- a/pixelgl/window.go +++ b/pixelgl/window.go @@ -374,7 +374,7 @@ func (w *Window) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles { } // SetPicture sets a Picture that will be used in subsequent drawings onto the Window. -func (w *Window) SetPicture(p *pixel.Picture) { +func (w *Window) SetPicture(p *pixel.GLPicture) { w.canvas.SetPicture(p) } diff --git a/util.go b/util.go index 6aafdb8..654ba4c 100644 --- a/util.go +++ b/util.go @@ -31,7 +31,7 @@ func transformToMat(t ...Transform) mgl32.Mat3 { return mat } -func pictureBounds(p *Picture, v Vec) Vec { +func pictureBounds(p *GLPicture, v Vec) Vec { w, h := float64(p.Texture().Width()), float64(p.Texture().Height()) a := p.Bounds().Pos b := p.Bounds().Pos + p.Bounds().Size