From eef8289854c1a440051baae70fad6080dda838f3 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 5 Aug 2017 20:43:49 +0900 Subject: [PATCH] loop: Remove dependency on ui --- internal/loop/run.go | 48 ++++++++++++++------------------------------ run.go | 32 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/internal/loop/run.go b/internal/loop/run.go index fcc5f928e..ac2b9ca91 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -20,7 +20,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/sync" - "github.com/hajimehoshi/ebiten/internal/ui" ) func CurrentFPS() float64 { @@ -81,30 +80,11 @@ func (c *runContext) updateFPS(fps float64) { c.m.Unlock() } -type GraphicsContext interface { - SetSize(width, height int, scale float64) - Update(updateCount int) error - Invalidate() +type Runner interface { + Run(width, height int, scale float64, title string) error } -type loopGraphicsContext struct { - runContext *runContext - graphicsContext GraphicsContext -} - -func (g *loopGraphicsContext) SetSize(width, height int, scale float64) { - g.graphicsContext.SetSize(width, height, scale) -} - -func (g *loopGraphicsContext) Update() error { - return g.runContext.render(g.graphicsContext) -} - -func (g *loopGraphicsContext) Invalidate() { - g.graphicsContext.Invalidate() -} - -func Run(g GraphicsContext, width, height int, scale float64, title string) (err error) { +func Run(runner Runner, width, height int, scale float64, title string) (err error) { if currentRunContext != nil { return errors.New("loop: The game is already running") } @@ -118,14 +98,7 @@ func Run(g GraphicsContext, width, height int, scale float64, title string) (err close(contextInitCh) - lg := &loopGraphicsContext{currentRunContext, g} - if err := ui.Run(width, height, scale, title, lg); err != nil { - if _, ok := err.(*ui.RegularTermination); ok { - return nil - } - return err - } - return nil + return runner.Run(width, height, scale, title) } func (c *runContext) updateCount(now int64) int { @@ -187,7 +160,16 @@ func (c *runContext) registerPing(ping func()) { c.m.Unlock() } -func (c *runContext) render(g GraphicsContext) error { +type Updater interface { + Update(updateCount int) error +} + +func Update(u Updater) error { + <-contextInitCh + return currentRunContext.update(u) +} + +func (c *runContext) update(u Updater) error { n := now() c.m.Lock() @@ -197,7 +179,7 @@ func (c *runContext) render(g GraphicsContext) error { c.m.Unlock() count := c.updateCount(n) - if err := g.Update(count); err != nil { + if err := u.Update(count); err != nil { return err } c.framesForFPS++ diff --git a/run.go b/run.go index 197c92139..69b06e416 100644 --- a/run.go +++ b/run.go @@ -60,6 +60,32 @@ 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, r); err != nil { + if _, ok := err.(*ui.RegularTermination); ok { + return nil + } + return err + } + return nil +} + +func (r *runner) SetSize(width, height int, scale float64) { + r.g.SetSize(width, height, scale) +} + +func (r *runner) Update() error { + return loop.Update(r.g) +} + +func (r *runner) Invalidate() { + r.g.Invalidate() +} + // Run runs the game. // f is a function which is called at every frame. // The argument (*Image) is the render target that represents the screen. @@ -82,7 +108,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e go func() { g := newGraphicsContext(f) theGraphicsContext.Store(g) - if err := loop.Run(g, width, height, scale, title); err != nil { + r := &runner{g} + if err := loop.Run(r, width, height, scale, title); err != nil { ch <- err } close(ch) @@ -106,7 +133,8 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, go func() { g := newGraphicsContext(f) theGraphicsContext.Store(g) - if err := loop.Run(g, width, height, scale, title); err != nil { + r := &runner{g} + if err := loop.Run(r, width, height, scale, title); err != nil { ch <- err } close(ch)