graphics: Refactoring: Remove (*sharedImagePart).region() usages from *Image

This commit is contained in:
Hajime Hoshi 2018-03-10 23:36:07 +09:00
parent 26f4999ddd
commit 9d0ea5c241
2 changed files with 25 additions and 22 deletions

View File

@ -15,7 +15,6 @@
package ebiten
import (
"fmt"
"image"
"image/color"
"runtime"
@ -71,8 +70,7 @@ func (i *Image) copyCheck() {
// Size returns the size of the image.
func (i *Image) Size() (width, height int) {
_, _, w, h := i.sharedImagePart.region()
return w, h
return i.sharedImagePart.Size()
}
// Clear resets the pixels of the image into 0.
@ -231,12 +229,6 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
geom = g
}
dx, dy, _, _ := img.sharedImagePart.region()
sx0 += dx
sy0 += dy
sx1 += dx
sy1 += dy
mode := opengl.CompositeMode(options.CompositeMode)
filter := graphics.FilterNearest
@ -272,11 +264,7 @@ func (i *Image) At(x, y int) color.Color {
if i.isDisposed() {
return color.RGBA{}
}
ox, oy, w, h := i.sharedImagePart.region()
if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}
}
clr, err := i.sharedImagePart.At(x+ox, y+oy)
clr, err := i.sharedImagePart.At(x, y)
if err != nil {
panic(err)
}
@ -317,11 +305,7 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isDisposed() {
return nil
}
x, y, w, h := i.sharedImagePart.region()
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
i.sharedImagePart.ReplacePixels(p, x, y, w, h)
i.sharedImagePart.ReplacePixels(p)
return nil
}

View File

@ -15,6 +15,7 @@
package ebiten
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/internal/affine"
@ -64,16 +65,34 @@ func (s *sharedImagePart) region() (x, y, width, height int) {
return s.node.Region()
}
func (s *sharedImagePart) Size() (width, height int) {
_, _, w, h := s.region()
return w, h
}
func (s *sharedImagePart) DrawImage(img *sharedImagePart, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
dx, dy, _, _ := img.region()
sx0 += dx
sy0 += dy
sx1 += dx
sy1 += dy
s.sharedImage.restorable.DrawImage(img.sharedImage.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
}
func (s *sharedImagePart) ReplacePixels(pixels []byte, x, y, width, height int) {
s.sharedImage.restorable.ReplacePixels(pixels, x, y, width, height)
func (s *sharedImagePart) ReplacePixels(p []byte) {
x, y, w, h := s.region()
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
s.sharedImage.restorable.ReplacePixels(p, x, y, w, h)
}
func (s *sharedImagePart) At(x, y int) (color.Color, error) {
return s.sharedImage.restorable.At(x, y)
ox, oy, w, h := s.region()
if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}, nil
}
return s.sharedImage.restorable.At(x+ox, y+oy)
}
func (s *sharedImagePart) isDisposed() bool {