<< Chapter < Page Chapter >> Page >

What is delta?

An int parameter named delta is received by the update method shown in Listing 6 . (The update method s a concrete version of the method having the same signature that is declared in the Game interface.)

According to the documentation for the update method in the Game interface, delta is

"The amount of time that has passed since last update in milliseconds"

Having that time available can be valuable in some game programs. For example, you might like for one of the actors to light a fuse on a bomb and havethat bomb detonate some given number of milliseconds later. In that case, the program would need real time information to know when to detonate the bomb.

Listing 5 shows the source code for the getDelta method.

Listing 5 . The getDelta method of the GameContainer class.
protected int getDelta() { long time = getTime();int delta = (int) (time - lastFrame); lastFrame = time;return delta;}//end getDelta method

Without getting into the details, the method named getTime that is called in Listing 5 returns the amount of time, (with a resolution of one millisecond) , that has elapsed since a historical point in time before the game started running.

The GameContainer class contains a protected instance variable of type long named lastFrame that is used to store a time value.

The code in Listing 5

  • subtracts the time value stored in lastFrame from the current time,
  • converts the time difference to type int , saving it in delta , and
  • stores the current time in lastFrame .

The difference between the two time values represents a time interval and that difference is returned as type int .

Various methods in the AppGameContainer and GameContainer classes call the getDelta method in such a way that the value of delta represents the time required to update and renderone frame when the program is running. (There are some other options as well that I may discuss in a future module.)

When the method named update is called in Listing 6 , the incoming parameter named delta contains the number of milliseconds that have elapsed since the last time that the update method was called.

When the method named getDelta is called in Listing 2 , the return value is discarded. This suggests that the call to the getDelta method in Listing 2 is made simply to cause the variable named lastFrame to be initialized with time that the start method was called.

The gameLoop method of the AppGameContainer class

That leaves us with one more method call from Listing 2 that we need to examine -- gameLoop . I anticipate that will be a fairly long discussion, so I am going to defer that discussion until the nextmodule.

Run the program

As explained earlier, the skeleton code in Listing 6 is different from the skeleton code that I presented in earlier modules.Therefore, I encourage you to copy the code from Listing 6 . Compile the code and execute it,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

The main purpose of this module was to analyze the behavior of the Slick2D gameengine when you call the start method to cause a Slick2D game program to start running.

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