From 93f052de8c10ffa9678eded561443b6df6de9cc6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Jul 2016 16:18:29 +0900 Subject: [PATCH] graphics: Refactoring --- graphicscontext.go | 18 +++++++++--------- internal/loop/run.go | 31 +++---------------------------- run.go | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index 6593b2c1b..1db531207 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -132,18 +132,18 @@ func (c *graphicsContext) drawToDefaultRenderTarget(context *opengl.Context) err return nil } -func (c *graphicsContext) UpdateAndDraw() error { +func (c *graphicsContext) UpdateAndDraw(updateCount int) error { if err := c.initializeIfNeeded(ui.GLContext()); err != nil { return err } - if err := c.offscreen.Clear(); err != nil { - return err - } - if err := c.f(c.offscreen); err != nil { - return err - } - if IsRunningSlowly() { - return nil + for i := 0; i < updateCount; i++ { + if err := c.offscreen.Clear(); err != nil { + return err + } + setRunningSlowly(i < updateCount-1) + if err := c.f(c.offscreen); err != nil { + return err + } } if err := c.offscreen2.Clear(); err != nil { return err diff --git a/internal/loop/run.go b/internal/loop/run.go index dfb7513d8..89941acfe 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -30,10 +30,6 @@ func IsRunning() bool { return currentRunContext.isRunning() } -func IsRunningSlowly() bool { - return currentRunContext.isRunningSlowly() -} - type runContext struct { running bool fps int @@ -81,25 +77,9 @@ func (c *runContext) updateFPS(fps float64) { c.currentFPS = fps } -func (c *runContext) isRunningSlowly() bool { - c.m.RLock() - defer c.m.RUnlock() - if !c.running { - // TODO: Should panic here? - return false - } - return c.runningSlowly -} - -func (c *runContext) setRunningSlowly(isRunningSlowly bool) { - c.m.Lock() - defer c.m.Unlock() - c.runningSlowly = isRunningSlowly -} - type GraphicsContext interface { SetSize(width, height int, scale float64) error - UpdateAndDraw() error + UpdateAndDraw(updateCount int) error Draw() error } @@ -161,7 +141,6 @@ func (c *runContext) render(g GraphicsContext) error { // If lastUpdated is too old, we assume that screen is not shown. if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated { - c.setRunningSlowly(false) c.lastUpdated = n return nil } @@ -176,12 +155,8 @@ func (c *runContext) render(g GraphicsContext) error { tt = 1 } if 1 <= tt { - for i := 0; i < tt; i++ { - slow := i < tt-1 - c.setRunningSlowly(slow) - if err := g.UpdateAndDraw(); err != nil { - return err - } + if err := g.UpdateAndDraw(tt); err != nil { + return err } } else { if err := g.Draw(); err != nil { diff --git a/run.go b/run.go index 1830978c4..75eb49840 100644 --- a/run.go +++ b/run.go @@ -15,6 +15,8 @@ package ebiten import ( + "sync/atomic" + "github.com/hajimehoshi/ebiten/internal/loop" "github.com/hajimehoshi/ebiten/internal/ui" ) @@ -34,13 +36,25 @@ func CurrentFPS() float64 { return loop.CurrentFPS() } +var ( + isRunningSlowly = int32(0) +) + +func setRunningSlowly(slow bool) { + v := int32(0) + if slow { + v = 1 + } + atomic.StoreInt32(&isRunningSlowly, v) +} + // IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering. // The game screen is not updated when IsRunningSlowly is true. // It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true. // // This function is concurrent-safe. func IsRunningSlowly() bool { - return loop.IsRunningSlowly() + return atomic.LoadInt32(&isRunningSlowly) != 0 } // Run runs the game.