Bug fix: ui.Main should be suspended when error occurs in the loop

This commit is contained in:
Hajime Hoshi 2016-08-02 02:47:45 +09:00
parent c7106f595c
commit 6968e898c3
6 changed files with 28 additions and 15 deletions

View File

@ -30,11 +30,14 @@ import (
func TestMain(m *testing.M) {
code := 0
// Run an Ebiten process so that (*Image).At is available.
regularTermination := errors.New("regular termination")
f := func(screen *Image) error {
code = m.Run()
return errors.New("regular termination")
return regularTermination
}
if err := Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
Run(f, 320, 240, 1, "Test")
os.Exit(code)
}

View File

@ -86,7 +86,7 @@ type GraphicsContext interface {
UpdateAndDraw(context *opengl.Context, updateCount int) error
}
func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) error {
func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) (err error) {
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
@ -100,7 +100,12 @@ func Run(g GraphicsContext, width, height int, scale float64, title string, fps
return err
}
// TODO: Use the error value
defer ui.CurrentUI().Terminate()
defer func() {
if err != nil {
return
}
err = ui.CurrentUI().Terminate()
}()
n := now()
currentRunContext.lastUpdated = n

View File

@ -76,14 +76,19 @@ func initialize() error {
return nil
}
func Main() error {
return currentUI.main()
func Main(ch <-chan error) error {
return currentUI.main(ch)
}
func (u *userInterface) main() error {
func (u *userInterface) main(ch <-chan error) error {
// TODO: Check this is done on the main thread.
for f := range u.funcs {
f()
for {
select {
case f := <-u.funcs:
f()
case err := <-ch:
return err
}
}
return nil
}

View File

@ -278,9 +278,8 @@ func devicePixelRatio() float64 {
return ratio
}
func Main() error {
// Do nothing
return nil
func Main(ch <-chan error) error {
return <-ch
}
func (u *userInterface) Start(width, height int, scale float64, title string) error {

View File

@ -24,7 +24,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
func Main() error {
func Main(ch <-chan error) error {
return errors.New("ui: don't call this: use RunWithoutMainLoop instead of Run")
}

5
run.go
View File

@ -81,10 +81,11 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
}
close(ch)
}()
if err := ui.Main(); err != nil {
// TODO: Use context in Go 1.7?
if err := ui.Main(ch); err != nil {
return err
}
return <-ch
return nil
}
// RunWithoutMainLoop runs the game, but don't call the loop on the main (UI) thread.