Adding Frame Rate Output #if (DEBUG) The first thing you'll notice here is that you've declared a new type of variable, the Direct3D.Font object. This object is used to render text onscreen as part of your scene. using Direct3D = Microsoft.DirectX.Direct3D; Now that the variable is declared, in your OnCreateDevice method add the following code to create a new font object: #if (DEBUG) The only parameters you need for this object are the rendering device and the font you want to render with. Using a default font is adequate for this scenario. Because the font object here is also a resource, make sure that you clean up that object in your OnDestroyDevice method. Add this code to that method now: #if (DEBUG) One thing you might notice on the font object is that it has two "special" methods for use when a device has just been reset or is about to be lost, OnDeviceReset and OnDeviceLost, respectively. You already have methods to handle when the device has just been reset and lost, so you want to call these font methods there. Add this section of code to the beginning of your OnResetDevice method to make sure that the font behaves correctly during a device reset: #if (DEBUG) You want to handle the lost case as well, so add this section of code to the OnLostDevice method: #if (DEBUG) Now that your font is created, you can look at the implementation of the UpdateFrameStats method from the framework, which is called every frame and calculates the current frame rate. See the code in Listing 5.5 from the framework. private void UpdateFrameStats() if (time - State.LastStatsUpdateTime > 1.0) State.FrameStats = string.Format(State.StaticFrameStats, Now that you know how the frame rate is calculated, the last thing you need to do is actually update your scene by rendering this text. Add this code to the end of your render method, directly before your EndScene call (but not inside the finally block). #if (DEBUG) Look at the prototype for the DrawText call; it is more or less self-explanatory. The prototype looks like this: public System.Int32 DrawText ( Microsoft.DirectX.Direct3D.Sprite sprite , Because the first parameter is null, you are specifying that you want to draw directly onscreen. Sprites are used to render 2D images onto the screen, and you use them for rendering the UI of the game later. For now, however, because you are just rendering debug information, you want it rendered directly to the screen. The second parameter is the string you will be rendering, and the third parameter is the rectangle you want to render the text into. The client rectangle is perfect here, even though you wouldn't need to use it all. The last parameter is the color you want the text to be rendered. It really is as simple as that.
|
|