graphics: Refactoring

This commit is contained in:
Hajime Hoshi 2016-07-03 16:18:29 +09:00
parent 375d71e085
commit 93f052de8c
3 changed files with 27 additions and 38 deletions

View File

@ -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
}
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 IsRunningSlowly() {
return nil
}
if err := c.offscreen2.Clear(); err != nil {
return err

View File

@ -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,13 +155,9 @@ 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 {
if err := g.UpdateAndDraw(tt); err != nil {
return err
}
}
} else {
if err := g.Draw(); err != nil {
return err

16
run.go
View File

@ -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.