graphics: Reduce calls of ui.GLContext()

This commit is contained in:
Hajime Hoshi 2016-07-03 17:27:33 +09:00
parent f29ff4a9e2
commit fbc1a4238c
2 changed files with 12 additions and 12 deletions

View File

@ -19,7 +19,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui"
) )
func newGraphicsContext(f func(*Image) error) *graphicsContext { func newGraphicsContext(f func(*Image) error) *graphicsContext {
@ -131,8 +130,8 @@ func (c *graphicsContext) drawToDefaultRenderTarget(context *opengl.Context) err
return nil return nil
} }
func (c *graphicsContext) UpdateAndDraw(updateCount int) error { func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int) error {
if err := c.initializeIfNeeded(ui.GLContext()); err != nil { if err := c.initializeIfNeeded(context); err != nil {
return err return err
} }
for i := 0; i < updateCount; i++ { for i := 0; i < updateCount; i++ {
@ -150,20 +149,20 @@ func (c *graphicsContext) UpdateAndDraw(updateCount int) error {
if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil { if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil {
return err return err
} }
if err := c.drawToDefaultRenderTarget(ui.GLContext()); err != nil { if err := c.drawToDefaultRenderTarget(context); err != nil {
return err return err
} }
if err := theImages.savePixels(ui.GLContext()); err != nil { if err := theImages.savePixels(context); err != nil {
return err return err
} }
return nil return nil
} }
func (c *graphicsContext) Draw() error { func (c *graphicsContext) Draw(context *opengl.Context) error {
if err := c.initializeIfNeeded(ui.GLContext()); err != nil { if err := c.initializeIfNeeded(context); err != nil {
return err return err
} }
if err := c.drawToDefaultRenderTarget(ui.GLContext()); err != nil { if err := c.drawToDefaultRenderTarget(context); err != nil {
return err return err
} }
return nil return nil

View File

@ -19,6 +19,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
) )
@ -79,8 +80,8 @@ func (c *runContext) updateFPS(fps float64) {
type GraphicsContext interface { type GraphicsContext interface {
SetSize(width, height int, scale float64) error SetSize(width, height int, scale float64) error
UpdateAndDraw(updateCount int) error UpdateAndDraw(context *opengl.Context, updateCount int) error
Draw() error Draw(context *opengl.Context) error
} }
func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) error { func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) error {
@ -155,11 +156,11 @@ func (c *runContext) render(g GraphicsContext) error {
tt = 1 tt = 1
} }
if 1 <= tt { if 1 <= tt {
if err := g.UpdateAndDraw(tt); err != nil { if err := g.UpdateAndDraw(ui.GLContext(), tt); err != nil {
return err return err
} }
} else { } else {
if err := g.Draw(); err != nil { if err := g.Draw(ui.GLContext()); err != nil {
return err return err
} }
} }