From d0fce2a2dbcec0e2e0bfcbae56106c8a9ee61a21 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 11 Feb 2020 22:14:41 +0900 Subject: [PATCH] mobile/ebitenmobileview: Refactoring --- mobile/ebitenmobileview/game.go | 54 ------------------- .../ebitenmobileview/{funcs.go => mobile.go} | 44 +++++++++------ 2 files changed, 29 insertions(+), 69 deletions(-) delete mode 100644 mobile/ebitenmobileview/game.go rename mobile/ebitenmobileview/{funcs.go => mobile.go} (73%) diff --git a/mobile/ebitenmobileview/game.go b/mobile/ebitenmobileview/game.go deleted file mode 100644 index 7bc738e29..000000000 --- a/mobile/ebitenmobileview/game.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2019 The Ebiten Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build android ios - -package ebitenmobileview - -import ( - "sync" - - "github.com/hajimehoshi/ebiten" -) - -var theState state - -type state struct { - game ebiten.Game - - delayedLayout func() - - errorCh <-chan error - - // 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 SetGame(game ebiten.Game) { - theState.m.Lock() - defer theState.m.Unlock() - - theState.game = game - - if theState.delayedLayout != nil { - theState.delayedLayout() - theState.delayedLayout = nil - } -} - -func (s *state) isRunning() bool { - return s.errorCh != nil -} diff --git a/mobile/ebitenmobileview/funcs.go b/mobile/ebitenmobileview/mobile.go similarity index 73% rename from mobile/ebitenmobileview/funcs.go rename to mobile/ebitenmobileview/mobile.go index f5de1d342..67ac324ac 100644 --- a/mobile/ebitenmobileview/funcs.go +++ b/mobile/ebitenmobileview/mobile.go @@ -27,25 +27,42 @@ import "C" import ( "runtime" + "sync" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/internal/uidriver/mobile" ) +var theState state + +type state struct { + game ebiten.Game + + errorCh <-chan error + + // 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 { + return s.game != nil && s.errorCh != nil +} + +func SetGame(game ebiten.Game) { + theState.m.Lock() + defer theState.m.Unlock() + + if theState.game != nil { + panic("ebitenmobileview: SetGame cannot be called twice or more") + } + theState.game = game +} + func Layout(viewWidth, viewHeight float64) { theState.m.Lock() defer theState.m.Unlock() - layout(viewWidth, viewHeight) -} - -func layout(viewWidth, viewHeight float64) { - if theState.game == nil { - // It is fine to override the existing function since only the last layout result matters. - theState.delayedLayout = func() { - layout(viewWidth, viewHeight) - } - return - } mobile.Get().SetOutsideSize(viewWidth, viewHeight) if !theState.isRunning() { @@ -54,16 +71,13 @@ func layout(viewWidth, viewHeight float64) { } func Update() error { + // Lock the OS thread since graphics functions (GL) must be called on this thread. runtime.LockOSThread() defer runtime.UnlockOSThread() theState.m.Lock() defer theState.m.Unlock() - return update() -} - -func update() error { if !theState.isRunning() { // start is not called yet, but as update can be called from another thread, it is OK. Just ignore // this.