internal/ui: refactoring: remove an error value from runOnAnotherThreadFromMainThread

This commit is contained in:
Hajime Hoshi 2022-02-14 00:36:59 +09:00
parent ce3f83958e
commit e09c1bf8f1
3 changed files with 23 additions and 28 deletions

View File

@ -60,7 +60,7 @@ func (u *UserInterface) Run(game Game) error {
// u.t is updated to the new thread until runOnAnotherThreadFromMainThread is called. // 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. // 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. // As this function is called from the main thread, u.t should never be accessed and can be updated here.
t := u.t t := u.t
defer func() { defer func() {
@ -71,11 +71,9 @@ func (u *UserInterface) runOnAnotherThreadFromMainThread(f func() error) error {
u.t = thread.NewOSThread() u.t = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t) graphicscommand.SetRenderingThread(u.t)
var err error
go func() { go func() {
defer u.t.Stop() defer u.t.Stop()
err = f() f()
}() }()
u.t.Loop() u.t.Loop()
return err
} }

View File

@ -43,6 +43,6 @@ func (u *UserInterface) Run(game Game) error {
return nil return nil
} }
func (u *UserInterface) runOnAnotherThreadFromMainThread(f func() error) error { func (u *UserInterface) runOnAnotherThreadFromMainThread(f func()) {
return f() f()
} }

View File

@ -109,6 +109,7 @@ type UserInterface struct {
defaultFramebufferSizeCallback glfw.FramebufferSizeCallback defaultFramebufferSizeCallback glfw.FramebufferSizeCallback
framebufferSizeCallbackCh chan struct{} framebufferSizeCallbackCh chan struct{}
// t is the main thread == the rendering thread.
t thread.Thread t thread.Thread
m sync.RWMutex m sync.RWMutex
} }
@ -701,33 +702,29 @@ func (u *UserInterface) registerWindowSetSizeCallback() {
return return
} }
if err := u.runOnAnotherThreadFromMainThread(func() error { if width != 0 || height != 0 {
var outsideWidth, outsideHeight float64 w := int(u.dipFromGLFWPixel(float64(width), u.currentMonitor()))
var deviceScaleFactor float64 h := int(u.dipFromGLFWPixel(float64(height), u.currentMonitor()))
u.setWindowSizeInDIP(w, h, u.isFullscreen())
}
u.t.Call(func() { outsideWidth, outsideHeight := u.updateSize()
if width != 0 || height != 0 { deviceScaleFactor := u.deviceScaleFactor(u.currentMonitor())
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() // In the game's update, u.t.Call might be called.
deviceScaleFactor = u.deviceScaleFactor(u.currentMonitor()) // In order to call it safely, use runOnAnotherThreadFromMainThread.
}) var err error
u.runOnAnotherThreadFromMainThread(func() {
u.context.layout(outsideWidth, outsideHeight) u.context.layout(outsideWidth, outsideHeight)
if err := u.context.forceUpdateFrame(deviceScaleFactor); err != nil { err = u.context.forceUpdateFrame(deviceScaleFactor)
return err })
} if err != nil {
if graphics().IsGL() {
u.t.Call(func() {
u.swapBuffers()
})
}
return nil
}); err != nil {
u.err = err u.err = err
} }
if graphics().IsGL() {
u.swapBuffers()
}
}) })
} }
u.window.SetSizeCallback(u.sizeCallback) u.window.SetSizeCallback(u.sizeCallback)