The Role of Timing in Games

Timing in games is a critical and important mechanism. The illusion of movement and interaction with a game world is founded on the ability to rapidly progress through the game loop and display the outcome of those interactions to the player. Games are atypical applications and are often intrusive into the various platforms on which they exist. These efforts require accurate timers that can report things such as frame rate or the length of time an enemy has exhibited a particular AI behavior. Without a reliable timing mechanism, developing games is a particularly difficult task. Some games often go to the actual hardware to get the performance they need. Lack of a standardized high-resolution timing routine in Java has been an issue for quite some time and has caused game developers to seek new and more accurate timing mechanisms in games. Some of these methods actually cause the portability of Java to be broken to some degree by binding Java functions to the local system; you can take advantage of more accurate system timers. This method is discussed more fully in Chapter 11: “Java Native Interface.”

Programmers who cannot allow for system-level timing must rely on the old tried-and-true techniques that have been developed over the years. One of these options is to use the method System.currentTimeMillis(). This method returns a value in milliseconds but has a resolution of approximately 50–55 milliseconds on Windows machines. This resolution is decent, but it may cause certain elements of a game to act jerky or prevent them from responding to updates quickly. This method is useful but may quickly show its limitations. A quick example of using currentTimeMillis follows:

long begintime = System.currentTimeMillis(); //time something out in between long endtime = System.currentTimeMillis() – begintime;

Another useful method is to tell the executing thread to sleep using the method of the same name. This method can have a resolution of closer to 5 milliseconds and is much more precise than the currentTimeMillis(). A call to sleep() tells the executing thread to stop for a specified number of milliseconds. With a resolution of 5 milliseconds, this method is a decent but an admittedly sloppy way to control the time for a game. An example of how to use the Thread.sleep() method from earlier in this chapter follows:

public void gameMain() { System.out.println("The Game Main is executing");  while(playerlives>=0)  {    playerlives-=1;    try    {     Thread.sleep(60);    }    catch(InterruptedException e)    {     System.out.println("Someone Interrupted my Thread!");     }  } }

One final and currently experimental method of tracking time is using a little documented class on the Java platform as of the time of this writing. Fortunately, this class, or one close to it in performance, is planned for the next major release of the JDK and will be implemented as the nanoTimer class, according to the Java Specification Request (JSR) 166. Unless JDK 1.5 has been released by the time you are reading this book (which means a second edition must be written), this class is the most precise way to obtain a high-resolution time. Each version is implemented for the various platforms for which Java is available. Some tests have yielded a 1-millisecond resolution. Remember that this code should be considered experimental and that it should not be relied upon completely until the finalized version of the class is included as part of the standard JDK. If you use this code, be sure to test for its availability before distributing mission-critical code that uses the following code segment:

import sun.misc.Perf;   public class Timer  {   Perf Timer;   long freq;     public Timer()    {    Timer = Perf.getPerf();    freq = hiResTimer.highResFrequency();   }     public long currentTimeMillis()    {    return Timer.highResCounter() * 1000 / freq;   } }

This timer class will allow for precise resolutions for timing of many operations in games. You should ensure you have it available on any systems with which you use it.

Timing is used everywhere in game programming. Some of the more common uses in games include:

  • Updating the state of the game by the duration of the previous frame

  • Locking the frame rate of a renderer

  • Maintaining and evaluating proper transition of AI states

  • Correctly identifying times to change frames in an animation

  • Testing for the proper amount of time to pass before adding additional input from a device

These are just a few uses of timing in games that require accurate timer controls to deliver consistent performance. Make sure that you know how accurate the timer on your system is before you depend on it.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net