mobile: Bug fix: mutex is necessary for start and update

mobile's Start and Update can be called from different threads.
Especially, on Android, this behavior is noted explicitly at
https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer
This commit is contained in:
Hajime Hoshi 2018-05-03 22:02:43 +09:00
parent f1927d8aca
commit 7a00f0f599

View File

@ -18,17 +18,22 @@ package mobile
import ( import (
"errors" "errors"
"sync"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
) )
var ( var (
chError <-chan error chError <-chan error
running bool running bool
mobileMutex sync.Mutex
) )
func update() error { func update() error {
mobileMutex.Lock()
defer mobileMutex.Unlock()
if chError == nil { if chError == nil {
return errors.New("mobile: chError must not be nil: Start is not called yet?") return errors.New("mobile: chError must not be nil: Start is not called yet?")
} }
@ -39,6 +44,9 @@ func update() error {
} }
func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) { func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) {
mobileMutex.Lock()
defer mobileMutex.Unlock()
running = true running = true
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title) chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
} }