Blocking versus Non-Blocking Loops

One of the biggest challenges that game developers who work on the PC face is the myriad of possible configurations they may encounter while trying to develop applications. The game loop presented earlier executes only as quickly as the computer running it is capable. The computer tries to draw to the screen as often as it can. This practice would be perfect if everyone had the same configuration, but that is not the case. This situation forces the philosophical questions: Does the game being created require the use of all resources on the machine? Will this game still be expected to be played in five years? Depending on the answer to these questions, developers need to choose between blocking and nonblocking loop structures.

Blocking Loops

A blocking loop effectively limits the overall frame rate that can be displayed by an application by placing a timing mechanism at the end of the game loop. The timing mechanism forces a specific amount of time to pass before the game can return to the top of the loop. Regardless of the CPU speed of a given machine, the maximum number of times the game can be drawn to screen correctly is determined by the vertical sync (VSYNC) of the monitor used.

VSYNC refers to the number of vertical refreshes the monitor can process per second. There is no visual benefit to displaying more frames, and often anomalies will appear on the screen when a game tries to draw faster than the VSYNC. This method can also limit the resource consumption on a machine, but, in some cases, it shows better performance on a wider range of system configurations. This method gives you a wider potential audience for the game and could increase its acceptance and overall success from the commercial perspective.

When using a blocking loop in Java, you have several possible choices. In the following example, the decision to use the Thread.sleep mechanism was made due to a question of accuracy. The specific issue of time is discussed later in this chapter.

The following code details how to use a timing function in conjunction with a blocking loop:

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

The actual amount by which the thread is put to sleep is determined by the acceptable frame rate for the game. To determine this number, divide the delay in milliseconds by the desired frame rate. If a game is to render 30 times per second, then each frame should take 1/30 of a second to display.

Nonblocking Loops

Nonblocking loops are generally used in games considered high-performance applications. These games do not use a timing mechanism to control the frame rate; they just allow things to run as quickly as possible. This method is seen mostly in 3D games that require the extra time to make their rendering passes. This technique has one drawback in the form of running on future machines. Because nothing slows down the progress of the game loop, this method will allow a 10-GHz processor to run through the loop in the same way it did a 2-Ghz processor. The performance will be much better in terms of rendered frames, but this jump will make the game unplayable for future gamers. The choice between these methods should be based on the type of game that you are planning to implement.



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