From ff8689cfcd97a7d81eb9d24785fcbef13271c9b1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 16 Feb 2020 19:01:16 +0900 Subject: [PATCH] buffered: Bug fix: Set and some functions before the main loop caused wrong results invalidatePendingPixels was not called properly. Fixes #1081 --- internal/buffered/image.go | 5 ++- internal/buffered/image_test.go | 65 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 21d06444b..880ae454c 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -111,13 +111,14 @@ func (i *Image) MarkDisposed() { if needsToDelayCommands { delayedCommands = append(delayedCommands, func() error { + i.invalidatePendingPixels() i.img.MarkDisposed() return nil }) return } - i.invalidatePendingPixels() + i.img.MarkDisposed() } func (i *Image) At(x, y int) (r, g, b, a byte, err error) { @@ -187,6 +188,7 @@ func (i *Image) Fill(clr color.RGBA) { if needsToDelayCommands { delayedCommands = append(delayedCommands, func() error { + i.invalidatePendingPixels() i.img.Fill(clr) return nil }) @@ -203,6 +205,7 @@ func (i *Image) ReplacePixels(pix []byte) { if needsToDelayCommands { delayedCommands = append(delayedCommands, func() error { + i.invalidatePendingPixels() copied := make([]byte, len(pix)) copy(copied, pix) i.img.ReplacePixels(copied) diff --git a/internal/buffered/image_test.go b/internal/buffered/image_test.go index e56c62d7d..0443e618f 100644 --- a/internal/buffered/image_test.go +++ b/internal/buffered/image_test.go @@ -181,3 +181,68 @@ func TestDrawTrianglesBeforeMain(t *testing.T) { t.Errorf("got: %v, want: %v", got, want) } } + +var testSetAndFillBeforeMainResult = func() testResult { + clr := color.RGBA{1, 2, 3, 4} + img, _ := ebiten.NewImage(16, 16, ebiten.FilterDefault) + img.Set(0, 0, clr) + img.Fill(color.RGBA{5, 6, 7, 8}) + img.Set(1, 0, clr) + + ch := make(chan color.RGBA, 1) + go func() { + runOnMainThread(func() { + ch <- img.At(0, 0).(color.RGBA) + }) + }() + + return testResult{ + want: color.RGBA{5, 6, 7, 8}, + got: ch, + } +}() + +func TestSetAndFillBeforeMain(t *testing.T) { + got := <-testSetAndFillBeforeMainResult.got + want := testSetAndFillBeforeMainResult.want + + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +var testSetAndReplacePixelsBeforeMainResult = func() testResult { + clr := color.RGBA{1, 2, 3, 4} + img, _ := ebiten.NewImage(16, 16, ebiten.FilterDefault) + img.Set(0, 0, clr) + pix := make([]byte, 4*16*16) + for i := 0; i < len(pix)/4; i++ { + pix[4*i] = 5 + pix[4*i+1] = 6 + pix[4*i+2] = 7 + pix[4*i+3] = 8 + } + img.ReplacePixels(pix) + img.Set(1, 0, clr) + + ch := make(chan color.RGBA, 1) + go func() { + runOnMainThread(func() { + ch <- img.At(0, 0).(color.RGBA) + }) + }() + + return testResult{ + want: color.RGBA{5, 6, 7, 8}, + got: ch, + } +}() + +func TestSetAndReplacePixelsBeforeMain(t *testing.T) { + got := <-testSetAndReplacePixelsBeforeMainResult.got + want := testSetAndReplacePixelsBeforeMainResult.want + + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } +}