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 ( import (
"context" "context"
"errors"
"image" "image"
"runtime"
"sync" "sync"
"time" "time"
@ -56,21 +54,7 @@ func Get() *UserInterface {
return theUI return theUI
} }
func (u *UserInterface) Render(chError <-chan error) error { func (u *UserInterface) Render() {
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:
}
renderCh <- struct{}{} renderCh <- struct{}{}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
@ -78,7 +62,6 @@ func (u *UserInterface) Render(chError <-chan error) error {
cancel() cancel()
}() }()
opengl.Get().DoWork(ctx) opengl.Get().DoWork(ctx)
return nil
} }
type UserInterface struct { type UserInterface struct {

View File

@ -18,6 +18,7 @@ package mobile
import ( import (
"errors" "errors"
"runtime"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile" "github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
@ -29,13 +30,24 @@ var (
) )
func update() error { func update() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if chError == nil { if chError == nil {
return errors.New("mobile: chError must not be nil: Start is not called yet?") return errors.New("mobile: chError must not be nil: Start is not called yet?")
} }
if !running { if !running {
return errors.New("mobile: start must be called ahead of update") 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) { 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. // Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run.
func Update() error { func Update() error {
// TODO: Is it safe to call Lock before locking the OS thread?
mobileMutex.Lock() mobileMutex.Lock()
defer mobileMutex.Unlock() defer mobileMutex.Unlock()