From a3e14c9ddb42da330f3f01e949aefd8f6ea2579c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 4 Mar 2017 02:23:39 +0900 Subject: [PATCH] graphics: Image.ReplacePixels/Dispose always returns nil (#331) --- image.go | 16 ++++++++++++++-- imageimpl.go | 17 ++++++----------- internal/restorable/image.go | 3 +-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/image.go b/image.go index 9f0a0ed9c..46de3f8aa 100644 --- a/image.go +++ b/image.go @@ -230,13 +230,18 @@ func (i *Image) At(x, y int) color.Color { // // The behavior of any functions for a disposed image is undefined. // +// When the image is disposed, Dipose does nothing. +// +// Dipose always return nil as of 1.5.0-alpha. +// // This function is concurrent-safe. func (i *Image) Dispose() error { if i.impl.isDisposed() { return nil } theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()) - return i.impl.Dispose() + i.impl.Dispose() + return nil } // ReplacePixels replaces the pixels of the image with p. @@ -245,10 +250,17 @@ func (i *Image) Dispose() error { // // ReplacePixels may be slow (as for implementation, this calls glTexSubImage2D). // +// When len(p) is not 4 * (width) * (height), ReplacePixels panics. +// +// When the image is disposed, ReplacePixels does nothing. +// +// ReplacePixels always returns nil as of 1.5.0-alpha. +// // This function is concurrent-safe. func (i *Image) ReplacePixels(p []uint8) error { theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()) - return i.impl.ReplacePixels(p) + i.impl.ReplacePixels(p) + return nil } // A DrawImageOptions represents options to render an image on an image. diff --git a/imageimpl.go b/imageimpl.go index 517526001..c22a75bcb 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -15,7 +15,6 @@ package ebiten import ( - "errors" "fmt" "image" "image/color" @@ -184,29 +183,26 @@ func (i *imageImpl) restore(context *opengl.Context) error { return nil } -func (i *imageImpl) Dispose() error { +func (i *imageImpl) Dispose() { i.m.Lock() defer i.m.Unlock() if i.restorable == nil { - return errors.New("ebiten: image is already disposed") - } - if err := i.restorable.Dispose(); err != nil { - return err + return } + i.restorable.Dispose() i.restorable = nil runtime.SetFinalizer(i, nil) - return nil } -func (i *imageImpl) ReplacePixels(p []uint8) error { +func (i *imageImpl) ReplacePixels(p []uint8) { w, h := i.restorable.Size() if l := 4 * w * h; len(p) != l { - return fmt.Errorf("ebiten: p's length must be %d", l) + panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l)) } i.m.Lock() defer i.m.Unlock() if i.restorable == nil { - return errors.New("ebiten: image is already disposed") + return } w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h) pix := make([]uint8, 4*w2*h2) @@ -214,7 +210,6 @@ func (i *imageImpl) ReplacePixels(p []uint8) error { copy(pix[j*w2*4:], p[j*w*4:(j+1)*w*4]) } i.restorable.ReplacePixels(pix) - return nil } func (i *imageImpl) isDisposed() bool { diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 954e634ea..4eafa1df0 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -261,14 +261,13 @@ func (p *Image) Restore(context *opengl.Context) error { return nil } -func (p *Image) Dispose() error { +func (p *Image) Dispose() { p.image.Dispose() p.image = nil p.basePixels = nil p.baseColor = color.RGBA{} p.drawImageHistory = nil p.stale = false - return nil } func (p *Image) IsInvalidated(context *opengl.Context) bool {