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

View File

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

View File

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