mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
loop: Refactoring: Remove FPS
This commit is contained in:
parent
dbd6ed8139
commit
b1afe6aeb2
@ -21,8 +21,6 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FPS = 60
|
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
return currentRunContext.currentFPS()
|
return currentRunContext.currentFPS()
|
||||||
}
|
}
|
||||||
@ -99,7 +97,7 @@ type GraphicsContext interface {
|
|||||||
Update() error
|
Update() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(g GraphicsContext, width, height, scale int, title string) error {
|
func Run(g GraphicsContext, width, height, scale int, title string, fps int) error {
|
||||||
currentRunContext.startRunning()
|
currentRunContext.startRunning()
|
||||||
defer currentRunContext.endRunning()
|
defer currentRunContext.endRunning()
|
||||||
|
|
||||||
@ -127,17 +125,17 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
|
|||||||
case ui.RenderEvent:
|
case ui.RenderEvent:
|
||||||
n2 := now()
|
n2 := now()
|
||||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||||
if int64(5*time.Second/FPS) < n2-beforeForUpdate {
|
if 5*int64(time.Second)/int64(fps) < n2-beforeForUpdate {
|
||||||
currentRunContext.setRunningSlowly(false)
|
currentRunContext.setRunningSlowly(false)
|
||||||
beforeForUpdate = n2
|
beforeForUpdate = n2
|
||||||
} else {
|
} else {
|
||||||
// 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 := n2 - beforeForUpdate
|
t := n2 - beforeForUpdate
|
||||||
currentRunContext.setRunningSlowly(t*FPS >= int64(time.Second*5/2))
|
currentRunContext.setRunningSlowly(t*int64(fps) >= int64(time.Second*5/2))
|
||||||
tt := int(t * 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)/FPS-int64(5*time.Millisecond)) < t {
|
if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
|
||||||
tt = 1
|
tt = 1
|
||||||
}
|
}
|
||||||
for i := 0; i < tt; i++ {
|
for i := 0; i < tt; i++ {
|
||||||
@ -146,7 +144,7 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ui.CurrentUI().SwapBuffers()
|
ui.CurrentUI().SwapBuffers()
|
||||||
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
||||||
frames++
|
frames++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
run.go
4
run.go
@ -20,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FPS represents how many times game updating happens in a second.
|
// FPS represents how many times game updating happens in a second.
|
||||||
const FPS = loop.FPS
|
const FPS = 60
|
||||||
|
|
||||||
// CurrentFPS returns the current number of frames per second of rendering.
|
// CurrentFPS returns the current number of frames per second of rendering.
|
||||||
//
|
//
|
||||||
@ -57,7 +57,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
ch := make(chan error)
|
ch := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
g := newGraphicsContext(f)
|
g := newGraphicsContext(f)
|
||||||
ch <- loop.Run(g, width, height, scale, title)
|
ch <- loop.Run(g, width, height, scale, title, FPS)
|
||||||
}()
|
}()
|
||||||
ui.Main()
|
ui.Main()
|
||||||
return <-ch
|
return <-ch
|
||||||
|
Loading…
Reference in New Issue
Block a user