From 67c9d65d825bcd7cd5af7c518e711503dca874ac Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 3 Jan 2023 21:17:47 +0900 Subject: [PATCH] internal/ui: refactoring --- internal/ui/run_glfw_notsinglethread.go | 35 ++++++++++++------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/internal/ui/run_glfw_notsinglethread.go b/internal/ui/run_glfw_notsinglethread.go index ab6ac6222..1ef3acf48 100644 --- a/internal/ui/run_glfw_notsinglethread.go +++ b/internal/ui/run_glfw_notsinglethread.go @@ -18,7 +18,8 @@ package ui import ( stdcontext "context" - "errors" + + "golang.org/x/sync/errgroup" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "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()) defer cancel() - go func() { + var wg errgroup.Group + + // Run the render thread. + wg.Go(func() error { + defer cancel() _ = u.renderThread.Loop(ctx) - }() + return nil + }) - ch := make(chan error, 1) - go func() { - defer close(ch) + // Run the game thread. + wg.Go(func() error { + defer cancel() + return u.loopGame() + }) - if err := u.loopGame(); err != nil { - ch <- err - cancel() - return - } - }() - - err := u.mainThread.Loop(ctx) - if errors.Is(err, stdcontext.Canceled) { - return <-ch - } - return err + // Run the main thread. + _ = u.mainThread.Loop(ctx) + return wg.Wait() }