From 5c8d8ab2eba681115c1424a4c5b4c5b9a82a7ebe Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 3 Apr 2021 18:44:41 +0900 Subject: [PATCH] ebiten: Make NewImage/NewImageFromImage panic when RunGame finishes Closes #1149 --- image.go | 12 +++++++++++- run.go | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/image.go b/image.go index e98c1a22c..049624c84 100644 --- a/image.go +++ b/image.go @@ -748,7 +748,12 @@ func (i *Image) ReplacePixels(pixels []byte) { // NewImage returns an empty image. // // If width or height is less than 1 or more than device-dependent maximum size, NewImage panics. +// +// NewImage panics if RunGame already finishes. func NewImage(width, height int) *Image { + if isRunGameEnded() { + panic(fmt.Sprintf("ebiten: NewImage cannot be called after RunGame finishes")) + } if width <= 0 { panic(fmt.Sprintf("ebiten: width at NewImage must be positive but %d", width)) } @@ -766,9 +771,14 @@ func NewImage(width, height int) *Image { // NewImageFromImage creates a new image with the given image (source). // // If source's width or height is less than 1 or more than device-dependent maximum size, NewImageFromImage panics. +// +// NewImageFromImage panics if RunGame already finishes. func NewImageFromImage(source image.Image) *Image { - size := source.Bounds().Size() + if isRunGameEnded() { + panic(fmt.Sprintf("ebiten: NewImage cannot be called after RunGame finishes")) + } + size := source.Bounds().Size() width, height := size.X, size.Y if width <= 0 { panic(fmt.Sprintf("ebiten: source width at NewImageFromImage must be positive but %d", width)) diff --git a/run.go b/run.go index aa112a621..ce0d77dd1 100644 --- a/run.go +++ b/run.go @@ -75,6 +75,7 @@ func CurrentFPS() float64 { var ( isScreenClearedEveryFrame = int32(1) + isRunGameEnded_ = int32(0) currentMaxTPS = int32(DefaultTPS) ) @@ -162,6 +163,8 @@ func (i *imageDumperGame) Layout(outsideWidth, outsideHeight int) (screenWidth, // // Don't call RunGame twice or more in one process. func RunGame(game Game) error { + defer atomic.StoreInt32(&isRunGameEnded_, 1) + fixWindowPosition(WindowSize()) theUIContext.set(&imageDumperGame{ game: game, @@ -175,6 +178,10 @@ func RunGame(game Game) error { return nil } +func isRunGameEnded() bool { + return atomic.LoadInt32(&isRunGameEnded_) != 0 +} + // RunGameWithoutMainLoop runs the game, but doesn't call the loop on the main (UI) thread. // Different from Run, RunGameWithoutMainLoop returns immediately. //