mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +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,
|
||||
Input: &game.input,
|
||||
})
|
||||
game.sceneManager.Draw(r)
|
||||
if !ebiten.IsRunningSlowly() {
|
||||
game.sceneManager.Draw(r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -39,6 +39,9 @@ func (c *graphicsContext) update(f func(*Image) error) error {
|
||||
if err := f(c.screen); err != nil {
|
||||
return err
|
||||
}
|
||||
if IsRunningSlowly() {
|
||||
return nil
|
||||
}
|
||||
// TODO: In WebGL, we don't need to clear the image here.
|
||||
if err := c.defaultRenderTarget.Clear(); err != nil {
|
||||
return err
|
||||
|
10
run.go
10
run.go
@ -26,6 +26,7 @@ var runContext = &struct {
|
||||
newScreenWidth int
|
||||
newScreenHeight int
|
||||
newScreenScale int
|
||||
isRunningSlowly bool
|
||||
}{}
|
||||
|
||||
// CurrentFPS returns the current number of frames per second.
|
||||
@ -33,6 +34,13 @@ func CurrentFPS() float64 {
|
||||
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.
|
||||
// f is a function which is called at every frame.
|
||||
// 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()
|
||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||
runContext.isRunningSlowly = false
|
||||
if int64(5*time.Second/60) < now-beforeForUpdate {
|
||||
beforeForUpdate = now
|
||||
} else {
|
||||
c := int((now - beforeForUpdate) * 60 / int64(time.Second))
|
||||
runContext.isRunningSlowly = c >= 2
|
||||
for i := 0; i < c; i++ {
|
||||
if err := graphicsContext.update(f); err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user