diff --git a/internal/ui/run_notsinglethread.go b/internal/ui/run_notsinglethread.go index 93beb2a1d..e40711c10 100644 --- a/internal/ui/run_notsinglethread.go +++ b/internal/ui/run_notsinglethread.go @@ -60,7 +60,7 @@ func (u *UserInterface) Run(game Game) error { // u.t is updated to the new thread until runOnAnotherThreadFromMainThread is called. // // Inside f, another functions that must be called from the main thread can be called safely. -func (u *UserInterface) runOnAnotherThreadFromMainThread(f func() error) error { +func (u *UserInterface) runOnAnotherThreadFromMainThread(f func()) { // As this function is called from the main thread, u.t should never be accessed and can be updated here. t := u.t defer func() { @@ -71,11 +71,9 @@ func (u *UserInterface) runOnAnotherThreadFromMainThread(f func() error) error { u.t = thread.NewOSThread() graphicscommand.SetRenderingThread(u.t) - var err error go func() { defer u.t.Stop() - err = f() + f() }() u.t.Loop() - return err } diff --git a/internal/ui/run_singlethread.go b/internal/ui/run_singlethread.go index eb738fea8..2628b86dc 100644 --- a/internal/ui/run_singlethread.go +++ b/internal/ui/run_singlethread.go @@ -43,6 +43,6 @@ func (u *UserInterface) Run(game Game) error { return nil } -func (u *UserInterface) runOnAnotherThreadFromMainThread(f func() error) error { - return f() +func (u *UserInterface) runOnAnotherThreadFromMainThread(f func()) { + f() } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 54b585dde..57a76ffbf 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -109,6 +109,7 @@ type UserInterface struct { defaultFramebufferSizeCallback glfw.FramebufferSizeCallback framebufferSizeCallbackCh chan struct{} + // t is the main thread == the rendering thread. t thread.Thread m sync.RWMutex } @@ -701,33 +702,29 @@ func (u *UserInterface) registerWindowSetSizeCallback() { return } - if err := u.runOnAnotherThreadFromMainThread(func() error { - var outsideWidth, outsideHeight float64 - var deviceScaleFactor float64 + if width != 0 || height != 0 { + w := int(u.dipFromGLFWPixel(float64(width), u.currentMonitor())) + h := int(u.dipFromGLFWPixel(float64(height), u.currentMonitor())) + u.setWindowSizeInDIP(w, h, u.isFullscreen()) + } - u.t.Call(func() { - if width != 0 || height != 0 { - w := int(u.dipFromGLFWPixel(float64(width), u.currentMonitor())) - h := int(u.dipFromGLFWPixel(float64(height), u.currentMonitor())) - u.setWindowSizeInDIP(w, h, u.isFullscreen()) - } + outsideWidth, outsideHeight := u.updateSize() + deviceScaleFactor := u.deviceScaleFactor(u.currentMonitor()) - outsideWidth, outsideHeight = u.updateSize() - deviceScaleFactor = u.deviceScaleFactor(u.currentMonitor()) - }) + // In the game's update, u.t.Call might be called. + // In order to call it safely, use runOnAnotherThreadFromMainThread. + var err error + u.runOnAnotherThreadFromMainThread(func() { u.context.layout(outsideWidth, outsideHeight) - if err := u.context.forceUpdateFrame(deviceScaleFactor); err != nil { - return err - } - if graphics().IsGL() { - u.t.Call(func() { - u.swapBuffers() - }) - } - return nil - }); err != nil { + err = u.context.forceUpdateFrame(deviceScaleFactor) + }) + if err != nil { u.err = err } + + if graphics().IsGL() { + u.swapBuffers() + } }) } u.window.SetSizeCallback(u.sizeCallback)