<< Chapter < Page Chapter >> Page >
Listing 3 . Beginning of the updateAndRender method of the GameContainer class.
protected void updateAndRender(int delta) throws SlickException {if (smoothDeltas) { if (getFPS() != 0) {delta = 1000 / getFPS(); }//end if}//end ifinput.poll(width, height);Music.poll(delta); if (!paused) {storedDelta += delta;if (storedDelta>= minimumLogicInterval) { try {if (maximumLogicInterval != 0) { long cycles =storedDelta / maximumLogicInterval; for (int i=0;i<cycles;i++) { game.update(this,(int)maximumLogicInterval);}//end for loopint remainder = (int)(delta % maximumLogicInterval);if (remainder>minimumLogicInterval) { game.update(this,(int)(delta % maximumLogicInterval)); storedDelta = 0;} else { storedDelta = remainder;}//end else } else {game.update(this, (int) storedDelta); storedDelta = 0;}//end else} catch (Throwable e) { Log.error(e);throw new SlickException( "Game.update() failure - check the game code.");}//end catch }//end if on minimumLogicInterval} else { game.update(this, 0);}//end else

Another verbal description

This is my verbal description of what happens when the gameLoop method calls the updateAndRender method.

(I will not discuss those actions that represent communication with the hardware via the Lightweight Java Game Library (lwjgl) as well as a fewother housekeeping actions) .

The updateAndRender method begins by selecting between normal delta and "smooth deltas."

(Smooth delta values essentially represent a moving average of individual delta values computed in asomewhat roundabout way.)

Calling the update and render methods

The update method of the Game object will be called none, one, ormore times during each iteration of the game loop on the basis of the contents of two variables named minimumLogicInterval and maximumLogicInterval .

The default value for minimumLogicInterval is 1. The default value for maximumLogicInterval is 0. Methods are provided by which you can change the values of these two variables.

The render method of the Game object will be called only once during each iteration of the game loop following the call or calls to the update method.

Calls to the update method

If the paused property is true, the update method is called once passing a value of zero for delta. Otherwise, the value ofdelta is added to the contents of a variable named storedDelta for the purpose of accumulating individual delta values.

Then the method enters a somewhat complex logic process, which I will describe as follows:

  • If storedDelta is less than minimumLogicInterval , don't call update during this iteration of the game loop.
  • If storedDelta is greater than or equal to minimumLogicInterval and maximumLogicInterval has a value of 0, call the update method once passing storedDelta as a parameter. Then set storedDelta to zero to set the accumulated value back to 0.
  • If storedDelta is greater than minimumLogicInterval and maximumLogicInterval is not equal to zero, call the update method several times in succession (if needed) during this iteration of the game loop, passing a value for delta during each call that is less than or equalto storedDelta . Continue this process until the sum of the delta values passed in the method calls equals storedDelta .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask