<< Chapter < Page Chapter >> Page >

General background information

As you learned in the previous module, the start method (see Listing 1 ) calls the setup method and then calls the getDelta method. Following that, it calls the gameLoop method as described below.

The property named running

The GameContainer class declares a protected boolean variable named running , which is inherited into the object of the AppGameContainer class. The descriptive comment reads "True if we're currently running the game loop."

The initial value of this variable is true and as near as I can tell, it only goes false

  • when the exit method is called,
  • when the closeRequested method is called and returns true, or
  • when some code in the game throws a SlickException .

Calling the gameLoop method

The start method in Listing 1 shows a call to the gameLoop method inside a while loop with a call to the running method as theconditional clause.

Listing 1 . The start method of the AppGameContainer class.
public void start() throws SlickException { try {setup();getDelta(); while (running()) {gameLoop(); }} finally { destroy();}if (forceExit) { System.exit(0);} }//end start

As you can see in Listing 1 , the gameLoop method is called repeatedly while the variable named running is true. Each time it returns, it is called again.

I will refer to each call to the gameLoop method as one iteration of the game loop in the discussion that follows.

The gameLoop method

The gameLoop method of the AppGameContainer class is shown in Listing 2 .

Listing 2 . The gameLoop method of the AppGameContainer class.
protected void gameLoop() throws SlickException { int delta = getDelta();if (!Display.isVisible()&&updateOnlyOnVisible) { try { Thread.sleep(100); } catch (Exception e) {}} else { try {updateAndRender(delta); } catch (SlickException e) {Log.error(e); running = false;return; }//end catch}//end else updateFPS();Display.update();if (Display.isCloseRequested()) { if (game.closeRequested()) {running = false; }//end if}//end if }//end gameLoop method

A verbal description

This is my verbal description of what happens each time the start method calls the gameLoop method.

First the gameLoop method gets the value for delta (the elapsed time since the call to the gameLoop method during the previous iteration of the game loop) .

If the display (the game window) is not visible and a property named updateOnlyOnVisible is true, the gameLoop method takes appropriate action. I will leave it as an exercise forinterested students to analyze those actions. (The program goes to sleep for 100 milliseconds.)

If the display is visible, the gameLoop method calls the updateAndRender method of the GameContainer class passing delta as a parameter. Upon return, the gameLoop method performs some housekeeping tasks and terminates.

The updateAndRender method

Listing 3 shows the beginning of the updateAndRender method of the GameContainer class. This is the code that controls calls to the update method. The code that controls calls to the render method is shown in Listing 4 later.

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