mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
mobile/ebitenmobileview: Refactoring
This commit is contained in:
parent
12d0f273ec
commit
5973833274
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user