Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- namespace WindowsGame1
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- Rect _rect;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// 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.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- base.Initialize();
- _rect = new Rect(GraphicsDevice, Vector3.Zero, new[] { Color.Red, Color.Green, Color.Blue, Color.Yellow }, 50f);
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- // TODO: Add your update logic here
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- _rect.Draw();
- base.Draw(gameTime);
- }
- }
- public abstract class Shape
- {
- public Vector3 Center;
- public Color[] Colors;
- public bool SetColorsOnUpdate;
- public virtual Rectangle BoundingBox { get; set; }
- public float Radius { get { return mRadius; } }
- public GraphicsDevice Device { get; private set; }
- protected VertexBuffer mVertexBuf;
- protected IndexBuffer mIndexBuf;
- protected float mRadius;
- protected BasicEffect mShader;
- public Shape(GraphicsDevice device, Vector3 center, Color[] colors, int numVertices, float radius)
- {
- Device = device;
- Colors = colors;
- Center = center;
- mVertexBuf = new VertexBuffer(device, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly);
- mIndexBuf = new IndexBuffer(device, IndexElementSize.SixteenBits, 6, BufferUsage.WriteOnly);
- SetColorsOnUpdate = true;
- mShader = new BasicEffect(device);
- mRadius = radius;
- }
- public abstract void Update();
- public abstract void Draw();
- }
- public class Rect : Shape
- {
- public override Rectangle BoundingBox
- {
- get
- {
- int x = (int)Center.X, y = (int)Center.Y;
- int diameter = (int)mRadius * 2;
- return new Rectangle(
- x, y,
- x + diameter,
- y + diameter
- );
- }
- }
- const float TestRectZCoordinate = 0;
- const int NumVertices = 4;
- public Rect(GraphicsDevice device, Vector3 center, Color[] colors, float radius)
- : base(device, center, colors, NumVertices, radius)
- {
- if (colors.Length < NumVertices)
- 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));
- mShader.VertexColorEnabled = true;
- mVertexBuf.SetData<VertexPositionColor>(
- new VertexPositionColor[]
- {
- new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]),
- new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]),
- new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]),
- new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3])
- });
- mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 0, 2, 3 });
- }
- public override void Update()
- {
- //TODO
- }
- public override void Draw()
- {
- var halfWidth = Device.Viewport.Width / 2f;
- var halfHeight = Device.Viewport.Height / 2f;
- // mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
- mShader.Projection = Matrix.CreateOrthographicOffCenter(-halfWidth, halfWidth, halfHeight, -halfHeight, 0, 1);
- Device.SetVertexBuffer(mVertexBuf);
- Device.Indices = mIndexBuf;
- foreach (var pass in mShader.CurrentTechnique.Passes)
- {
- pass.Apply();
- Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement