<< 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:

  1. No call at all .
  2. One call with a delta value of zero .
  3. One call with a non-zero value for delta .
  4. Multiple calls, each with a non-zero value for delta .

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.

Calls to the render method

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.)

Overall structure of a game program

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.

  • Define a class with a main method.
  • Cause the main method to instantiate an object of the BasicGame class.
  • Cause the main method to instantiate an object of the AppGameContainer class, passing the BasicGame object's reference as a parameter to the constructor for AppGameContainer .
  • Cause the main method to call the start method on the AppGameContainer object.
  • Override the init method inherited from the Basic game class to initialize the state of your game. This method will be called onceby default before the game loop begins.
  • Override the update method to update the state of your game during each iteration of the game loop. Use the incoming value of delta fortiming control. The update method will be called none, one, or more times during each iteration of the game loop as described earlier.
  • Override the render method to draw the state of your game in the game window once during each iteration of the game loop.
  • Optionally override the inherited getTitle and closeRequested methods if needed.
  • Using the Slick2D javadocs and the Java javadocs (or a later version) as a guide, write code into your constructor, your main method, and your overridden methods to tailor the behavior of your game program to your liking.

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