mobile: Accept gomobile build (#249)

This commit is contained in:
Hajime Hoshi 2018-03-24 01:07:36 +09:00
parent 1119734ca7
commit 2110191794
7 changed files with 244 additions and 16 deletions

View File

@ -82,7 +82,16 @@ func Init() {
theContext = c
}
func InitWithContext(context mgl.Context) {
c := &Context{}
c.gl = context
theContext = c
}
func (c *Context) DoWork(chError <-chan error, chDone <-chan struct{}) error {
if c.worker == nil {
panic("not reached")
}
// TODO: Check this is called on the rendering thread
loop:
for {

View File

@ -40,14 +40,14 @@ func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
return false
}
func (i *Input) updateTouches(touches []Touch) {
func (i *Input) updateTouches(touches []Touch, dx, dy int) {
i.m.Lock()
defer i.m.Unlock()
ts := make([]touch, len(touches))
for i := 0; i < len(ts); i++ {
ts[i].id = touches[i].ID()
x, y := touches[i].Position()
ts[i].x, ts[i].y = x, y
ts[i].x, ts[i].y = x+dx, y+dy
}
i.touches = ts
i.m.Unlock()
}

View File

@ -0,0 +1,95 @@
// Copyright 2018 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
// +build gomobilebuild
package ui
import (
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
mtouch "golang.org/x/mobile/event/touch"
"golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
var (
glContextCh chan gl.Context
)
func appMain(a app.App) {
var glctx gl.Context
touches := map[mtouch.Sequence]*touch{}
for e := range a.Events() {
switch e := a.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
glctx, _ = e.DrawContext.(gl.Context)
// Assume that glctx is always a same instance.
// Then, only once initializing should be enough.
if glContextCh != nil {
glContextCh <- glctx
glContextCh = nil
}
a.Send(paint.Event{})
case lifecycle.CrossOff:
glctx = nil
}
case size.Event:
setFullscreen(e.WidthPx, e.HeightPx)
case paint.Event:
if glctx == nil || e.External {
continue
}
chRender <- struct{}{}
<-chRenderEnd
a.Publish()
a.Send(paint.Event{})
case mtouch.Event:
switch e.Type {
case mtouch.TypeBegin, mtouch.TypeMove:
s := float32(actualScale())
t := &touch{
id: int(e.Sequence), // TODO: Is it ok to cast from int64 to int here?
x: int(e.X / s),
y: int(e.Y / s),
}
touches[e.Sequence] = t
case mtouch.TypeEnd:
delete(touches, e.Sequence)
}
ts := []Touch{}
for _, t := range touches {
ts = append(ts, t)
}
UpdateTouches(ts)
}
}
}
func RunMainThreadLoop(ch <-chan error) error {
glContextCh = make(chan gl.Context)
app.Main(appMain)
return nil
}
func initOpenGL() {
ctx := <-glContextCh
opengl.InitWithContext(ctx)
}

View File

@ -0,0 +1,32 @@
// Copyright 2018 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
// +build !gomobilebuild
package ui
import (
"errors"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
func RunMainThreadLoop(ch <-chan error) error {
return errors.New("ui: don't call this: use RunWithoutMainLoop instead of Run")
}
func initOpenGL() {
opengl.Init()
}

View File

@ -27,10 +27,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl"
)
func RunMainThreadLoop(ch <-chan error) error {
return errors.New("ui: don't call this: use RunWithoutMainLoop instead of Run")
}
func Render(chError <-chan error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -54,6 +50,11 @@ type userInterface struct {
scale float64
sizeChanged bool
// Used for gomobile-build
fullscreenScale float64
fullscreenWidthPx int
fullscreenHeightPx int
m sync.RWMutex
}
@ -72,9 +73,10 @@ func Run(width, height int, scale float64, title string, g GraphicsContext) erro
u.scale = scale
u.sizeChanged = true
u.m.Unlock()
// title is ignored?
opengl.Init()
initOpenGL()
for {
if err := u.update(g); err != nil {
return err
@ -92,7 +94,7 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
if sizeChanged {
width = u.width
height = u.height
actualScale = u.scale * devicescale.DeviceScale()
actualScale = u.actualScaleImpl()
}
u.sizeChanged = false
u.m.Unlock()
@ -103,6 +105,25 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
}
}
func actualScale() float64 {
return currentUI.actualScale()
}
func (u *userInterface) actualScale() float64 {
u.m.Lock()
s := u.actualScaleImpl()
u.m.Unlock()
return s
}
func (u *userInterface) actualScaleImpl() float64 {
scale := u.scale
if u.fullscreenScale != 0 {
scale = u.fullscreenScale
}
return scale * devicescale.DeviceScale()
}
func (u *userInterface) update(g GraphicsContext) error {
<-chRender
defer func() {
@ -119,6 +140,17 @@ func (u *userInterface) update(g GraphicsContext) error {
return nil
}
func screenSize() (int, int) {
return currentUI.screenSize()
}
func (u *userInterface) screenSize() (int, int) {
u.m.Lock()
w, h := u.width, u.height
u.m.Unlock()
return w, h
}
func SetScreenSize(width, height int) bool {
currentUI.setScreenSize(width, height)
return true
@ -129,6 +161,7 @@ func (u *userInterface) setScreenSize(width, height int) {
if u.width != width || u.height != height {
u.width = width
u.height = height
u.updateFullscreenScaleIfNeeded()
u.sizeChanged = true
}
u.m.Unlock()
@ -156,12 +189,64 @@ func ScreenScale() float64 {
return s
}
func setFullscreen(widthPx, heightPx int) {
currentUI.setFullscreen(widthPx, heightPx)
}
func (u *userInterface) setFullscreen(widthPx, heightPx int) {
u.m.Lock()
u.fullscreenWidthPx = widthPx
u.fullscreenHeightPx = heightPx
u.updateFullscreenScaleIfNeeded()
u.sizeChanged = true
u.m.Unlock()
}
func (u *userInterface) updateFullscreenScaleIfNeeded() {
if u.fullscreenWidthPx == 0 || u.fullscreenHeightPx == 0 {
return
}
w, h := u.width, u.height
scaleX := float64(u.fullscreenWidthPx) / float64(w)
scaleY := float64(u.fullscreenHeightPx) / float64(h)
scale := scaleX
if scale > scaleY {
scale = scaleY
}
u.fullscreenScale = scale / devicescale.DeviceScale()
}
func ScreenPadding() (x0, y0, x1, y1 float64) {
return 0, 0, 0, 0
return currentUI.screenPadding()
}
func (u *userInterface) screenPadding() (x0, y0, x1, y1 float64) {
u.m.Lock()
x0, y0, x1, y1 = u.screenPaddingImpl()
u.m.Unlock()
return
}
func (u *userInterface) screenPaddingImpl() (x0, y0, x1, y1 float64) {
if u.fullscreenScale == 0 {
return 0, 0, 0, 0
}
s := u.fullscreenScale * devicescale.DeviceScale()
ox := (float64(u.fullscreenWidthPx) - float64(u.width)*s) / 2
oy := (float64(u.fullscreenHeightPx) - float64(u.height)*s) / 2
return ox, oy, ox, oy
}
func adjustCursorPosition(x, y int) (int, int) {
return x, y
return currentUI.adjustCursorPosition(x, y)
}
func (u *userInterface) adjustCursorPosition(x, y int) (int, int) {
u.m.Lock()
ox, oy, _, _ := u.screenPaddingImpl()
s := u.actualScaleImpl()
u.m.Unlock()
return x - int(ox/s), y - int(oy/s)
}
func IsCursorVisible() bool {
@ -201,5 +286,9 @@ func SetWindowDecorated(decorated bool) {
}
func UpdateTouches(touches []Touch) {
currentInput.updateTouches(touches)
currentUI.m.Lock()
ox, oy, _, _ := currentUI.screenPaddingImpl()
s := currentUI.actualScaleImpl()
currentUI.m.Unlock()
currentInput.updateTouches(touches, -int(ox/s), -int(oy/s))
}

View File

@ -14,6 +14,9 @@
// Package mobile provides functions for mobile platforms (Android and iOS).
//
// This package is used when you use `gomobile bind`.
// For `gomobile build`, you don't have to use this package.
//
// For usage, see https://github.com/hajimehoshi/ebiten/wiki/Mobile, https://github.com/hajimehoshi/ebiten/wiki/Android and https://github.com/hajimehoshi/ebiten/wiki/iOS.
package mobile

6
run.go
View File

@ -109,7 +109,7 @@ func run(width, height int, scale float64, title string, g *graphicsContext) err
// f is not called when the window is in background by default.
// This setting is configurable with SetRunnableInBackground.
//
// The given scale is ignored on fullscreen mode.
// The given scale is ignored on fullscreen mode or gomobile-build mode.
//
// Run returns error when 1) OpenGL error happens, 2) audio error happens or 3) f returns error.
// In the case of 3), Run returns the same error.
@ -139,8 +139,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
// RunWithoutMainLoop runs the game, but don't call the loop on the main (UI) thread.
// Different from Run, this function returns immediately.
//
// Typically, Ebiten users don't have to call this directly.
// Instead, functions in github.com/hajimehoshi/ebiten/mobile module call this.
// Ebiten users should NOT call this function.
// Instead, functions in github.com/hajimehoshi/ebiten/mobile package calls this.
func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, title string) <-chan error {
ch := make(chan error)
go func() {