From b85f5edee0f00d27934ab24c72db592d160b7ce0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 5 Aug 2017 21:24:04 +0900 Subject: [PATCH] loop: Simplify logic --- internal/loop/run.go | 13 ++++++------- run.go | 32 ++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/internal/loop/run.go b/internal/loop/run.go index ac2b9ca91..3218cfbd0 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -80,25 +80,24 @@ func (c *runContext) updateFPS(fps float64) { c.m.Unlock() } -type Runner interface { - Run(width, height int, scale float64, title string) error -} - -func Run(runner Runner, width, height int, scale float64, title string) (err error) { +func Start() error { if currentRunContext != nil { return errors.New("loop: The game is already running") } currentRunContext = &runContext{} currentRunContext.startRunning() - defer currentRunContext.endRunning() n := now() currentRunContext.lastUpdated = n currentRunContext.lastFPSUpdated = n close(contextInitCh) + return nil +} - return runner.Run(width, height, scale, title) +func End() { + currentRunContext.endRunning() + currentRunContext = nil } func (c *runContext) updateCount(now int64) int { diff --git a/run.go b/run.go index 3cf96853b..c62e3a311 100644 --- a/run.go +++ b/run.go @@ -60,12 +60,8 @@ func IsRunningSlowly() bool { var theGraphicsContext atomic.Value -type runner struct { - g *graphicsContext -} - -func (r *runner) Run(width, height int, scale float64, title string) error { - if err := ui.Run(width, height, scale, title, &updater{r.g}); err != nil { +func run(width, height int, scale float64, title string, g *graphicsContext) error { + if err := ui.Run(width, height, scale, title, &updater{g}); err != nil { if _, ok := err.(*ui.RegularTermination); ok { return nil } @@ -110,13 +106,19 @@ func (u *updater) Invalidate() { func Run(f func(*Image) error, width, height int, scale float64, title string) error { ch := make(chan error) go func() { + defer close(ch) + g := newGraphicsContext(f) theGraphicsContext.Store(g) - r := &runner{g} - if err := loop.Run(r, width, height, scale, title); err != nil { + if err := loop.Start(); err != nil { ch <- err + return } - close(ch) + if err := run(width, height, scale, title, g); err != nil { + ch <- err + return + } + loop.End() }() // TODO: Use context in Go 1.7? if err := ui.RunMainThreadLoop(ch); err != nil { @@ -135,13 +137,19 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, title string) <-chan error { ch := make(chan error) go func() { + defer close(ch) + g := newGraphicsContext(f) theGraphicsContext.Store(g) - r := &runner{g} - if err := loop.Run(r, width, height, scale, title); err != nil { + if err := loop.Start(); err != nil { ch <- err + return } - close(ch) + if err := run(width, height, scale, title, g); err != nil { + ch <- err + return + } + loop.End() }() return ch }