mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
loop: Refactoring
This commit is contained in:
parent
6640f60c88
commit
218df19ead
@ -23,7 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
return currentRunContext.currentFPS()
|
return currentRunContext.getCurrentFPS()
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsRunning() bool {
|
func IsRunning() bool {
|
||||||
@ -35,10 +35,14 @@ func IsRunningSlowly() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type runContext struct {
|
type runContext struct {
|
||||||
running bool
|
running bool
|
||||||
fps float64
|
fps int
|
||||||
runningSlowly bool
|
currentFPS float64
|
||||||
m sync.RWMutex
|
runningSlowly bool
|
||||||
|
frames int
|
||||||
|
beforeForUpdate int64
|
||||||
|
beforeForFPS int64
|
||||||
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentRunContext *runContext
|
var currentRunContext *runContext
|
||||||
@ -61,20 +65,20 @@ func (c *runContext) endRunning() {
|
|||||||
c.running = false
|
c.running = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *runContext) currentFPS() float64 {
|
func (c *runContext) getCurrentFPS() float64 {
|
||||||
c.m.RLock()
|
c.m.RLock()
|
||||||
defer c.m.RUnlock()
|
defer c.m.RUnlock()
|
||||||
if !c.running {
|
if !c.running {
|
||||||
// TODO: Should panic here?
|
// TODO: Should panic here?
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return c.fps
|
return c.currentFPS
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *runContext) updateFPS(fps float64) {
|
func (c *runContext) updateFPS(fps float64) {
|
||||||
c.m.Lock()
|
c.m.Lock()
|
||||||
defer c.m.Unlock()
|
defer c.m.Unlock()
|
||||||
c.fps = fps
|
c.currentFPS = fps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *runContext) isRunningSlowly() bool {
|
func (c *runContext) isRunningSlowly() bool {
|
||||||
@ -103,7 +107,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
if currentRunContext != nil {
|
if currentRunContext != nil {
|
||||||
return errors.New("loop: The game is already running")
|
return errors.New("loop: The game is already running")
|
||||||
}
|
}
|
||||||
currentRunContext = &runContext{}
|
currentRunContext = &runContext{
|
||||||
|
fps: fps,
|
||||||
|
}
|
||||||
currentRunContext.startRunning()
|
currentRunContext.startRunning()
|
||||||
defer currentRunContext.endRunning()
|
defer currentRunContext.endRunning()
|
||||||
|
|
||||||
@ -113,10 +119,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
// TODO: Use the error value
|
// TODO: Use the error value
|
||||||
defer ui.CurrentUI().Terminate()
|
defer ui.CurrentUI().Terminate()
|
||||||
|
|
||||||
frames := 0
|
|
||||||
n := now()
|
n := now()
|
||||||
beforeForUpdate := n
|
currentRunContext.beforeForUpdate = n
|
||||||
beforeForFPS := n
|
currentRunContext.beforeForFPS = n
|
||||||
for {
|
for {
|
||||||
e, err := ui.CurrentUI().Update()
|
e, err := ui.CurrentUI().Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -130,45 +135,8 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
case ui.CloseEvent:
|
case ui.CloseEvent:
|
||||||
return nil
|
return nil
|
||||||
case ui.RenderEvent:
|
case ui.RenderEvent:
|
||||||
n2 := now()
|
if err := currentRunContext.render(g); err != nil {
|
||||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
return err
|
||||||
if 5*int64(time.Second)/int64(fps) < n2-beforeForUpdate {
|
|
||||||
currentRunContext.setRunningSlowly(false)
|
|
||||||
beforeForUpdate = n2
|
|
||||||
} else {
|
|
||||||
// Note that generally t is a little different from 1/60[sec].
|
|
||||||
t := n2 - 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
|
|
||||||
}
|
|
||||||
if 1 <= tt {
|
|
||||||
for i := 0; i < tt; i++ {
|
|
||||||
slow := i < tt-1
|
|
||||||
currentRunContext.setRunningSlowly(slow)
|
|
||||||
if err := g.UpdateAndDraw(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := g.Draw(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := ui.CurrentUI().SwapBuffers(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
|
||||||
frames++
|
|
||||||
}
|
|
||||||
// Calc the current FPS.
|
|
||||||
if time.Second <= time.Duration(n2-beforeForFPS) {
|
|
||||||
fps := float64(frames) * float64(time.Second) / float64(n2-beforeForFPS)
|
|
||||||
currentRunContext.updateFPS(fps)
|
|
||||||
beforeForFPS = n2
|
|
||||||
frames = 0
|
|
||||||
}
|
}
|
||||||
e.Done <- struct{}{}
|
e.Done <- struct{}{}
|
||||||
default:
|
default:
|
||||||
@ -176,3 +144,48 @@ 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
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := ui.CurrentUI().SwapBuffers(); 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
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user