ebiten: Ensure that Update is called at least once before Draw in the first frame

Fixes #1155
This commit is contained in:
Hajime Hoshi 2020-05-15 04:04:29 +09:00
parent 837f767f45
commit f7f507e912
2 changed files with 12 additions and 0 deletions

3
run.go
View File

@ -35,6 +35,9 @@ type Game interface {
// With Draw (the recommended way), Update updates only the game logic and Draw draws the screen. // With Draw (the recommended way), Update updates only the game logic and Draw draws the screen.
// In this case, the argument screen's updated content by Update is not adopted for the actual game screen, // In this case, the argument screen's updated content by Update is not adopted for the actual game screen,
// and the screen's updated content by Draw is adopted instead. // and the screen's updated content by Draw is adopted instead.
// In the first frame, it is ensured that Update is called at least once before Draw. You can use Update
// to initialize the game state. After the first frame, Update might not be called or might be called
// multiple times for one frame. The frequency is determined by the current TPS (tick-per-second).
// //
// Without Draw (the legacy way), Update updates the game logic and also draws the screen. // Without Draw (the legacy way), Update updates the game logic and also draws the screen.
// In this case, the argument screen's updated content by Update is adopted for the actual game screen. // In this case, the argument screen's updated content by Update is adopted for the actual game screen.

View File

@ -50,6 +50,8 @@ type uiContext struct {
offscreen *Image offscreen *Image
screen *Image screen *Image
updateCalled bool
// scaleForWindow is the scale of a window. This doesn't represent the scale on fullscreen. This value works // scaleForWindow is the scale of a window. This doesn't represent the scale on fullscreen. This value works
// only on desktops. // only on desktops.
// //
@ -263,6 +265,13 @@ func (c *uiContext) update() error {
_, hasDraw := c.game.(interface{ Draw(*Image) }) _, hasDraw := c.game.(interface{ Draw(*Image) })
updateCount := clock.Update(MaxTPS()) updateCount := clock.Update(MaxTPS())
// Ensure that Update is called once before Draw so that Update can be used for initialization.
if !c.updateCalled && updateCount == 0 {
updateCount = 1
c.updateCalled = true
}
for i := 0; i < updateCount; i++ { for i := 0; i < updateCount; i++ {
c.updateOffscreen() c.updateOffscreen()