From 9d0ea5c241f5700ef153f07b57327dc155d21563 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 10 Mar 2018 23:36:07 +0900 Subject: [PATCH] graphics: Refactoring: Remove (*sharedImagePart).region() usages from *Image --- image.go | 22 +++------------------- shared.go | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/image.go b/image.go index 75c2ce55d..018c03d0f 100644 --- a/image.go +++ b/image.go @@ -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 } diff --git a/shared.go b/shared.go index c0bdbf977..960c1b1b9 100644 --- a/shared.go +++ b/shared.go @@ -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 {