internal/buffered: refactoring: replace At with ReadPixels

Updates #1995
This commit is contained in:
Hajime Hoshi 2022-08-05 23:57:56 +09:00
parent bf5f7ee34d
commit 6c22f3f1a8
2 changed files with 32 additions and 11 deletions

View File

@ -16,6 +16,7 @@ package buffered
import (
"fmt"
"image"
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
@ -80,20 +81,36 @@ func (i *Image) MarkDisposed() {
i.img.MarkDisposed()
}
func (i *Image) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
checkDelayedCommandsFlushed("At")
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error {
checkDelayedCommandsFlushed("ReadPixels")
idx := (y*i.width + x)
if i.pixels != nil {
return i.pixels[4*idx], i.pixels[4*idx+1], i.pixels[4*idx+2], i.pixels[4*idx+3], nil
r := image.Rect(x, y, x+width, y+height).Intersect(image.Rect(0, 0, i.width, i.height))
if r.Empty() {
for i := range pixels {
pixels[i] = 0
}
return nil
}
if i.pixels == nil {
pix := make([]byte, 4*i.width*i.height)
if err := i.img.ReadPixels(graphicsDriver, pix); err != nil {
return 0, 0, 0, 0, err
return err
}
i.pixels = pix
return i.pixels[4*idx], i.pixels[4*idx+1], i.pixels[4*idx+2], i.pixels[4*idx+3], nil
}
dstBaseX := r.Min.X - x
dstBaseY := r.Min.Y - y
srcBaseX := r.Min.X
srcBaseY := r.Min.Y
lineWidth := 4 * r.Dx()
for j := 0; j < r.Dy(); j++ {
dstX := 4 * ((dstBaseY+j)*width + dstBaseX)
srcX := 4 * ((srcBaseY+j)*i.width + srcBaseX)
copy(pixels[dstX:dstX+lineWidth], i.pixels[srcX:srcX+lineWidth])
}
return nil
}
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {

View File

@ -55,7 +55,11 @@ func (m *Mipmap) ReplacePixels(pix []byte, x, y, width, height int) {
}
func (m *Mipmap) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
return m.orig.At(graphicsDriver, x, y)
var pix [4]byte
if err := m.orig.ReadPixels(graphicsDriver, pix[:], x, y, 1, 1); err != nil {
return 0, 0, 0, 0, err
}
return pix[0], pix[1], pix[2], pix[3], nil
}
func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {