From 6cc815018556913bad690bd76acb5a9fdb940475 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 13 Jan 2024 18:56:59 +0900 Subject: [PATCH] internal/restorable: integrate Image.WritePixels into internal/atlas Updates #805 --- internal/atlas/image.go | 25 +++++++++++++++++++++++-- internal/restorable/image.go | 16 ---------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 3f224a9a5..fddc1662b 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -81,6 +81,9 @@ type backend struct { // restorable is an atlas on which there might be multiple images. restorable *restorable.Image + width int + height int + // page is an atlas map. Each part is called a node. // If page is nil, the backend's image is isolated and not on an atlas. page *packing.Page @@ -107,6 +110,8 @@ func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) { w, h := b.page.Size() b.restorable = b.restorable.Extend(w, h) + b.width = w + b.height = h return n, true } @@ -487,7 +492,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) { pix2 := graphics.NewManagedBytes(len(pix), func(bs []byte) { copy(bs, pix) }) - i.backend.restorable.WritePixels(pix2, region) + i.backend.writePixels(pix2, region) return } @@ -516,7 +521,17 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) { copy(bs[4*j*r.Dx():], pix[4*j*region.Dx():4*(j+1)*region.Dx()]) } }) - i.backend.restorable.WritePixels(pixb, r) + i.backend.writePixels(pixb, r) +} + +func (b *backend) writePixels(pixels *graphics.ManagedBytes, region image.Rectangle) { + if region.Dx() <= 0 || region.Dy() <= 0 { + panic("atlas: width/height must be positive") + } + if !region.In(image.Rect(0, 0, b.width, b.height)) { + panic(fmt.Sprintf("atlas: out of range %v", region)) + } + b.restorable.Image.WritePixels(pixels, region) } // ReadPixels reads pixels on the given region to the given slice pixels. @@ -667,6 +682,8 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) { // A screen image doesn't have a padding. i.backend = &backend{ restorable: restorable.NewImage(i.width, i.height, true), + width: i.width, + height: i.height, } theBackends = append(theBackends, i.backend) return @@ -682,6 +699,8 @@ func (i *Image) allocate(forbiddenBackends []*backend, asSource bool) { i.backend = &backend{ restorable: restorable.NewImage(wp, hp, false), + width: wp, + height: hp, source: asSource && i.imageType == ImageTypeRegular, } theBackends = append(theBackends, i.backend) @@ -728,6 +747,8 @@ loop: b := &backend{ restorable: restorable.NewImage(width, height, false), + width: width, + height: height, page: packing.NewPage(width, height, maxSize), source: asSource, } diff --git a/internal/restorable/image.go b/internal/restorable/image.go index c61781c47..48f06bda5 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -15,7 +15,6 @@ package restorable import ( - "fmt" "image" "github.com/hajimehoshi/ebiten/v2/internal/graphics" @@ -101,18 +100,3 @@ func (i *Image) ClearPixels(region image.Rectangle) { } clearImage(i.Image, region.Intersect(image.Rect(0, 0, i.width, i.height))) } - -// WritePixels replaces the image pixels with the given pixels slice. -// -// The specified region must not be overlapped with other regions by WritePixels. -func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangle) { - if region.Dx() <= 0 || region.Dy() <= 0 { - panic("restorable: width/height must be positive") - } - w, h := i.width, i.height - if !region.In(image.Rect(0, 0, w, h)) { - panic(fmt.Sprintf("restorable: out of range %v", region)) - } - - i.Image.WritePixels(pixels, region) -}