Hopefully there is not much that can go wrong in this chapter. This chapter only touched on two major issues: unit testing and sprites. You also might have problems getting the game to run on your Xbox 360, but I already wrote down a bunch of tips and tricks on this issue at the end of Chapter 1; check them out if you still have problems setting up your Xbox 360.
Another problem most of you will probably not have, but for the few readers that are not experienced with exceptions, especially with the ones from XNA, is to understand why an exception is thrown and what exactly has to be done to fix it. Always read the exception very closely and try to review the full stack trace and check out if there are inner exceptions with more detailed information.
Similar to Managed DirectX, sometimes you get exceptions from the underlying DirectX framework, which can be confusing sometimes. Try to search for help on specific error numbers on the Internet and MSDN. In those cases the context is usually more important than the error message itself. For example, if compiling a shader fails you might get strange error codes, but instead of trying to find out what these error codes mean make sure the shader file exists, it can be compiled, and your hardware can handle it. A good tool to check if shader files are valid is FX Composer from Nvidia.
If you run into trouble using unit testing please also read the troubleshooting section of the next chapter, because it talks a lot about unit testing too. Basically always remember that the kind of unit tests you used here behave like normal programs; treat them the same way. Debug them and step through them like you would with the final application.
Last but not least, some tips about using sprites in XNA:
If you render a lot of sprites on top of each other there might be many issues in XNA showing random sorting order, problems when using different textures, and often the background sprite might kill everything in front of it. As you can see in XNA Pong you rendered the background in an extra spritebatch call. This way it was separated and cannot interfere with any of the sprites in the game or menu.
Try to render all sprites at once, especially if you can sort them by the blend mode. It is a lot faster if you render all sprites at once in one big call to spritebatch. Check out the DrawSprites method as an example. If you use different blend modes like additive for light effects and alpha blending for all other sprites, try to render in two passes, first the alpha blended stuff, then the additive lights on top. Performance can improve to 200%–300% if you do that.
Think about resolutions and how to handle different widths and heights; you can get the used resolution in the Initialize method with the following two lines:
width = graphics.GraphicsDevice.Viewport.Width; height = graphics.GraphicsDevice.Viewport.Height;
You cannot force a specific resolution; it will not work on the Xbox 360 at all and on Windows it is only used as a suggestion. For example, if you want to test the 720p 16:9 resolution (1280×720) on your PC, you must have a monitor that supports this resolution either in windowed mode or in fullscreen mode depending on what you are testing. Add the following lines to the constructor of your main game class (PongGame in this Chapter) to suggest the 720p resolution. You can check if that worked in the Initialize method by checking the width and height as described earlier:
graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720;
There is no font support in XNA - to show text you can either create textures like in this game with all the text already written down or write your own font support like in Chapter 5. For both solutions you will use sprites to render text or just a letter at a time on the screen.
Sprites are not the only way to display 2D data in XNA; you can write your own shaders and even do much more advanced effects than is possible with the sprite class. As an example check out Chapter 5 and see how 2D lines are rendered with help of shaders.