diff --git a/mobile/ebitenmobileview/funcs.go b/mobile/ebitenmobileview/funcs.go index 2b598b405..27851f88a 100644 --- a/mobile/ebitenmobileview/funcs.go +++ b/mobile/ebitenmobileview/funcs.go @@ -30,9 +30,16 @@ type ViewRectSetter interface { func Layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) { theState.m.Lock() defer theState.m.Unlock() + layout(viewWidth, viewHeight, viewRectSetter) +} +func layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) { if theState.game == nil { - panic("ebitenmobileview: SetGame must be called before Layout") + // It is fine to override the existing function since only the last layout result matters. + theState.delayedLayout = func() { + layout(viewWidth, viewHeight, viewRectSetter) + } + return } w, h := theState.game.Layout(int(viewWidth), int(viewHeight)) diff --git a/mobile/ebitenmobileview/game.go b/mobile/ebitenmobileview/game.go index 3ea85d10a..d23a0cd2a 100644 --- a/mobile/ebitenmobileview/game.go +++ b/mobile/ebitenmobileview/game.go @@ -34,6 +34,8 @@ type state struct { running bool + delayedLayout func() + // m is a mutex required for each function. // For example, on Android, Update can be called from a different thread: // https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer @@ -45,4 +47,9 @@ func SetGame(game game) { defer theState.m.Unlock() theState.game = game + + if theState.delayedLayout != nil { + theState.delayedLayout() + theState.delayedLayout = nil + } }