mobile: Replace SetUpdateFunc with Set

This commit is contained in:
Hajime Hoshi 2019-08-11 21:27:10 +09:00
parent 2925fc718b
commit de49bbab12
2 changed files with 44 additions and 26 deletions

View File

@ -25,52 +25,70 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
var ( var theState state
// mobileMutex is a mutex required for each function.
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: // For example, on Android, Update can be called from a different thread:
// https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer // https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer
mobileMutex sync.Mutex m sync.Mutex
) }
func Run(width, height int, scale float64) { func Run(scale float64) {
mobileMutex.Lock() theState.m.Lock()
defer mobileMutex.Unlock() defer theState.m.Unlock()
if updateFunc == nil { if theState.updateFunc == nil {
panic("ebitenmobileview: SetUpdateFunc must be called before Run") panic("ebitenmobileview: SetUpdateFunc must be called before Run")
} }
start(updateFunc, width, height, scale) start(theState.updateFunc, theState.screenWidth, theState.screenHeight, scale)
} }
func Update() error { func Update() error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
mobileMutex.Lock() theState.m.Lock()
defer mobileMutex.Unlock() defer theState.m.Unlock()
return update() return update()
} }
func UpdateTouchesOnAndroid(action int, id int, x, y int) { func UpdateTouchesOnAndroid(action int, id int, x, y int) {
mobileMutex.Lock() theState.m.Lock()
defer mobileMutex.Unlock() defer theState.m.Unlock()
updateTouchesOnAndroid(action, id, x, y) updateTouchesOnAndroid(action, id, x, y)
} }
func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
mobileMutex.Lock() theState.m.Lock()
defer mobileMutex.Unlock() defer theState.m.Unlock()
updateTouchesOnIOSImpl(phase, ptr, x, y) updateTouchesOnIOSImpl(phase, ptr, x, y)
} }
var updateFunc func(*ebiten.Image) error func ScreenWidth() int {
theState.m.Lock()
func SetUpdateFunc(f func(*ebiten.Image) error) { defer theState.m.Unlock()
mobileMutex.Lock() return theState.screenWidth
defer mobileMutex.Unlock() }
updateFunc = f 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
} }

View File

@ -37,8 +37,8 @@ import (
// //
// Start always returns nil as of 1.5.0-alpha. // 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 { func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error {
ebitenmobileview.SetUpdateFunc(f) ebitenmobileview.Set(f, width, height)
ebitenmobileview.Run(width, height, scale) ebitenmobileview.Run(scale)
return nil return nil
} }
@ -136,6 +136,6 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y) ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y)
} }
func SetUpdateFunc(f func(*ebiten.Image) error) { func Set(updateFunc func(*ebiten.Image) error, screenWidth, screenHeight int) {
ebitenmobileview.SetUpdateFunc(f) ebitenmobileview.Set(updateFunc, screenWidth, screenHeight)
} }