Adjusting to the Environment

Most games are written with a target display mode in mind. Unless it is a low resolution with a color depth of 8 bpp, no assumptions can be made on the end user’s system. It is important to have a minimum set of requirements that will serve as the lower bounds for the execution of the game. If a better mode is found to be available, the application can select it.

How can an acceptable video mode be determined? The simplest method would be to compare a list of acceptable modes against the list of available modes for the device. If the system doesn’t support any of the modes, the game can be set to run in windowed mode instead of full-screen mode. First, let’s define the list of acceptable modes. Note that this list can continue through the full list of desired display modes; just add it to the existing list.

DisplayMode[] requestedDisplayModes = new DisplayMode[]  { new DisplayMode(1280, 1024,32, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(1024, 768, 32, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(800,  600, 16, DisplayMode.REFRESH_RATE_UNKNOWN), new DisplayMode(640,  480, 16, DisplayMode.REFRESH_RATE_UNKNOWN) };

Now let’s create a method to compare the video mode “wish list” with the available list of modes. Let’s call this method findRequestedMode(). This method searches through the returned values in the displayModes array trying to find a match for the height, width, and bit depth of the display. It returns the highest possible match for all three of these factors.

public static DisplayMode findRequestedMode() {     DisplayMode best = null; // loop through each of our requested modes for(int rIndex=0; rIndex #### requestedDisplayModes.length; rIndex++)           {    // loop through each of the available modes        for(int mIndex=0; mIndex #### displayModes.length; mIndex++)        {         if(displayModes[mIndex].getWidth() ==         requestedDisplayModes[rIndex].getWidth()          &&         displayModes[mIndex].getHeight()          ==requestedDisplayModes[rIndex].getHeight()              &&             displayModes[mIndex].getBitDepth() ==             requestedDisplayModes[rIndex].getBitDepth()){     // We found a resolution match          if(best==null)          {          // if the refresh rate was specified try to match that as well                if(requestedDisplayModes[rIndex].getRefreshRate() !=                DisplayMode.REFRESH_RATE_UNKNOWN)     {      if(displayModes[mIndex].getRefreshRate() $$$$=         requestedDisplayModes[rIndex].getRefreshRate())            {              best = displayModes[mIndex];              return best;            }          }       else       {        best = displayModes[mIndex];        return best;       }      }     }    }   } // no matching modes so we return null return best; }

 On the CD   When the determination has been made as to the available display modes and the resolution, the game can begin. In the source code provided for this chapter, the FullScreenFrame1 example uses the basic structures that have been discussed so far to create a full-screen window with the best resolution available on the monitor. If the system doesn’t support full-screen mode, or if it does but none of the desired modes is available, the program will run in windowed mode. It is possible to set the windowed mode by default by passing the string windowed from the command line. This code is abbreviated to conserve space. Please refer to the actual provided code for the full implementation.

public static void main(String[] args) {   DisplayMode newMode = null;     // we need to make sure the system default display can     // support full screen mode, if it can’t we will run     // in windowed mode     boolean fullScreenMode = false;     if(defaultScreenDevice.isFullScreenSupported())     {         fullScreenMode = true;         // try to get one of the modes we really want         newMode = findRequestedMode();       // if the mode doesn’t exist then go into windowed mode         if(newMode==null)           fullScreenMode = false;     }     else     System.out.println("full screen mode unsupported.");     FullScreenFrame1 myFrame = null;     if(fullScreenMode && !forceWindowedMode) myFrame = new FullScreenFrame1("FullScreenFrame1 Full Screen  Mode", newMode);     else myFrame = new FullScreenFrame1("FullScreenFrame1 Windowed Mode",  false);     myFrame.initToScreen();    } }

When the program starts, it first determines if the system on which it is running supports full-screen exclusive mode by calling the isFullScreenSupported() method on the default screen device. If this method returns true, the program prints a list of all available display modes for the device to the console, so you can get an idea of what is available. Next, the findRequestedMode() method attempts to get a display mode that matches one of the preferred modes. If the system doesn’t support one of these modes or if full-screen mode isn’t supported, the fullScreenMode variable is set to false.

If full-screen mode is supported and the system can use the desired video mode, a new FullScreenFrame1 object is constructed by passing it the new video mode. The constructor first saves the current video mode so it can be restored when the program ends.

Next, using the Frame class’s method setUndecorated, it turns off window decorations such as the title bar. If this isn’t done, the video mode will be set and the window will be a normal window positioned in the upper-left corner of the screen with the width and height of the screen. The user will be able to drag it around, just like any other window. The constructor also calls the Frame class’s method setIgnoreRepaint, passing it a value of true. This setting makes sure that the windowing system won’t send repaint events to the window. The reason for this will be explained a bit later.

Finally, the constructor calls the method setWindowListener to make sure the program gets the closing, iconified, deiconified, activated, and deactivated events. The reason these events are needed will be explained a little later in this chapter.

The constructor for the FullScreenFrame1 class follows:

public FullScreenFrame1(String title, DisplayMode mode) throws HeadlessException     {       super(title);                // this is the video mode we will run in       newDisplayMode = mode; // assume window decorations are not desired     setUndecorated(true);     // make sure the windowing system doesn’t send repaint events     setIgnoreRepaint(true);        // make sure we get the window events         setupWindowListener();    }

If full-screen mode is not supported, a new FullScreenFrame1 object will be constructed that will run in windowed mode. The main difference between this constructor and the previous one is the window is set so that it can’t be resized, and the caller has the choice of whether or not the window is undecorated.

The last action the main method performs is calling the initToScreen method, which puts the window on the screen. If the windowedMode variable is false, this method calls the methods needed to get the window on the screen in full-screen mode and set the video mode. If the windowedMode variable is true, the size of the window is set and the show method is called.

public void initToScreen()  {  pack();  if(!windowedMode)  {     // set this Frame as a full screen window      defaultScreenDevice.setFullScreenWindow(this);     // change the video mode to the one we wanted     defaultScreenDevice.setDisplayMode(newDisplayMode);  }   else   {     setSize(windowedWidth,windowedHeight);     show();   } }

If this program runs properly, the screen should be cleared and set to white. If the modes of choice are not available or windowed mode is selected, the window will look like that shown in Figure 3.3.

image from book
Figure 3.3: Results of running the FullScreenFrame1.



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