mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-05 07:24:30 +01:00
More precise game loop
This commit is contained in:
parent
5f0bc87402
commit
038613aaf9
24
run.go
24
run.go
@ -20,6 +20,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fps = 0
|
||||||
|
|
||||||
// Run runs the game.
|
// Run runs the game.
|
||||||
// f is a function which is called at every frame.
|
// f is a function which is called at every frame.
|
||||||
// The argument (*Image) is the render target that represents the screen.
|
// The argument (*Image) is the render target that represents the screen.
|
||||||
@ -44,9 +46,10 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
frames := 0
|
||||||
|
t0 := time.Now().UnixNano()
|
||||||
|
tt0 := t0
|
||||||
for {
|
for {
|
||||||
// To avoid busy loop when the window is inactive, wait 1/120 [sec] at least.
|
|
||||||
ch := time.After(1 * time.Second / 120)
|
|
||||||
ui.DoEvents()
|
ui.DoEvents()
|
||||||
if ui.IsClosed() {
|
if ui.IsClosed() {
|
||||||
return nil
|
return nil
|
||||||
@ -70,6 +73,21 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
<-ch
|
|
||||||
|
// Wait if the frame is too fast.
|
||||||
|
now := time.Now().UnixNano()
|
||||||
|
d := time.Duration(now - t0)
|
||||||
|
if d < time.Second/90 {
|
||||||
|
time.Sleep(time.Second/60 - d)
|
||||||
|
}
|
||||||
|
t0 = now
|
||||||
|
|
||||||
|
// Calc the current FPS.
|
||||||
|
frames++
|
||||||
|
if time.Second <= time.Duration(now-tt0) {
|
||||||
|
fps = frames
|
||||||
|
tt0 = now
|
||||||
|
frames = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user