mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 03:58:55 +01:00
loop: Refactoring
This commit is contained in:
parent
b852f5a0f7
commit
ceea24ab95
@ -40,8 +40,8 @@ type runContext struct {
|
|||||||
currentFPS float64
|
currentFPS float64
|
||||||
runningSlowly bool
|
runningSlowly bool
|
||||||
frames int
|
frames int
|
||||||
beforeForUpdate int64
|
lastUpdated int64
|
||||||
beforeForFPS int64
|
lastFPSUpdated int64
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,8 +120,8 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
defer ui.CurrentUI().Terminate()
|
defer ui.CurrentUI().Terminate()
|
||||||
|
|
||||||
n := now()
|
n := now()
|
||||||
currentRunContext.beforeForUpdate = n
|
currentRunContext.lastUpdated = n
|
||||||
currentRunContext.beforeForFPS = n
|
currentRunContext.lastFPSUpdated = n
|
||||||
for {
|
for {
|
||||||
e, err := ui.CurrentUI().Update()
|
e, err := ui.CurrentUI().Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -148,14 +148,28 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
func (c *runContext) render(g GraphicsContext) error {
|
func (c *runContext) render(g GraphicsContext) error {
|
||||||
fps := c.fps
|
fps := c.fps
|
||||||
n := now()
|
n := now()
|
||||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
defer func() {
|
||||||
if 5*int64(time.Second)/int64(fps) < n-c.beforeForUpdate {
|
// Calc the current FPS.
|
||||||
|
if time.Second > time.Duration(n-c.lastFPSUpdated) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
currentFPS := float64(c.frames) * float64(time.Second) / float64(n-c.lastFPSUpdated)
|
||||||
|
c.updateFPS(currentFPS)
|
||||||
|
c.lastFPSUpdated = n
|
||||||
|
c.frames = 0
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 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.setRunningSlowly(false)
|
||||||
c.beforeForUpdate = n
|
c.lastUpdated = n
|
||||||
} else {
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Note that generally t is a little different from 1/60[sec].
|
// Note that generally t is a little different from 1/60[sec].
|
||||||
t := n - c.beforeForUpdate
|
t := n - c.lastUpdated
|
||||||
tt := int(t * int64(fps) / int64(time.Second))
|
tt := int(t * int64(fps) / int64(time.Second))
|
||||||
|
|
||||||
// As t is not accurate 1/60[sec], errors are accumulated.
|
// As t is not accurate 1/60[sec], errors are accumulated.
|
||||||
// To make the FPS stable, set tt 1 if t is a little less than 1/60[sec].
|
// To make the FPS stable, set tt 1 if t is a little less than 1/60[sec].
|
||||||
if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
|
if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
|
||||||
@ -177,15 +191,7 @@ func (c *runContext) render(g GraphicsContext) error {
|
|||||||
if err := ui.CurrentUI().SwapBuffers(); err != nil {
|
if err := ui.CurrentUI().SwapBuffers(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
c.lastUpdated += int64(tt) * int64(time.Second) / int64(fps)
|
||||||
c.frames++
|
c.frames++
|
||||||
}
|
|
||||||
// Calc the current FPS.
|
|
||||||
if time.Second <= time.Duration(n-c.beforeForFPS) {
|
|
||||||
fps := float64(c.frames) * float64(time.Second) / float64(n-c.beforeForFPS)
|
|
||||||
c.updateFPS(fps)
|
|
||||||
c.beforeForFPS = n
|
|
||||||
c.frames = 0
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user