fix PictureData.Color (off by 1)

This commit is contained in:
faiface 2017-03-06 13:05:45 +01:00
parent c91d49dadf
commit 146797bad5
2 changed files with 22 additions and 10 deletions

View File

@ -249,7 +249,7 @@ func (pd *PictureData) Image() *image.NRGBA {
} }
func (pd *PictureData) offset(at Vec) int { func (pd *PictureData) offset(at Vec) int {
at -= pd.Rect.Pos at -= pd.Rect.Pos.Map(math.Floor)
x, y := int(at.X()), int(at.Y()) x, y := int(at.X()), int(at.Y())
return y*pd.Stride + x return y*pd.Stride + x
} }

View File

@ -3,6 +3,7 @@ package pixelgl
import ( import (
"fmt" "fmt"
"image/color" "image/color"
"math"
"github.com/faiface/glhf" "github.com/faiface/glhf"
"github.com/faiface/mainthread" "github.com/faiface/mainthread"
@ -68,23 +69,34 @@ func (c *Canvas) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles {
// //
// PictureColor is supported. // PictureColor is supported.
func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture { func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture {
pd := pixel.PictureDataFromPicture(p) bounds := p.Bounds()
pixels := make([]uint8, 4*len(pd.Pix)) bx, by, bw, bh := discreteBounds(bounds)
for i := range pd.Pix {
pixels[i*4+0] = uint8(pd.Pix[i].R * 255) pixels := make([]uint8, 4*bw*bh)
pixels[i*4+1] = uint8(pd.Pix[i].G * 255) if p, ok := p.(pixel.PictureColor); ok {
pixels[i*4+2] = uint8(pd.Pix[i].B * 255) for y := 0; y < bh; y++ {
pixels[i*4+3] = uint8(pd.Pix[i].A * 255) for x := 0; x < bw; x++ {
at := pixel.V(
math.Max(float64(bx+x), bounds.Pos.X()),
math.Max(float64(by+y), bounds.Pos.Y()),
)
color := p.Color(at)
pixels[(y*bw+x)*4+0] = uint8(color.R * 255)
pixels[(y*bw+x)*4+1] = uint8(color.G * 255)
pixels[(y*bw+x)*4+2] = uint8(color.B * 255)
pixels[(y*bw+x)*4+3] = uint8(color.A * 255)
}
}
} }
var tex *glhf.Texture var tex *glhf.Texture
mainthread.Call(func() { mainthread.Call(func() {
tex = glhf.NewTexture(pd.Stride, len(pd.Pix)/pd.Stride, c.smooth, pixels) tex = glhf.NewTexture(bw, bh, c.smooth, pixels)
}) })
return &canvasPicture{ return &canvasPicture{
tex: tex, tex: tex,
bounds: pd.Rect, bounds: bounds,
c: c, c: c,
} }
} }