ebiten: Make NewImage/NewImageFromImage panic when RunGame finishes

Closes #1149
This commit is contained in:
Hajime Hoshi 2021-04-03 18:44:41 +09:00
parent d6ee55cb22
commit 5c8d8ab2eb
2 changed files with 18 additions and 1 deletions

View File

@ -748,7 +748,12 @@ func (i *Image) ReplacePixels(pixels []byte) {
// NewImage returns an empty image. // NewImage returns an empty image.
// //
// If width or height is less than 1 or more than device-dependent maximum size, NewImage panics. // 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 { func NewImage(width, height int) *Image {
if isRunGameEnded() {
panic(fmt.Sprintf("ebiten: NewImage cannot be called after RunGame finishes"))
}
if width <= 0 { if width <= 0 {
panic(fmt.Sprintf("ebiten: width at NewImage must be positive but %d", width)) 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). // 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. // 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 { 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 width, height := size.X, size.Y
if width <= 0 { if width <= 0 {
panic(fmt.Sprintf("ebiten: source width at NewImageFromImage must be positive but %d", width)) panic(fmt.Sprintf("ebiten: source width at NewImageFromImage must be positive but %d", width))

7
run.go
View File

@ -75,6 +75,7 @@ func CurrentFPS() float64 {
var ( var (
isScreenClearedEveryFrame = int32(1) isScreenClearedEveryFrame = int32(1)
isRunGameEnded_ = int32(0)
currentMaxTPS = int32(DefaultTPS) 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. // Don't call RunGame twice or more in one process.
func RunGame(game Game) error { func RunGame(game Game) error {
defer atomic.StoreInt32(&isRunGameEnded_, 1)
fixWindowPosition(WindowSize()) fixWindowPosition(WindowSize())
theUIContext.set(&imageDumperGame{ theUIContext.set(&imageDumperGame{
game: game, game: game,
@ -175,6 +178,10 @@ func RunGame(game Game) error {
return nil 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. // RunGameWithoutMainLoop runs the game, but doesn't call the loop on the main (UI) thread.
// Different from Run, RunGameWithoutMainLoop returns immediately. // Different from Run, RunGameWithoutMainLoop returns immediately.
// //