Skip to content

Commit

Permalink
change fundamental interfaces (add Picture)
Browse files Browse the repository at this point in the history
  • Loading branch information
faiface committed Feb 22, 2017
1 parent dbd90a0 commit 6dc55b8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 44 deletions.
8 changes: 4 additions & 4 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
47 changes: 30 additions & 17 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
26 changes: 13 additions & 13 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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())),
}
Expand All @@ -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())))

Expand Down Expand Up @@ -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
}

Expand All @@ -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},
}
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions pixelgl/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pixelgl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pixelgl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6dc55b8

Please sign in to comment.