mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
mobile/ebitenmobileview: Remove mutex usages
This commit is contained in:
parent
0f21829867
commit
91aa6a0f3a
@ -68,6 +68,8 @@ func Get() *UserInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update is called from mobile/ebitenmobileview.
|
// Update is called from mobile/ebitenmobileview.
|
||||||
|
//
|
||||||
|
// Update must be called on the rendering thread.
|
||||||
func (u *UserInterface) Update() error {
|
func (u *UserInterface) Update() error {
|
||||||
select {
|
select {
|
||||||
case err := <-u.errCh:
|
case err := <-u.errCh:
|
||||||
@ -358,6 +360,8 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetOutsideSize is called from mobile/ebitenmobileview.
|
// SetOutsideSize is called from mobile/ebitenmobileview.
|
||||||
|
//
|
||||||
|
// SetOutsideSize is concurrent safe.
|
||||||
func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) {
|
func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) {
|
||||||
// Called from ebitenmobileview.
|
// Called from ebitenmobileview.
|
||||||
u.m.Lock()
|
u.m.Lock()
|
||||||
|
@ -27,7 +27,7 @@ import "C"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/internal/restorable"
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
||||||
@ -37,33 +37,26 @@ import (
|
|||||||
var theState state
|
var theState state
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
started bool
|
running int32
|
||||||
|
|
||||||
// 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
|
|
||||||
m sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *state) isRunning() bool {
|
func (s *state) isRunning() bool {
|
||||||
return s.started
|
return atomic.LoadInt32(&s.running) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *state) run() {
|
||||||
|
atomic.StoreInt32(&s.running, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetGame(game ebiten.Game) {
|
func SetGame(game ebiten.Game) {
|
||||||
theState.m.Lock()
|
if theState.isRunning() {
|
||||||
defer theState.m.Unlock()
|
|
||||||
|
|
||||||
if theState.started {
|
|
||||||
panic("ebitenmobileview: SetGame cannot be called twice or more")
|
panic("ebitenmobileview: SetGame cannot be called twice or more")
|
||||||
}
|
}
|
||||||
ebiten.RunGameWithoutMainLoop(game)
|
ebiten.RunGameWithoutMainLoop(game)
|
||||||
theState.started = true
|
theState.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Layout(viewWidth, viewHeight float64) {
|
func Layout(viewWidth, viewHeight float64) {
|
||||||
theState.m.Lock()
|
|
||||||
defer theState.m.Unlock()
|
|
||||||
|
|
||||||
mobile.Get().SetOutsideSize(viewWidth, viewHeight)
|
mobile.Get().SetOutsideSize(viewWidth, viewHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,9 +65,7 @@ func Update() error {
|
|||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
theState.m.Lock()
|
if !theState.isRunning() {
|
||||||
defer theState.m.Unlock()
|
|
||||||
if !theState.started {
|
|
||||||
// start is not called yet, but as update can be called from another thread, it is OK. Just ignore
|
// start is not called yet, but as update can be called from another thread, it is OK. Just ignore
|
||||||
// this.
|
// this.
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user