mobile: Refactoring

This commit is contained in:
Hajime Hoshi 2019-06-08 01:53:39 +09:00
parent 449679665b
commit fa377ce9d1
3 changed files with 15 additions and 19 deletions

View File

@ -18,9 +18,7 @@ package mobile
import (
"context"
"errors"
"image"
"runtime"
"sync"
"time"
@ -56,21 +54,7 @@ func Get() *UserInterface {
return theUI
}
func (u *UserInterface) Render(chError <-chan error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if chError == nil {
return errors.New("ui: chError must not be nil")
}
// TODO: Check this is called on the rendering thread
select {
case err := <-chError:
return err
default:
}
func (u *UserInterface) Render() {
renderCh <- struct{}{}
ctx, cancel := context.WithCancel(context.Background())
go func() {
@ -78,7 +62,6 @@ func (u *UserInterface) Render(chError <-chan error) error {
cancel()
}()
opengl.Get().DoWork(ctx)
return nil
}
type UserInterface struct {

View File

@ -18,6 +18,7 @@ package mobile
import (
"errors"
"runtime"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
@ -29,13 +30,24 @@ var (
)
func update() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if chError == nil {
return errors.New("mobile: chError must not be nil: Start is not called yet?")
}
if !running {
return errors.New("mobile: start must be called ahead of update")
}
return mobile.Get().Render(chError)
select {
case err := <-chError:
return err
default:
}
mobile.Get().Render()
return nil
}
func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) {

View File

@ -63,6 +63,7 @@ func Start(f func(*ebiten.Image) error, width, height int, scale float64, title
//
// Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run.
func Update() error {
// TODO: Is it safe to call Lock before locking the OS thread?
mobileMutex.Lock()
defer mobileMutex.Unlock()