diff --git a/graphicscontext.go b/graphicscontext.go index df3e68cf5..a55b0e05d 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -140,7 +140,7 @@ func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int if err := c.initializeIfNeeded(context); err != nil { return err } - if err := theImagesForRestoring.resetPixels(context); err != nil { + if err := theImagesForRestoring.ensurePixels(context); err != nil { return err } for i := 0; i < updateCount; i++ { diff --git a/image.go b/image.go index aeeda340d..c8e9c30c7 100644 --- a/image.go +++ b/image.go @@ -63,11 +63,11 @@ func (i *images) remove(img *Image) { runtime.SetFinalizer(img, nil) } -func (i *images) resetPixels(context *opengl.Context) error { +func (i *images) ensurePixels(context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() for img := range i.images { - if err := img.resetPixels(context); err != nil { + if err := img.ensurePixels(context); err != nil { return err } } diff --git a/imageimpl.go b/imageimpl.go index d5e821ad4..cafa06545 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -53,7 +53,7 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, volatile: volatile, pixels: pixels.NewPixels(img), } - i.pixels.ResetWithPixels(make([]uint8, width*height*4)) + i.pixels.ReplacePixels(make([]uint8, width*height*4)) runtime.SetFinalizer(i, (*imageImpl).Dispose) return i, nil } @@ -86,7 +86,7 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error filter: filter, pixels: pixels.NewPixels(img), } - i.pixels.ResetWithPixels(p) + i.pixels.ReplacePixels(p) runtime.SetFinalizer(i, (*imageImpl).Dispose) return i, nil } @@ -104,7 +104,7 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) { screen: true, pixels: pixels.NewPixels(img), } - i.pixels.ResetWithPixels(make([]uint8, width*height*4)) + i.pixels.ReplacePixels(make([]uint8, width*height*4)) runtime.SetFinalizer(i, (*imageImpl).Dispose) return i, nil } @@ -198,7 +198,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color { return clr } -func (i *imageImpl) resetPixels(context *opengl.Context) error { +func (i *imageImpl) ensurePixels(context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() if i.disposed { @@ -305,7 +305,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error { if i.pixels == nil { i.pixels = pixels.NewPixels(i.image) } - i.pixels.ResetWithPixels(p) + i.pixels.ReplacePixels(p) if i.disposed { return errors.New("ebiten: image is already disposed") } diff --git a/internal/pixels/pixels.go b/internal/pixels/pixels.go index a9fb0bd53..a14f014ca 100644 --- a/internal/pixels/pixels.go +++ b/internal/pixels/pixels.go @@ -46,15 +46,6 @@ func NewPixels(image *graphics.Image) *Pixels { } } -func (p *Pixels) ResetWithPixels(pixels []uint8) { - if p.basePixels == nil { - p.basePixels = make([]uint8, len(pixels)) - } - copy(p.basePixels, pixels) - p.baseColor = nil - p.drawImageHistory = nil -} - func (p *Pixels) Clear() { p.basePixels = nil p.baseColor = nil @@ -67,6 +58,15 @@ func (p *Pixels) Fill(clr color.Color) { p.drawImageHistory = nil } +func (p *Pixels) ReplacePixels(pixels []uint8) { + if p.basePixels == nil { + p.basePixels = make([]uint8, len(pixels)) + } + copy(p.basePixels, pixels) + p.baseColor = nil + p.drawImageHistory = nil +} + func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) { item := &drawImageHistoryItem{ image: image,