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 (
"math"
"runtime"
"github.com/hajimehoshi/ebiten"
)
type ViewRectSetter interface {
@ -52,11 +54,12 @@ func layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) {
x := (viewWidth - width) / 2
y := (viewHeight - height) / 2
if !theState.running {
start(theState.game.Update, w, h, scale)
theState.running = true
if theState.isRunning() {
ebiten.SetScreenSize(w, h)
ebiten.SetScreenScale(scale)
} 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 {

View File

@ -32,10 +32,10 @@ type game interface {
type state struct {
game game
running bool
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
@ -53,3 +53,7 @@ func SetGame(game game) {
theState.delayedLayout = nil
}
}
func (s *state) isRunning() bool {
return s.errorCh != nil
}

View File

@ -22,23 +22,18 @@ package ebitenmobileview
import "C"
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
)
var (
chError <-chan 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
// this.
return nil
}
select {
case err := <-chError:
case err := <-theState.errorCh:
return err
default:
}
@ -46,13 +41,3 @@ func update() error {
mobile.Get().Render()
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
import (
"github.com/hajimehoshi/ebiten"
)
func update() error {
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) {
}