Advertisement
Guest User

Untitled

a guest
Jun 18th, 2012
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.55 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 WindowsGame1
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         Rect _rect;
  21.  
  22.         public Game1()
  23.         {
  24.             graphics = new GraphicsDeviceManager(this);
  25.             Content.RootDirectory = "Content";
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Allows the game to perform any initialization it needs to before starting to run.
  30.         /// This is where it can query for any required services and load any non-graphic
  31.         /// related content.  Calling base.Initialize will enumerate through any components
  32.         /// and initialize them as well.
  33.         /// </summary>
  34.         protected override void Initialize()
  35.         {
  36.             // TODO: Add your initialization logic here
  37.  
  38.             base.Initialize();
  39.  
  40.             _rect = new Rect(GraphicsDevice, Vector3.Zero, new[] { Color.Red, Color.Green, Color.Blue, Color.Yellow }, 50f);
  41.         }
  42.  
  43.         /// <summary>
  44.         /// LoadContent will be called once per game and is the place to load
  45.         /// all of your content.
  46.         /// </summary>
  47.         protected override void LoadContent()
  48.         {
  49.             // TODO: use this.Content to load your game content here
  50.         }
  51.  
  52.         /// <summary>
  53.         /// UnloadContent will be called once per game and is the place to unload
  54.         /// all content.
  55.         /// </summary>
  56.         protected override void UnloadContent()
  57.         {
  58.             // TODO: Unload any non ContentManager content here
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Allows the game to run logic such as updating the world,
  63.         /// checking for collisions, gathering input, and playing audio.
  64.         /// </summary>
  65.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  66.         protected override void Update(GameTime gameTime)
  67.         {
  68.             // Allows the game to exit
  69.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  70.                 this.Exit();
  71.  
  72.             // TODO: Add your update logic here
  73.  
  74.             base.Update(gameTime);
  75.         }
  76.  
  77.         /// <summary>
  78.         /// This is called when the game should draw itself.
  79.         /// </summary>
  80.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  81.         protected override void Draw(GameTime gameTime)
  82.         {
  83.             GraphicsDevice.Clear(Color.CornflowerBlue);
  84.  
  85.             // TODO: Add your drawing code here
  86.             _rect.Draw();
  87.  
  88.             base.Draw(gameTime);
  89.         }
  90.     }
  91.  
  92.     public abstract class Shape
  93.     {
  94.         public Vector3 Center;
  95.         public Color[] Colors;
  96.         public bool SetColorsOnUpdate;
  97.  
  98.         public virtual Rectangle BoundingBox { get; set; }
  99.         public float Radius { get { return mRadius; } }
  100.  
  101.         public GraphicsDevice Device { get; private set; }
  102.  
  103.         protected VertexBuffer mVertexBuf;
  104.         protected IndexBuffer mIndexBuf;
  105.         protected float mRadius;
  106.         protected BasicEffect mShader;
  107.  
  108.         public Shape(GraphicsDevice device, Vector3 center, Color[] colors, int numVertices, float radius)
  109.         {
  110.             Device = device;
  111.             Colors = colors;
  112.             Center = center;
  113.             mVertexBuf = new VertexBuffer(device, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly);
  114.             mIndexBuf = new IndexBuffer(device, IndexElementSize.SixteenBits, 6, BufferUsage.WriteOnly);
  115.             SetColorsOnUpdate = true;
  116.             mShader = new BasicEffect(device);
  117.             mRadius = radius;
  118.         }
  119.  
  120.         public abstract void Update();
  121.         public abstract void Draw();
  122.     }
  123.  
  124.     public class Rect : Shape
  125.     {
  126.         public override Rectangle BoundingBox
  127.         {
  128.             get
  129.             {
  130.                 int x = (int)Center.X, y = (int)Center.Y;
  131.                 int diameter = (int)mRadius * 2;
  132.  
  133.                 return new Rectangle(
  134.                     x, y,
  135.                     x + diameter,
  136.                     y + diameter
  137.                 );
  138.             }
  139.         }
  140.  
  141.         const float TestRectZCoordinate = 0;
  142.         const int NumVertices = 4;
  143.  
  144.         public Rect(GraphicsDevice device, Vector3 center, Color[] colors, float radius)
  145.             : base(device, center, colors, NumVertices, radius)
  146.         {
  147.             if (colors.Length < NumVertices)
  148.                 throw new IndexOutOfRangeException(string.Format("Color array passed to Rect constructor MUST have an element index size of 4. Current length passed is {0}", colors.Length));
  149.  
  150.             mShader.VertexColorEnabled = true;
  151.  
  152.             mVertexBuf.SetData<VertexPositionColor>(
  153.                     new VertexPositionColor[]
  154.                 {
  155.                     new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]),
  156.                     new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]),
  157.                     new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]),
  158.                     new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3])
  159.                 });
  160.  
  161.             mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 0, 2, 3 });
  162.         }
  163.  
  164.         public override void Update()
  165.         {
  166.             //TODO
  167.         }
  168.  
  169.         public override void Draw()
  170.         {
  171.             var halfWidth = Device.Viewport.Width / 2f;
  172.             var halfHeight = Device.Viewport.Height / 2f;
  173.            
  174.             // mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
  175.             mShader.Projection = Matrix.CreateOrthographicOffCenter(-halfWidth, halfWidth, halfHeight, -halfHeight, 0, 1);
  176.  
  177.             Device.SetVertexBuffer(mVertexBuf);
  178.             Device.Indices = mIndexBuf;
  179.             foreach (var pass in mShader.CurrentTechnique.Passes)
  180.             {
  181.                 pass.Apply();
  182.  
  183.                 Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 2);
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement