<< Chapter < Page | Chapter >> Page > |
Possible outcomes
This algorithm results in the following possible outcomes regarding calls to the update method during each iteration of the game loop prior to calling the render method:
Analysis of the outcomes
Item 1 represents a situation where you don't want to execute update code for values of delta that are below a certain threshold and you prefer to execute update code less frequently using accumulated values of delta instead.
Item 2 represents a situation where the paused property has been set to true and no updates should be performed. (This situation is indicated by a delta value of zero, which can be tested by code in the update method.)
Item 3 represents a situation where you are willing to execute the code in the update method once during each iteration of the game loop using the incoming value ofdelta.
Item 4 represents a situation where you need to execute the code in the update method two or more times in succession during each iteration of the game loopwith the total value of delta being divided into smaller values.
The situation regarding calls to the render method, as shown in Listing 4 , is much less complicated.
Listing 4 . The remainder of the updateAndRender method of the GameContainer class. |
---|
if (hasFocus() || getAlwaysRender()) {
if (clearEachFrame) {GL.glClear(SGL.GL_COLOR_BUFFER_BIT |
SGL.GL_DEPTH_BUFFER_BIT);}//end ifGL.glLoadIdentity();graphics.resetFont();
graphics.resetLineWidth();graphics.setAntiAlias(false);
try {game.render(this, graphics);
} catch (Throwable e) {
Log.error(e);throw new SlickException(
"Game.render() failure - check the game code.");}//end catch
graphics.resetTransform();if (showFPS) {defaultFont.drawString(10,10,"FPS: "+recordedFPS);
}//end ifGL.flush();}//end if on hasFocusif (targetFPS != -1) {
Display.sync(targetFPS);}//end if
}//end method updateAndRender |
One call per iteration of the game loop
Although there is some tedious housekeeping code in Listing 4 , one call to the render method is made during each iteration of the game loop provided that the gamewindow has the focus or a property named alwaysRender is true.
(The default value for alwaysRender is false, but a public method is provided to set its value to true or false.)
Although the Slick2D library can be used in a variety of ways to create game programs, the overall structure for one approach looks something like the following.
Notification Switch
Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?