using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
using System.IO;
using DarkWynter.Stream.PhysicsGpu;
using Nuclex;
namespace PlayerTrace
{
///
/// This is the main type for your game
///
public class PlayerTraceControl : Nuclex.GameControl
{
public struct Fonts
{
public static SpriteFont Arial;
public static SpriteFont ComicSans;
public static SpriteFont ComicSansSmall;
}
SpriteBatch spriteBatch;
public static ContentManager content;
/// Default Vertex- & PixelShader for the graphics card
private Effect effect;
public List faces;
public List traces;
public Vector3 cameraPosition = Vector3.Zero;
public GpuProcessor gpuProcessor;
public List variables;
public PlayerTraceControl()
{
this.Focus();
this.IsFixedTimeStep = false;
//Graphics = new Nuclex.GraphicsDeviceManager(this);
content = new ContentManager(Services);
content.RootDirectory = "Content";
// Check all available adapters on the system
for (int i = 0; i < GraphicsAdapter.Adapters.Count; i++)
{
// Get the capabilities of the hardware device
GraphicsDeviceCapabilities caps = GraphicsAdapter.Adapters[i].GetCapabilities(DeviceType.Hardware);
if (caps.MaxPixelShaderProfile < ShaderProfile.PS_3_0)
{
System.Diagnostics.Debug.WriteLine("This adapter does not support Shader Model 3.0.");
}
else
{
System.Diagnostics.Debug.WriteLine("Shader Model 3.0 supported.");
}
}
}
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
protected override void Initialize()
{
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load Fonts and HUD
Fonts.Arial = content.Load("_fonts/Arial");
Fonts.ComicSans = content.Load("_fonts/ComicSansMS");
Fonts.ComicSansSmall = content.Load("_fonts/ComicSansSmall");
// Create effect containing vertex and pixel shaders used to draw
this.effect = content.Load("GPGPU_PlayerTrace");
LoadObjectData("traces/l1s3t-ssps.xml", effect);
traces = new List();
LoadTraceData("traces/trace1.data");
LoadTraceData("traces/trace2.data");
LoadTraceData("traces/trace3.data");
gpuProcessor = new GpuProcessor(GraphicsDevice);
variables = new List();
for (int i = 0; i < faces.Count; i++)
{
variables.Add(faces[i].gpuTexture);
}
}
public void LoadObjectData(string fileName, Effect effect)
{
faces = new List();
try
{
XmlDocument reader = new XmlDocument();
reader.Load(fileName);
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode levelNode in allNodes)
{
if (levelNode.Name == "ssps")
{
foreach (XmlNode posNode in levelNode.ChildNodes)
{
if (posNode.Name == "positive")
{
foreach (XmlNode objNode in posNode.ChildNodes)
{
if (objNode.Name == "object")
{
foreach (XmlNode faceNode in objNode.ChildNodes)
{
if (faceNode.Name == "face")
{
faces.Add(new Face(faceNode, graphics.GraphicsDevice, content, effect));
}
}
}
}
}
}
}
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
throw new Exception("Error Loading Level");
}
}
public void LoadTraceData(string fileName)
{
StreamReader file = new StreamReader(Directory.GetCurrentDirectory() + "/" + fileName);
string line;
while ((line = file.ReadLine()) != null)
{
if (line.IndexOf("\t") != -1)
{
traces.Add(new Trace(line, graphics.GraphicsDevice, content));
}
else
{
Console.WriteLine(line);
}
}
file.Close();
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
Vector3 cameraMotion = Vector3.Zero;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
cameraMotion.Y -= 100;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
cameraMotion.Y += 100;
}
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
cameraMotion.X -= 100;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
cameraMotion.X += 100;
}
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
cameraMotion.Z -= 1;
}
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
cameraMotion.Z += 1;
}
cameraPosition += cameraMotion;
base.Update(gameTime);
}
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
this.effect.Parameters["World"].SetValue(Matrix.Identity);
this.effect.Parameters["View"].SetValue(
Matrix.CreateLookAt(cameraPosition, faces[0].vert0, new Vector3(0.0f, 0.0f, 1.0f))
);
this.effect.Parameters["Projection"].SetValue(
Matrix.CreatePerspectiveFieldOfView(
(float)Math.PI / 4,
this.graphics.GraphicsDevice.Viewport.Width /
this.graphics.GraphicsDevice.Viewport.Height,
0.3f, 100000f)
);
this.graphics.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
this.graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
this.graphics.GraphicsDevice.Clear(Color.DarkSlateGray);
for (int i = 0; i < faces.Count; i++)
{
faces[i].Draw(graphics.GraphicsDevice, effect);
}
for (int i = 0; i < traces.Count; i++)
{
traces[i].Draw(graphics.GraphicsDevice, effect);
}
gpuProcessor.ExecuteRTT(GraphicsDevice, variables, effect);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.DrawString(Fonts.Arial, "Camera Position: " + cameraPosition.ToString(),
new Vector2(50, 50), Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}
}