mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
loop: Refactoring
This commit is contained in:
parent
b852f5a0f7
commit
ceea24ab95
@ -35,14 +35,14 @@ func IsRunningSlowly() bool {
|
||||
}
|
||||
|
||||
type runContext struct {
|
||||
running bool
|
||||
fps int
|
||||
currentFPS float64
|
||||
runningSlowly bool
|
||||
frames int
|
||||
beforeForUpdate int64
|
||||
beforeForFPS int64
|
||||
m sync.RWMutex
|
||||
running bool
|
||||
fps int
|
||||
currentFPS float64
|
||||
runningSlowly bool
|
||||
frames int
|
||||
lastUpdated int64
|
||||
lastFPSUpdated int64
|
||||
m sync.RWMutex
|
||||
}
|
||||
|
||||
var currentRunContext *runContext
|
||||
@ -120,8 +120,8 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
||||
defer ui.CurrentUI().Terminate()
|
||||
|
||||
n := now()
|
||||
currentRunContext.beforeForUpdate = n
|
||||
currentRunContext.beforeForFPS = n
|
||||
currentRunContext.lastUpdated = n
|
||||
currentRunContext.lastFPSUpdated = n
|
||||
for {
|
||||
e, err := ui.CurrentUI().Update()
|
||||
if err != nil {
|
||||
@ -148,44 +148,50 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
||||
func (c *runContext) render(g GraphicsContext) error {
|
||||
fps := c.fps
|
||||
n := now()
|
||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||
if 5*int64(time.Second)/int64(fps) < n-c.beforeForUpdate {
|
||||
c.setRunningSlowly(false)
|
||||
c.beforeForUpdate = n
|
||||
} else {
|
||||
// Note that generally t is a little different from 1/60[sec].
|
||||
t := n - c.beforeForUpdate
|
||||
tt := int(t * int64(fps) / int64(time.Second))
|
||||
// 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].
|
||||
if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
|
||||
tt = 1
|
||||
defer func() {
|
||||
// Calc the current FPS.
|
||||
if time.Second > time.Duration(n-c.lastFPSUpdated) {
|
||||
return
|
||||
}
|
||||
if 1 <= tt {
|
||||
for i := 0; i < tt; i++ {
|
||||
slow := i < tt-1
|
||||
c.setRunningSlowly(slow)
|
||||
if err := g.UpdateAndDraw(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := g.Draw(); err != nil {
|
||||
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.lastUpdated = n
|
||||
return nil
|
||||
}
|
||||
|
||||
// Note that generally t is a little different from 1/60[sec].
|
||||
t := n - c.lastUpdated
|
||||
tt := int(t * int64(fps) / int64(time.Second))
|
||||
|
||||
// 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].
|
||||
if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
|
||||
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 := ui.CurrentUI().SwapBuffers(); err != nil {
|
||||
} else {
|
||||
if err := g.Draw(); err != nil {
|
||||
return err
|
||||
}
|
||||
c.beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
||||
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
|
||||
if err := ui.CurrentUI().SwapBuffers(); err != nil {
|
||||
return err
|
||||
}
|
||||
c.lastUpdated += int64(tt) * int64(time.Second) / int64(fps)
|
||||
c.frames++
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user