mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
ui: Introduce IsRunningSlowly (#168)
This commit is contained in:
parent
8132264a88
commit
b3d7e04de5
@ -46,6 +46,8 @@ func (game *Game) Update(r *ebiten.Image) error {
|
|||||||
SceneManager: game.sceneManager,
|
SceneManager: game.sceneManager,
|
||||||
Input: &game.input,
|
Input: &game.input,
|
||||||
})
|
})
|
||||||
|
if !ebiten.IsRunningSlowly() {
|
||||||
game.sceneManager.Draw(r)
|
game.sceneManager.Draw(r)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,9 @@ func (c *graphicsContext) update(f func(*Image) error) error {
|
|||||||
if err := f(c.screen); err != nil {
|
if err := f(c.screen); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if IsRunningSlowly() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
// TODO: In WebGL, we don't need to clear the image here.
|
// TODO: In WebGL, we don't need to clear the image here.
|
||||||
if err := c.defaultRenderTarget.Clear(); err != nil {
|
if err := c.defaultRenderTarget.Clear(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
10
run.go
10
run.go
@ -26,6 +26,7 @@ var runContext = &struct {
|
|||||||
newScreenWidth int
|
newScreenWidth int
|
||||||
newScreenHeight int
|
newScreenHeight int
|
||||||
newScreenScale int
|
newScreenScale int
|
||||||
|
isRunningSlowly bool
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
// CurrentFPS returns the current number of frames per second.
|
// CurrentFPS returns the current number of frames per second.
|
||||||
@ -33,6 +34,13 @@ func CurrentFPS() float64 {
|
|||||||
return runContext.fps
|
return runContext.fps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS.
|
||||||
|
// The game screen is not updated when IsRunningSlowly is true.
|
||||||
|
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
||||||
|
func IsRunningSlowly() bool {
|
||||||
|
return runContext.isRunningSlowly
|
||||||
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@ -94,10 +102,12 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
}
|
}
|
||||||
now := ui.Now()
|
now := ui.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.
|
||||||
|
runContext.isRunningSlowly = false
|
||||||
if int64(5*time.Second/60) < now-beforeForUpdate {
|
if int64(5*time.Second/60) < now-beforeForUpdate {
|
||||||
beforeForUpdate = now
|
beforeForUpdate = now
|
||||||
} else {
|
} else {
|
||||||
c := int((now - beforeForUpdate) * 60 / int64(time.Second))
|
c := int((now - beforeForUpdate) * 60 / int64(time.Second))
|
||||||
|
runContext.isRunningSlowly = c >= 2
|
||||||
for i := 0; i < c; i++ {
|
for i := 0; i < c; i++ {
|
||||||
if err := graphicsContext.update(f); err != nil {
|
if err := graphicsContext.update(f); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user