From 146c1e5846ad74b707064686211063bf67d91eb6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Jul 2016 18:56:32 +0900 Subject: [PATCH] graphics: Refactoring: Remove graphicsContext.Draw and flush --- graphicscontext.go | 47 ++++++++++++++++---------------------------- internal/loop/run.go | 11 ++--------- 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index a001fa117..b745ce5de 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -124,9 +124,14 @@ func (c *graphicsContext) drawToDefaultRenderTarget(context *opengl.Context) err if err := drawWithFittingScale(c.screen, c.offscreen2); err != nil { return err } - if err := c.flush(context); err != nil { + // TODO: imageM is necessary to call graphics functions. Move this to graphics package. + imageM.Lock() + defer imageM.Unlock() + if err := graphics.FlushCommands(context); err != nil { return err } + // Call glFlush to prevent black flicking (especially on Android (#226) and iOS). + context.Flush() return nil } @@ -143,43 +148,25 @@ func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int return err } } - if err := c.offscreen2.Clear(); err != nil { - return err - } - if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil { - return err + if 0 < updateCount { + if err := c.offscreen2.Clear(); err != nil { + return err + } + if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil { + return err + } } if err := c.drawToDefaultRenderTarget(context); err != nil { return err } - if err := theImages.savePixels(context); err != nil { - return err + if 0 < updateCount { + if err := theImages.savePixels(context); err != nil { + return err + } } return nil } -func (c *graphicsContext) Draw(context *opengl.Context) error { - if err := c.initializeIfNeeded(context); err != nil { - return err - } - if err := c.drawToDefaultRenderTarget(context); err != nil { - return err - } - return nil -} - -func (c *graphicsContext) flush(context *opengl.Context) error { - // TODO: imageM is necessary to call graphics functions. Move this to graphics package. - imageM.Lock() - defer imageM.Unlock() - if err := graphics.FlushCommands(context); err != nil { - return err - } - // Call glFlush to prevent black flicking (especially on Android (#226) and iOS). - context.Flush() - return nil -} - func (c *graphicsContext) restore(context *opengl.Context) error { imageM.Lock() defer imageM.Unlock() diff --git a/internal/loop/run.go b/internal/loop/run.go index 94360a258..995724a00 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -81,7 +81,6 @@ func (c *runContext) updateFPS(fps float64) { type GraphicsContext interface { SetSize(width, height int, scale float64) error UpdateAndDraw(context *opengl.Context, updateCount int) error - Draw(context *opengl.Context) error } func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) error { @@ -155,14 +154,8 @@ func (c *runContext) render(g GraphicsContext) error { if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t { tt = 1 } - if 1 <= tt { - if err := g.UpdateAndDraw(ui.GLContext(), tt); err != nil { - return err - } - } else { - if err := g.Draw(ui.GLContext()); err != nil { - return err - } + if err := g.UpdateAndDraw(ui.GLContext(), tt); err != nil { + return err } if err := ui.CurrentUI().SwapBuffers(); err != nil { return err