mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Reland: graphics: Bug fix: a sub image's Size was wrong
Size() usages are also fixed. Fixes #732
This commit is contained in:
parent
27331024af
commit
f54bdfa159
28
image.go
28
image.go
@ -150,7 +150,8 @@ func (i *Image) copyCheck() {
|
||||
|
||||
// Size returns the size of the image.
|
||||
func (i *Image) Size() (width, height int) {
|
||||
return i.mipmap.original().Size()
|
||||
s := i.Bounds().Size()
|
||||
return s.X, s.Y
|
||||
}
|
||||
|
||||
func (i *Image) isDisposed() bool {
|
||||
@ -329,24 +330,23 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
|
||||
return
|
||||
}
|
||||
|
||||
w, h := img.Size()
|
||||
sx0, sy0, sx1, sy1 := 0, 0, w, h
|
||||
// TODO: Add tests about #732
|
||||
bounds := img.Bounds()
|
||||
sx0, sy0, sx1, sy1 := bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y
|
||||
|
||||
// SourceRect is deprecated. This implementation is for backward compatibility.
|
||||
if img.bounds != nil || options.SourceRect != nil {
|
||||
r := img.bounds
|
||||
if r == nil {
|
||||
r = options.SourceRect
|
||||
} else if options.SourceRect != nil {
|
||||
r2 := r.Intersect(*options.SourceRect)
|
||||
r = &r2
|
||||
}
|
||||
if options.SourceRect != nil {
|
||||
r := bounds.Intersect(*options.SourceRect)
|
||||
if r.Empty() {
|
||||
return
|
||||
}
|
||||
|
||||
sx0 = r.Min.X
|
||||
sy0 = r.Min.Y
|
||||
if sx0 < r.Min.X {
|
||||
sx0 = r.Min.X
|
||||
}
|
||||
if sy0 < r.Min.Y {
|
||||
sy0 = r.Min.Y
|
||||
}
|
||||
if sx1 > r.Max.X {
|
||||
sx1 = r.Max.X
|
||||
}
|
||||
@ -569,7 +569,7 @@ func (i *Image) SubImage(r image.Rectangle) image.Image {
|
||||
// Bounds returns the bounds of the image.
|
||||
func (i *Image) Bounds() image.Rectangle {
|
||||
if i.bounds == nil {
|
||||
w, h := i.Size()
|
||||
w, h := i.mipmap.original().Size()
|
||||
return image.Rect(0, 0, w, h)
|
||||
}
|
||||
return *i.bounds
|
||||
|
@ -1034,3 +1034,14 @@ func TestImageSubImageAt(t *testing.T) {
|
||||
t.Errorf("got: %v, want: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageSubImageSize(t *testing.T) {
|
||||
img, _ := NewImage(16, 16, FilterDefault)
|
||||
img.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
||||
|
||||
got, _ := img.SubImage(image.Rect(1, 1, 16, 16)).(*Image).Size()
|
||||
want := 15
|
||||
if got != want {
|
||||
t.Errorf("got: %v, want: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user