mobile/ebitenmobileview: Allow Layout without SetGame

This commit is contained in:
Hajime Hoshi 2019-09-23 16:10:52 +09:00
parent dfb89e13e3
commit c24b43099b
2 changed files with 15 additions and 1 deletions

View File

@ -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))

View File

@ -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
}
}