Challenge: Write a Game Component


It is now your turn to write some code and test it on both the Windows and Xbox 360 platforms. To make it easy you just have to write a simple “frames per second” display in the upper-left corner of the screen. Remember the safe region on the Xbox 360; it would make sense to have different code for the Xbox 360 and the PC for the location of the frame per second number. Take a look at the following code to see how this can be done:

  #if XBOX360     Point fpsPos = new Point(2 + width / 15, 2 + height / 15); #else     Point fpsPos = new Point(2, 2); #endif 

You should plug the game component in the same way as the SimpleCamera class and you should put it in the game namespace too. For drawing the frames per second counter, use the last line of the BaseGame.Draw method before base.Draw is called. Then test a couple of unit tests (see the program class) and see if the frames per seconds counter is displayed correctly on Windows and the Xbox 360 if you have one.

To calculate the frames per seconds each frame you could use a frame counter variable and then find out how long the application runs and divide these two values to get the number of frames you have each second in average. A better way would be just to count the frames every second and then start over at 0 every time a second has passed.

If you are unsure about your implementation, skip a few chapters ahead and see the code that is used in the games there (they all have fps counters and the code for it in them). For example, the Rocket Commander game always uses an fps display in debug mode and for all unit tests to quickly help you to see any performance drops if you do anything wrong. For just the component code take a look at the FpsCounter class from the source code for this chapter.




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

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