All pastes #403061 Raw Edit

tobz

public text v1 · immutable
·published 2007-03-20 04:16 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNAtutorial
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager graphics;
        private ContentManager content;
        private GraphicsDevice device;
        private VertexPositionColor[] bbOne;
        private VertexPositionColor[] bbTwo;
        private VertexPositionColor[] bbThree;
        private VertexPositionColor[] bbFour;
        private VertexPositionColor[] bsOne;
        Effect effect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            base.Initialize();
            SetUpXNADevice();
            SetUpVertices();

            SetUpCamera();
        }

        private void SetUpXNADevice()
        {
            device = graphics.GraphicsDevice;

            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Riemer's XNA Tutorials -- Series 1";

            CompiledEffect compiledEffect = Effect.CompileEffectFromFile("@/../../../effects.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
            effect = new Effect(graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, null);
        }


        private void SetUpVertices()
        {
            Zone testZone = new Zone("Test Zone");

            testZone.X = 0;
            testZone.Y = 0;
            testZone.Height = 1;
            testZone.Width = 1;

            testZone.PartitionSpace();

            bbOne = GetVertices(testZone.m_root.Children[0, 0].Bounds.GetCorners());
            bbTwo = GetVertices(testZone.m_root.Children[0, 1].Bounds.GetCorners());
            bbThree = GetVertices(testZone.m_root.Children[1, 0].Bounds.GetCorners());
            bbFour = GetVertices(testZone.m_root.Children[1, 1].Bounds.GetCorners());
        }

        private VertexPositionColor[] GetVertices(Vector3[] bbCorners)
        {
            VertexPositionColor[] bbTempVertices = new VertexPositionColor[24];

            bbTempVertices[0] = new VertexPositionColor(bbCorners[0], Color.Black);
            bbTempVertices[1] = new VertexPositionColor(bbCorners[1], Color.Black);

            bbTempVertices[2] = new VertexPositionColor(bbCorners[1], Color.Black);
            bbTempVertices[3] = new VertexPositionColor(bbCorners[2], Color.Black);

            bbTempVertices[4] = new VertexPositionColor(bbCorners[2], Color.Black);
            bbTempVertices[5] = new VertexPositionColor(bbCorners[3], Color.Black);

            bbTempVertices[6] = new VertexPositionColor(bbCorners[3], Color.Black);
            bbTempVertices[7] = new VertexPositionColor(bbCorners[0], Color.Black);

            bbTempVertices[8] = new VertexPositionColor(bbCorners[4], Color.Black);
            bbTempVertices[9] = new VertexPositionColor(bbCorners[5], Color.Black);

            bbTempVertices[10] = new VertexPositionColor(bbCorners[5], Color.Black);
            bbTempVertices[11] = new VertexPositionColor(bbCorners[6], Color.Black);

            bbTempVertices[12] = new VertexPositionColor(bbCorners[6], Color.Black);
            bbTempVertices[13] = new VertexPositionColor(bbCorners[7], Color.Black);

            bbTempVertices[14] = new VertexPositionColor(bbCorners[7], Color.Black);
            bbTempVertices[15] = new VertexPositionColor(bbCorners[0], Color.Black);

            bbTempVertices[16] = new VertexPositionColor(bbCorners[0], Color.Black);
            bbTempVertices[17] = new VertexPositionColor(bbCorners[4], Color.Black);

            bbTempVertices[18] = new VertexPositionColor(bbCorners[1], Color.Black);
            bbTempVertices[19] = new VertexPositionColor(bbCorners[5], Color.Black);

            bbTempVertices[20] = new VertexPositionColor(bbCorners[2], Color.Black);
            bbTempVertices[21] = new VertexPositionColor(bbCorners[6], Color.Black);

            bbTempVertices[22] = new VertexPositionColor(bbCorners[3], Color.Black);
            bbTempVertices[23] = new VertexPositionColor(bbCorners[7], Color.Black);

            return bbTempVertices;
        }

        private void SetUpCamera()
        {
            Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(10000, 10000, 10000), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            Matrix projectionMatrix =
                Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                    Window.ClientBounds.Width/Window.ClientBounds.Height, 1.0f, 50.0f);

            effect.Parameters["xView"].SetValue(viewMatrix);
            effect.Parameters["xProjection"].SetValue(projectionMatrix);
            effect.Parameters["xWorld"].SetValue(Matrix.Identity);
        }


        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
            }
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                content.Unload();
            }
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            device.RenderState.CullMode = CullMode.None;

            device.Clear(Color.White);

            effect.CurrentTechnique = effect.Techniques["Pretransformed"];
            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
                device.DrawUserPrimitives(PrimitiveType.LineStrip, bbOne, 0, 12);

                pass.End();
            }
            effect.End();

            base.Draw(gameTime);
        }
    }
}