internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2023-01-03 21:17:47 +09:00
parent 0bdfeec610
commit 67c9d65d82

View File

@ -18,7 +18,8 @@ package ui
import ( import (
stdcontext "context" stdcontext "context"
"errors"
"golang.org/x/sync/errgroup"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
@ -41,24 +42,22 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
ctx, cancel := stdcontext.WithCancel(stdcontext.Background()) ctx, cancel := stdcontext.WithCancel(stdcontext.Background())
defer cancel() defer cancel()
go func() { var wg errgroup.Group
// Run the render thread.
wg.Go(func() error {
defer cancel()
_ = u.renderThread.Loop(ctx) _ = u.renderThread.Loop(ctx)
}() return nil
})
ch := make(chan error, 1) // Run the game thread.
go func() { wg.Go(func() error {
defer close(ch) defer cancel()
return u.loopGame()
})
if err := u.loopGame(); err != nil { // Run the main thread.
ch <- err _ = u.mainThread.Loop(ctx)
cancel() return wg.Wait()
return
}
}()
err := u.mainThread.Loop(ctx)
if errors.Is(err, stdcontext.Canceled) {
return <-ch
}
return err
} }