change fundamental interfaces (add Picture)

This commit is contained in:
faiface 2017-02-22 20:57:22 +01:00
parent dbd90a0bcb
commit 6dc55b8746
8 changed files with 57 additions and 44 deletions

View File

@ -13,9 +13,9 @@ import (
// object.Draw(batch) // object.Draw(batch)
type Batch struct { type Batch struct {
cont TrianglesDrawer cont TrianglesDrawer
fixpic *Picture fixpic *GLPicture
pic *Picture pic *GLPicture
mat mgl32.Mat3 mat mgl32.Mat3
col NRGBA col NRGBA
} }
@ -26,7 +26,7 @@ type Batch struct {
// properties, that the supplied container supports. // properties, that the supplied container supports.
// //
// Note, that if the container does not support TrianglesColor, color masking will not work. // 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{ return &Batch{
cont: TrianglesDrawer{Triangles: container}, cont: TrianglesDrawer{Triangles: container},
fixpic: pic, 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 // 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 // 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. // 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() { if p != nil && p.Texture() != b.fixpic.Texture() {
panic("batch: attempted to draw with a different underlying Picture") panic("batch: attempted to draw with a different underlying Picture")
} }

View File

@ -162,12 +162,12 @@ func (td *TrianglesDrawer) Dirty() {
type Sprite struct { type Sprite struct {
data TrianglesData data TrianglesData
td TrianglesDrawer td TrianglesDrawer
pic *Picture pic *GLPicture
} }
// 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 *GLPicture) *Sprite {
s := &Sprite{ s := &Sprite{
data: TrianglesData{ 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)},
@ -184,7 +184,7 @@ 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 *GLPicture) {
oldPic := s.pic oldPic := s.pic
s.pic = pic s.pic = pic
if oldPic != nil && oldPic.Bounds().Size == pic.Bounds().Size { 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. // Picture returns the current Picture of the Sprite.
func (s *Sprite) Picture() *Picture { func (s *Sprite) Picture() *GLPicture {
return s.pic return s.pic
} }

View File

@ -19,9 +19,20 @@ type Target interface {
// present) when making new TargetTriangles. This varies from Target to Target. // present) when making new TargetTriangles. This varies from Target to Target.
MakeTriangles(Triangles) TargetTriangles MakeTriangles(Triangles) TargetTriangles
// These are the most basic Target "adjustment" methods. //TODO: doc
SetPicture(*Picture) 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) SetTransform(...Transform)
// SetMaskColor sets a color that will be multiplied with the TrianglesColor property of all
// Triangles.
SetMaskColor(color.Color) SetMaskColor(color.Color)
} }
@ -58,6 +69,12 @@ type Triangles interface {
Copy() Triangles 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 // TargetTriangles are Triangles generated by a Target with MakeTriangles method. They can be drawn
// onto that Target. // onto that Target.
type TargetTriangles interface { type TargetTriangles interface {
@ -67,33 +84,29 @@ type TargetTriangles interface {
Draw() Draw()
} }
//TODO: doc
type TargetPicture interface {
Picture
Draw(TargetTriangles)
}
// TrianglesPosition specifies Triangles with Position property. // TrianglesPosition specifies Triangles with Position property.
//
// Default value for a position is (0, 0).
type TrianglesPosition interface { type TrianglesPosition interface {
Triangles Triangles
Position(i int) Vec Position(i int) Vec
} }
// TrianglesColor specifies Triangles with Color property. // TrianglesColor specifies Triangles with Color property.
//
// Default value for a color is the white color.
type TrianglesColor interface { type TrianglesColor interface {
Triangles Triangles
Color(i int) NRGBA 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. // Note that this represents picture coordinates, not an actual picture.
// type TrianglesPicture interface {
// Default value for a texture is (-1, -1), which means 'no texture'.
type TrianglesTexture interface {
Triangles Triangles
Texture(i int) Vec Picture(i int) Vec
}
// Drawer is something that can be drawn onto any Target.
type Drawer interface {
Draw(Target)
} }

View File

@ -8,18 +8,18 @@ import (
"github.com/faiface/mainthread" "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 // 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-Picture" // generated. After the creation, Pictures can be sliced (slicing creates a "sub-GLPicture"
// from a Picture) into smaller Pictures. // from a GLPicture) into smaller Pictures.
type Picture struct { type GLPicture struct {
tex *glhf.Texture tex *glhf.Texture
bounds Rect bounds Rect
} }
// NewPicture creates a new Picture from an image.Image. // 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 // convert the image to NRGBA format
bounds := img.Bounds() bounds := img.Bounds()
nrgba := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy())) 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. // PictureFromTexture returns a new Picture that spans the whole supplied Texture.
func PictureFromTexture(tex *glhf.Texture) *Picture { func PictureFromTexture(tex *glhf.Texture) *GLPicture {
return &Picture{ return &GLPicture{
tex: tex, tex: tex,
bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())), 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. // Image returns the content of the Picture as an image.NRGBA.
// //
// Note, that this operation can be rather expensive. // Note, that this operation can be rather expensive.
func (p *Picture) Image() *image.NRGBA { func (p *GLPicture) Image() *image.NRGBA {
bounds := p.Bounds() bounds := p.Bounds()
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())))
@ -88,7 +88,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() *glhf.Texture { func (p *GLPicture) Texture() *glhf.Texture {
return p.tex 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, // 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. // 100, 50, 100), we get the upper-right quadrant of the original Picture.
func (p *Picture) Slice(slice Rect) *Picture { func (p *GLPicture) Slice(slice Rect) *GLPicture {
return &Picture{ return &GLPicture{
tex: p.tex, tex: p.tex,
bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size}, 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 // If the original Picture was sliced with the return value of this method, this Picture would
// be obtained. // be obtained.
func (p *Picture) Bounds() Rect { func (p *GLPicture) Bounds() Rect {
return p.bounds return p.bounds
} }

View File

@ -22,7 +22,7 @@ type Canvas struct {
drawTd pixel.TrianglesDrawer drawTd pixel.TrianglesDrawer
pic *pixel.Picture pic *pixel.GLPicture
mat mgl32.Mat3 mat mgl32.Mat3
col mgl32.Vec4 col mgl32.Vec4
bnd 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 // 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 // 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() *pixel.Picture { func (c *Canvas) Content() *pixel.GLPicture {
return pixel.PictureFromTexture(c.f.Texture()) 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. // 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. // 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 { if p != nil {
min := pictureBounds(p, pixel.V(0, 0)) min := pictureBounds(p, pixel.V(0, 0))
max := pictureBounds(p, pixel.V(1, 1)) max := pictureBounds(p, pixel.V(1, 1))

View File

@ -34,7 +34,7 @@ func transformToMat(t ...pixel.Transform) mgl32.Mat3 {
return mat 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()) w, h := float64(p.Texture().Width()), float64(p.Texture().Height())
a := p.Bounds().Pos a := p.Bounds().Pos
b := p.Bounds().Pos + p.Bounds().Size b := p.Bounds().Pos + p.Bounds().Size

View File

@ -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. // 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) w.canvas.SetPicture(p)
} }

View File

@ -31,7 +31,7 @@ func transformToMat(t ...Transform) mgl32.Mat3 {
return mat 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()) w, h := float64(p.Texture().Width()), float64(p.Texture().Height())
a := p.Bounds().Pos a := p.Bounds().Pos
b := p.Bounds().Pos + p.Bounds().Size b := p.Bounds().Pos + p.Bounds().Size