mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
mobile/ebitenmobileview: Refactoring
This commit is contained in:
parent
9298b044e3
commit
d0fce2a2db
@ -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
|
|
||||||
}
|
|
@ -27,25 +27,42 @@ import "C"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
|
"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) {
|
func Layout(viewWidth, viewHeight float64) {
|
||||||
theState.m.Lock()
|
theState.m.Lock()
|
||||||
defer theState.m.Unlock()
|
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)
|
mobile.Get().SetOutsideSize(viewWidth, viewHeight)
|
||||||
if !theState.isRunning() {
|
if !theState.isRunning() {
|
||||||
@ -54,16 +71,13 @@ func layout(viewWidth, viewHeight float64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Update() error {
|
func Update() error {
|
||||||
|
// Lock the OS thread since graphics functions (GL) must be called on this thread.
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
theState.m.Lock()
|
theState.m.Lock()
|
||||||
defer theState.m.Unlock()
|
defer theState.m.Unlock()
|
||||||
|
|
||||||
return update()
|
|
||||||
}
|
|
||||||
|
|
||||||
func update() error {
|
|
||||||
if !theState.isRunning() {
|
if !theState.isRunning() {
|
||||||
// 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.
|
Loading…
Reference in New Issue
Block a user