Guest User

TestGame for http://pastebin.com/Q1WQnFTC

a guest
May 31st, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace CubeRender
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.  
  18.         Matrix projection, view;
  19.         BoundingFrustum frustum;
  20.         BasicEffect effect;
  21.  
  22.         List<RenderObject> cubes = new List<RenderObject>();
  23.  
  24.         int cubeCount = 0;
  25.         bool frustumCull = false;
  26.         bool defaultCube = false;
  27.  
  28.         private TimeSpan FpsElapsedTime;
  29.         private float FpsCounter = 0, FpsCurrent = 0;
  30.         private float? FpsMin = null, FpsMax = null;
  31.  
  32.         public Game1()
  33.         {
  34.             graphics = new GraphicsDeviceManager(this);
  35.             Content.RootDirectory = "Content";
  36.         }
  37.  
  38.         protected override void Initialize()
  39.         {
  40.             base.Initialize();
  41.         }
  42.  
  43.         protected override void LoadContent()
  44.         {
  45.             projection =
  46.               Matrix.CreatePerspectiveFieldOfView
  47.               (
  48.                   MathHelper.ToRadians(45f),
  49.                   GraphicsDevice.Viewport.AspectRatio,
  50.                   0.01f,
  51.                   10000f
  52.               );
  53.  
  54.             view =
  55.                     Matrix.CreateRotationY(MathHelper.ToRadians(-45f)) *
  56.                     Matrix.CreateRotationX(MathHelper.ToRadians(-45f)) *
  57.                     Matrix.CreateTranslation(Vector3.Zero) *
  58.                     Matrix.CreateLookAt(new Vector3(0, 0, -10f), Vector3.Zero, Vector3.Up);
  59.  
  60.             frustum = new BoundingFrustum(view * projection);
  61.  
  62.             effect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, TextureEnabled = false };
  63.  
  64.             defaultCube = true;
  65.             bool white = false;
  66.  
  67.             for (int x = -50; x < 50; x++)
  68.             {
  69.                 for (int z = -50; z < 50; z++)
  70.                 {
  71.                     // change here if you want to try different cubes
  72.  
  73.                     if (defaultCube)
  74.                     {
  75.                         if (white)
  76.                         {
  77.                             cubes.Add(new Cube2(GraphicsDevice, Color.White) { Position = new Vector3(x, 0, z) });
  78.                         }
  79.                         else
  80.                         {
  81.                             cubes.Add(new Cube2(GraphicsDevice, Color.Black) { Position = new Vector3(x, 0, z) });
  82.                         }
  83.  
  84.                         white = !white;
  85.                     }
  86.                     else
  87.                     {
  88.                         cubes.Add(new Cube(GraphicsDevice) { Position = new Vector3(x, 0, z) });
  89.                     }
  90.                 }
  91.             }
  92.  
  93.         }
  94.  
  95.         protected override void UnloadContent()
  96.         {
  97.             effect.Dispose();
  98.  
  99.             foreach (RenderObject cube in cubes)
  100.                 cube.Dispose();
  101.         }
  102.  
  103.         protected override void Update(GameTime gameTime)
  104.         {
  105.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  106.                 this.Exit();
  107.  
  108.             FpsElapsedTime += gameTime.ElapsedGameTime;
  109.  
  110.             if (this.FpsElapsedTime > TimeSpan.FromSeconds(1))
  111.             {
  112.                 this.FpsElapsedTime -= TimeSpan.FromSeconds(1);
  113.                 FpsCurrent = FpsCounter;
  114.                 FpsCounter = 0;
  115.  
  116.                 if (gameTime.TotalGameTime.TotalSeconds > 5)
  117.                 {
  118.                     if (FpsMax == null)
  119.                         FpsMax = FpsCurrent;
  120.                     else if (FpsCurrent > FpsMax.Value)
  121.                         FpsMax = FpsCurrent;
  122.  
  123.                     if (FpsMin == null)
  124.                         FpsMin = FpsCurrent;
  125.                     else if (FpsCurrent < FpsMin.Value)
  126.                         FpsMin = FpsCurrent;
  127.                 }
  128.  
  129.                 this.Window.Title = "Press space to frustum cull on or off! FPS: " + FpsCurrent + ", Cubes Drawn: " + cubeCount + " / " + cubes.Count;
  130.             }
  131.  
  132.             if (Keyboard.GetState().IsKeyDown(Keys.Space))
  133.                 frustumCull = false;
  134.             else
  135.                 frustumCull = true;
  136.  
  137.             base.Update(gameTime);
  138.         }
  139.  
  140.         protected override void Draw(GameTime gameTime)
  141.         {
  142.  
  143.             FpsCounter++;
  144.  
  145.             GraphicsDevice.Clear(Color.CornflowerBlue);
  146.  
  147.             effect.View = view;
  148.             effect.Projection = projection;
  149.  
  150.             cubeCount = 0;
  151.  
  152.             if (frustumCull)
  153.             {
  154.                 foreach (RenderObject cube in cubes.Where(c => frustum.Contains(c.BoundingBox) != ContainmentType.Disjoint))
  155.                 {
  156.                     cube.Draw(GraphicsDevice, effect);
  157.                     cubeCount++;
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 foreach (RenderObject cube in cubes)
  163.                 {
  164.                     cube.Draw(GraphicsDevice, effect);
  165.                     cubeCount++;
  166.                 }
  167.             }
  168.  
  169.             base.Draw(gameTime);
  170.         }
  171.     }
  172. }
Add Comment
Please, Sign In to add comment