internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2022-12-28 16:44:22 +09:00
parent e9248c6f33
commit 01eb1a0bca
3 changed files with 15 additions and 16 deletions

View File

@ -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). // Initialize the main thread first so the thread is available at u.run (#809).
u.mainThread = thread.NewOSThread() u.mainThread = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.mainThread) 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) ch := make(chan error, 1)
go func() { go func() {
defer u.mainThread.Stop()
defer close(ch) defer close(ch)
var err error if err := u.loopGame(); err != nil {
if u.mainThread.Call(func() {
err = u.init(options)
}); err != nil {
ch <- err
return
}
if err := u.loop(); err != nil {
ch <- err ch <- err
return return
} }
}() }()
u.setRunning(true) defer u.mainThread.Stop()
u.mainThread.Loop() u.mainThread.Loop()
u.setRunning(false)
return <-ch return <-ch
} }

View File

@ -29,16 +29,16 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
graphicscommand.SetRenderingThread(u.mainThread) graphicscommand.SetRenderingThread(u.mainThread)
u.setRunning(true) u.setRunning(true)
defer u.setRunning(false)
if err := u.init(options); err != nil { if err := u.init(options); err != nil {
return err return err
} }
if err := u.loop(); err != nil { if err := u.loopGame(); err != nil {
return err return err
} }
u.setRunning(false)
return nil return nil
} }

View File

@ -849,6 +849,7 @@ event:
u.framebufferSizeCallbackCh = nil u.framebufferSizeCallbackCh = nil
} }
// init must be called from the main thread.
func (u *userInterfaceImpl) init(options *RunOptions) error { func (u *userInterfaceImpl) init(options *RunOptions) error {
glfw.WindowHint(glfw.AutoIconify, glfw.False) glfw.WindowHint(glfw.AutoIconify, glfw.False)
@ -1065,7 +1066,7 @@ func (u *userInterfaceImpl) update() (float64, float64, error) {
return outsideWidth, outsideHeight, nil return outsideWidth, outsideHeight, nil
} }
func (u *userInterfaceImpl) loop() error { func (u *userInterfaceImpl) loopGame() error {
defer u.mainThread.Call(func() { defer u.mainThread.Call(func() {
u.window.Destroy() u.window.Destroy()
glfw.Terminate() glfw.Terminate()