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.
//
// 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
}

View File

@ -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()
}

View File

@ -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)