From de49bbab12e992aba8f438023137acb45d644087 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 11 Aug 2019 21:27:10 +0900 Subject: [PATCH] mobile: Replace SetUpdateFunc with Set --- mobile/ebitenmobileview/ebitenmobileview.go | 62 +++++++++++++-------- mobile/mobile.go | 8 +-- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/mobile/ebitenmobileview/ebitenmobileview.go b/mobile/ebitenmobileview/ebitenmobileview.go index 368ad7694..814c454e5 100644 --- a/mobile/ebitenmobileview/ebitenmobileview.go +++ b/mobile/ebitenmobileview/ebitenmobileview.go @@ -25,52 +25,70 @@ import ( "github.com/hajimehoshi/ebiten" ) -var ( - // mobileMutex is a mutex required for each function. +var theState state + +type state struct { + updateFunc func(*ebiten.Image) error + screenWidth int + screenHeight int + + // 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 - mobileMutex sync.Mutex -) + m sync.Mutex +} -func Run(width, height int, scale float64) { - mobileMutex.Lock() - defer mobileMutex.Unlock() +func Run(scale float64) { + theState.m.Lock() + defer theState.m.Unlock() - if updateFunc == nil { + if theState.updateFunc == nil { panic("ebitenmobileview: SetUpdateFunc must be called before Run") } - start(updateFunc, width, height, scale) + start(theState.updateFunc, theState.screenWidth, theState.screenHeight, scale) } func Update() error { runtime.LockOSThread() defer runtime.UnlockOSThread() - mobileMutex.Lock() - defer mobileMutex.Unlock() + theState.m.Lock() + defer theState.m.Unlock() return update() } func UpdateTouchesOnAndroid(action int, id int, x, y int) { - mobileMutex.Lock() - defer mobileMutex.Unlock() + theState.m.Lock() + defer theState.m.Unlock() updateTouchesOnAndroid(action, id, x, y) } func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { - mobileMutex.Lock() - defer mobileMutex.Unlock() + theState.m.Lock() + defer theState.m.Unlock() updateTouchesOnIOSImpl(phase, ptr, x, y) } -var updateFunc func(*ebiten.Image) error - -func SetUpdateFunc(f func(*ebiten.Image) error) { - mobileMutex.Lock() - defer mobileMutex.Unlock() - - updateFunc = f +func ScreenWidth() int { + theState.m.Lock() + defer theState.m.Unlock() + return theState.screenWidth +} + +func ScreenHeight() int { + theState.m.Lock() + defer theState.m.Unlock() + return theState.screenHeight +} + +func Set(updateFunc func(*ebiten.Image) error, screenWidth, screenHeight int) { + theState.m.Lock() + defer theState.m.Unlock() + + theState.updateFunc = updateFunc + theState.screenWidth = screenWidth + theState.screenHeight = screenHeight } diff --git a/mobile/mobile.go b/mobile/mobile.go index 6d86210cc..e7eaec583 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -37,8 +37,8 @@ import ( // // Start always returns nil as of 1.5.0-alpha. func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error { - ebitenmobileview.SetUpdateFunc(f) - ebitenmobileview.Run(width, height, scale) + ebitenmobileview.Set(f, width, height) + ebitenmobileview.Run(scale) return nil } @@ -136,6 +136,6 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y) } -func SetUpdateFunc(f func(*ebiten.Image) error) { - ebitenmobileview.SetUpdateFunc(f) +func Set(updateFunc func(*ebiten.Image) error, screenWidth, screenHeight int) { + ebitenmobileview.Set(updateFunc, screenWidth, screenHeight) }