mobile/ebitenmobileview: Refactoring

This commit is contained in:
Hajime Hoshi 2019-10-11 22:38:29 +09:00
parent 12d0f273ec
commit 5973833274
4 changed files with 15 additions and 33 deletions

View File

@ -21,6 +21,8 @@ package ebitenmobileview
import ( import (
"math" "math"
"runtime" "runtime"
"github.com/hajimehoshi/ebiten"
) )
type ViewRectSetter interface { type ViewRectSetter interface {
@ -52,11 +54,12 @@ func layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) {
x := (viewWidth - width) / 2 x := (viewWidth - width) / 2
y := (viewHeight - height) / 2 y := (viewHeight - height) / 2
if !theState.running { if theState.isRunning() {
start(theState.game.Update, w, h, scale) ebiten.SetScreenSize(w, h)
theState.running = true ebiten.SetScreenScale(scale)
} else { } else {
setScreenSize(w, h, scale) // The last argument 'title' is not used on mobile platforms, so just pass an empty string.
theState.errorCh = ebiten.RunWithoutMainLoop(theState.game.Update, w, h, scale, "")
} }
if viewRectSetter != nil { if viewRectSetter != nil {

View File

@ -32,10 +32,10 @@ type game interface {
type state struct { type state struct {
game game game game
running bool
delayedLayout func() delayedLayout func()
errorCh <-chan error
// m is a mutex required for each function. // 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
@ -53,3 +53,7 @@ func SetGame(game game) {
theState.delayedLayout = nil theState.delayedLayout = nil
} }
} }
func (s *state) isRunning() bool {
return s.errorCh != nil
}

View File

@ -22,23 +22,18 @@ package ebitenmobileview
import "C" import "C"
import ( import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile" "github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
) )
var (
chError <-chan error
)
func update() error { func update() error {
if chError == nil { 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.
return nil return nil
} }
select { select {
case err := <-chError: case err := <-theState.errorCh:
return err return err
default: default:
} }
@ -46,13 +41,3 @@ func update() error {
mobile.Get().Render() mobile.Get().Render()
return nil return nil
} }
func start(f func(*ebiten.Image) error, width, height int, scale float64) {
// The last argument 'title' is not used on mobile platforms, so just pass an empty string.
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, "")
}
func setScreenSize(width, height int, scale float64) {
ebiten.SetScreenSize(width, height)
ebiten.SetScreenScale(scale)
}

View File

@ -17,20 +17,10 @@
package ebitenmobileview package ebitenmobileview
import (
"github.com/hajimehoshi/ebiten"
)
func update() error { func update() error {
return nil return nil
} }
func start(f func(*ebiten.Image) error, width, height int, scale float64) {
}
func setScreenSize(width, height int, scale float64) {
}
func updateTouchesOnAndroid(action int, id int, x, y int) { func updateTouchesOnAndroid(action int, id int, x, y int) {
} }