diff --git a/run.go b/run.go index 4efbafc12..d18cdd36a 100644 --- a/run.go +++ b/run.go @@ -35,6 +35,9 @@ type Game interface { // 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, // 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. // In this case, the argument screen's updated content by Update is adopted for the actual game screen. diff --git a/uicontext.go b/uicontext.go index 3550b83ea..8ce01c608 100644 --- a/uicontext.go +++ b/uicontext.go @@ -50,6 +50,8 @@ type uiContext struct { offscreen *Image screen *Image + updateCalled bool + // scaleForWindow is the scale of a window. This doesn't represent the scale on fullscreen. This value works // only on desktops. // @@ -263,6 +265,13 @@ func (c *uiContext) update() error { _, hasDraw := c.game.(interface{ Draw(*Image) }) 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++ { c.updateOffscreen()