Rendering the Game


Now you need to update the rendering code that you wrote a few chapters ago to handle rendering both the user interface screens and the game itself. Use the code from Listing 17.7 to replace the current rendering method you have defined.

Listing 17.7. Rendering the Scene
 public void OnFrameRender(Device device, double appTime, float elapsedTime) {     bool beginSceneCalled = false;     // Clear the render target and the zbuffer     device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0, 1.0f, 0);     try     {         device.BeginScene();         beginSceneCalled = true;         if (isMainMenuShowing)         {             mainScreen.Draw(this);         }         else         {             // Set the white material, it will be used for the             // majority of the rendering             device.Material = WhiteMaterial;             // Render the game's scene             RenderGameScene(device);         }         #if (DEBUG)         // Show debug stats (in debug mode)         debugFont.DrawText(null, sampleFramework.FrameStats,             new System.Drawing.Rectangle(2,0,0,0),             DrawTextFormat.NoClip, unchecked((int)0xffffff00));         debugFont.DrawText(null, sampleFramework.DeviceStats,             new System.Drawing.Rectangle(2,15,0,0),             DrawTextFormat.NoClip, unchecked((int)0xffffff00));         // Draw the number of vertices and face information         debugFont.DrawText(null, string.Format             ("\r\nNumber Of Vertices Rendered: {0}\r\nNumber of               Faces Rendered: {1}"             ,numberVerts, numberFaces), new System.Drawing.Rectangle(2,30,0,0),             DrawTextFormat.NoClip, unchecked((int)0xffffffff));         #endif       }       finally       {           if (beginSceneCalled)           device.EndScene();     } } /// <summary> /// Renders the game scene, including the level, the tanks, etc. /// </summary> private void RenderGameScene(Device device) {     // Render the sky box first     worldSky.Draw(this, device);     // Now, the level     worldLevel.Draw(this, device);     // Track the local player     sceneCamera.Track(localPlayer);     // Draw the player     localPlayer.Draw(this, sceneCamera);     // Is there a remote player to draw?     if (remotePlayer.IsPlayerConnected)     {     remotePlayer.Draw(this, sceneCamera);     }     // Make sure only one thread has access to this collection     lock(allBullets)     {         // Draw all of the bullets currently onscreen         Bullet.SetBulletRenderStates(this);         foreach(Bullet b in allBullets)         {             b.Draw(this, sceneCamera);         }         Bullet.EndBulletRenderStates(this);     }     // Now render the player names     localPlayer.DrawPlayerName(this);     // Is there a remote player to draw?     if (remotePlayer.IsPlayerConnected)     {         remotePlayer.DrawPlayerName(this);     }     particleEffects.Draw(this); } 

The main rendering method is quite similar to the first one that you implemented earlier for this game, but it actually draws the user interface or the game, depending on what should be rendered currently.

See Figure 17.1 for a shot of the game, finally finished. Congratulations!

Figure 17.1. Tankers: finished at last.




Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

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