From 8f857daf3e4f2af460352a06b71ab412911d079d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 9 Sep 2021 03:58:58 +0900 Subject: [PATCH] internal/restorable: Bug fix: Forbid NewImage before the graphics driver is ready --- internal/restorable/image.go | 4 ++++ internal/restorable/images.go | 3 +++ internal/restorable/shader_test.go | 5 +++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 8570ef8c5..505a54c05 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -141,6 +141,10 @@ func ensureEmptyImage() *Image { // // Note that Dispose is not called automatically. func NewImage(width, height int) *Image { + if !graphicsDriverInitialized { + panic("restorable: graphics driver must be ready at NewImage but not") + } + i := &Image{ image: graphicscommand.NewImage(width, height), width: width, diff --git a/internal/restorable/images.go b/internal/restorable/images.go index e1f6db211..b877a4830 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -281,8 +281,11 @@ func (i *images) restore() error { return nil } +var graphicsDriverInitialized bool + // InitializeGraphicsDriverState initializes the graphics driver state. func InitializeGraphicsDriverState() error { + graphicsDriverInitialized = true return graphicscommand.InitializeGraphicsDriverState() } diff --git a/internal/restorable/shader_test.go b/internal/restorable/shader_test.go index 7167347be..6fca3bccb 100644 --- a/internal/restorable/shader_test.go +++ b/internal/restorable/shader_test.go @@ -25,9 +25,10 @@ import ( etesting "github.com/hajimehoshi/ebiten/v2/internal/testing" ) -var emptyImage = NewImage(3, 3) - func clearImage(img *Image, w, h int) { + emptyImage := NewImage(3, 3) + defer emptyImage.Dispose() + dx0 := float32(0) dy0 := float32(0) dx1 := float32(w)