internal/restorable: integrate Image.WritePixels into internal/atlas

Updates #805
This commit is contained in:
Hajime Hoshi 2024-01-13 18:56:59 +09:00
parent 3ee905bc4d
commit 6cc8150185
2 changed files with 23 additions and 18 deletions

View File

@ -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,
}

View File

@ -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)
}