diff --git a/internal/ui/run_notsinglethread.go b/internal/ui/run_notsinglethread.go index a246301ee..5ff95a65d 100644 --- a/internal/ui/run_notsinglethread.go +++ b/internal/ui/run_notsinglethread.go @@ -26,31 +26,29 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { // Initialize the main thread first so the thread is available at u.run (#809). u.mainThread = thread.NewOSThread() + graphicscommand.SetRenderingThread(u.mainThread) + u.setRunning(true) + defer u.setRunning(false) + + if err := u.init(options); err != nil { + return err + } + ch := make(chan error, 1) go func() { - defer u.mainThread.Stop() - defer close(ch) - var err error - if u.mainThread.Call(func() { - err = u.init(options) - }); err != nil { - ch <- err - return - } - - if err := u.loop(); err != nil { + if err := u.loopGame(); err != nil { ch <- err return } }() - u.setRunning(true) + defer u.mainThread.Stop() u.mainThread.Loop() - u.setRunning(false) + return <-ch } diff --git a/internal/ui/run_singlethread.go b/internal/ui/run_singlethread.go index 3ce69bbd7..5cd3be2a9 100644 --- a/internal/ui/run_singlethread.go +++ b/internal/ui/run_singlethread.go @@ -29,16 +29,16 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { graphicscommand.SetRenderingThread(u.mainThread) u.setRunning(true) + defer u.setRunning(false) if err := u.init(options); err != nil { return err } - if err := u.loop(); err != nil { + if err := u.loopGame(); err != nil { return err } - u.setRunning(false) return nil } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 00e5ea3b3..b99d97886 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -849,6 +849,7 @@ event: u.framebufferSizeCallbackCh = nil } +// init must be called from the main thread. func (u *userInterfaceImpl) init(options *RunOptions) error { glfw.WindowHint(glfw.AutoIconify, glfw.False) @@ -1065,7 +1066,7 @@ func (u *userInterfaceImpl) update() (float64, float64, error) { return outsideWidth, outsideHeight, nil } -func (u *userInterfaceImpl) loop() error { +func (u *userInterfaceImpl) loopGame() error { defer u.mainThread.Call(func() { u.window.Destroy() glfw.Terminate()