123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Game1.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- 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.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- namespace DrawUserPrimitivesWindows
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- Matrix worldMatrix;
- Matrix viewMatrix;
- Matrix projectionMatrix;
- BasicEffect basicEffect;
- VertexDeclaration vertexDeclaration;
- VertexPositionColor[] pointList;
- VertexBuffer vertexBuffer;
- int points = 8;
- short[] lineListIndices;
- short[] lineStripIndices;
- short[] triangleListIndices;
- short[] triangleStripIndices;
- enum PrimType
- {
- LineList,
- LineStrip,
- TriangleList,
- TriangleStrip
- };
- PrimType typeToDraw = PrimType.LineList;
- RasterizerState rasterizerState;
- GamePadState currentGamePadState;
- GamePadState lastGamePadState;
- KeyboardState currentKeyboardState;
- KeyboardState lastKeyboardState;
- GraphicsDeviceManager graphics;
- 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()
- {
- InitializeTransform();
- InitializeEffect();
- InitializePoints();
- InitializeLineList();
- InitializeLineStrip();
- InitializeTriangleList();
- InitializeTriangleStrip();
- rasterizerState = new RasterizerState();
- rasterizerState.FillMode = FillMode.WireFrame;
- rasterizerState.CullMode = CullMode.None;
- base.Initialize();
- }
- /// <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>
- /// Initializes the transforms used by the game.
- /// </summary>
- private void InitializeTransform()
- {
- viewMatrix = Matrix.CreateLookAt(
- new Vector3(0.0f, 0.0f, 1.0f),
- Vector3.Zero,
- Vector3.Up
- );
- projectionMatrix = Matrix.CreateOrthographicOffCenter(
- 0,
- (float)GraphicsDevice.Viewport.Width,
- (float)GraphicsDevice.Viewport.Height,
- 0,
- 1.0f, 1000.0f);
- }
- /// <summary>
- /// Initializes the effect (loading, parameter setting, and technique selection)
- /// used by the game.
- /// </summary>
- private void InitializeEffect()
- {
- vertexDeclaration = new VertexDeclaration(new VertexElement[]
- {
- new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
- new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0)
- }
- );
- basicEffect = new BasicEffect(GraphicsDevice);
- basicEffect.VertexColorEnabled = true;
- worldMatrix = Matrix.CreateTranslation(GraphicsDevice.Viewport.Width / 2f - 150,
- GraphicsDevice.Viewport.Height / 2f - 50, 0);
- basicEffect.World = worldMatrix;
- basicEffect.View = viewMatrix;
- basicEffect.Projection = projectionMatrix;
- }
- /// <summary>
- /// Initializes the point list.
- /// </summary>
- private void InitializePoints()
- {
- pointList = new VertexPositionColor[points];
- for (int x = 0; x < points / 2; x++)
- {
- for (int y = 0; y < 2; y++)
- {
- pointList[(x * 2) + y] = new VertexPositionColor(
- new Vector3(x * 100, y * 100, 0), Color.White);
- }
- }
- // Initialize the vertex buffer, allocating memory for each vertex.
- vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, vertexDeclaration,
- points, BufferUsage.None);
- // Set the vertex buffer data to the array of vertices.
- vertexBuffer.SetData<VertexPositionColor>(pointList);
- }
- /// <summary>
- /// Initializes the line list.
- /// </summary>
- private void InitializeLineList()
- {
- // Initialize an array of indices of type short.
- lineListIndices = new short[(points * 2) - 2];
- // Populate the array with references to indices in the vertex buffer
- for (int i = 0; i < points - 1; i++)
- {
- lineListIndices[i * 2] = (short)(i);
- lineListIndices[(i * 2) + 1] = (short)(i + 1);
- }
- }
- /// <summary>
- /// Initializes the line strip.
- /// </summary>
- private void InitializeLineStrip()
- {
- // Initialize an array of indices of type short.
- lineStripIndices = new short[points];
- // Populate the array with references to indices in the vertex buffer.
- for (int i = 0; i < points; i++)
- {
- lineStripIndices[i] = (short)(i);
- }
- }
- /// <summary>
- /// Initializes the triangle list.
- /// </summary>
- private void InitializeTriangleList()
- {
- int width = 4;
- int height = 2;
- triangleListIndices = new short[(width - 1) * (height - 1) * 6];
- for (int x = 0; x < width - 1; x++)
- {
- for (int y = 0; y < height - 1; y++)
- {
- triangleListIndices[(x + y * (width - 1)) * 6] = (short)(2 * x);
- triangleListIndices[(x + y * (width - 1)) * 6 + 1] = (short)(2 * x + 1);
- triangleListIndices[(x + y * (width - 1)) * 6 + 2] = (short)(2 * x + 2);
- triangleListIndices[(x + y * (width - 1)) * 6 + 3] = (short)(2 * x + 2);
- triangleListIndices[(x + y * (width - 1)) * 6 + 4] = (short)(2 * x + 1);
- triangleListIndices[(x + y * (width - 1)) * 6 + 5] = (short)(2 * x + 3);
- }
- }
- }
- /// <summary>
- /// Initializes the triangle strip.
- /// </summary>
- private void InitializeTriangleStrip()
- {
- // Initialize an array of indices of type short.
- triangleStripIndices = new short[points];
- // Populate the array with references to indices in the vertex buffer.
- for (int i = 0; i < points; i++)
- {
- triangleStripIndices[i] = (short)i;
- }
- }
- /// <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();
- #if MAC || WINDOWS
- if (Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- #endif
- CheckGamePadInput();
- CheckKeyboardInput();
- base.Update(gameTime);
- }
- /// <summary>
- /// Determines which primitive to draw based on input from the keyboard
- /// or game pad.
- /// </summary>
- private void CheckGamePadInput()
- {
- lastGamePadState = currentGamePadState;
- currentGamePadState = GamePad.GetState(PlayerIndex.One);
- if (((currentGamePadState.Buttons.A == ButtonState.Pressed)) &&
- (lastGamePadState.Buttons.A == ButtonState.Released))
- {
- typeToDraw++;
- if (typeToDraw > PrimType.TriangleStrip)
- typeToDraw = 0;
- }
- }
- /// <summary>
- /// Determines which primitive to draw based on input from the keyboard
- /// or game pad.
- /// </summary>
- private void CheckKeyboardInput()
- {
- lastKeyboardState = currentKeyboardState;
- currentKeyboardState = Keyboard.GetState();
- if (currentKeyboardState.IsKeyDown(Keys.A) &&
- lastKeyboardState.IsKeyUp(Keys.A))
- {
- typeToDraw++;
- if (typeToDraw > PrimType.TriangleStrip)
- typeToDraw = 0;
- }
- }
- /// <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.SteelBlue);
- // The effect is a compiled effect created and compiled elsewhere
- // in the application.
- foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
- {
- pass.Apply();
- switch (typeToDraw)
- {
- case PrimType.LineList:
- DrawLineList();
- break;
- // case PrimType.LineStrip:
- // DrawLineStrip();
- // break;
- // case PrimType.TriangleList:
- // GraphicsDevice.RasterizerState = rasterizerState;
- // DrawTriangleList();
- // break;
- // case PrimType.TriangleStrip:
- // GraphicsDevice.RasterizerState = rasterizerState;
- // DrawTriangleStrip();
- // break;
- }
- }
- base.Draw(gameTime);
- }
- /// <summary>
- /// Draws the line list.
- /// </summary>
- private void DrawLineList()
- {
- GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
- PrimitiveType.LineList,
- pointList,
- 0, // vertex buffer offset to add to each element of the index buffer
- 8, // number of vertices in pointList
- lineListIndices, // the index buffer
- 0, // first index element to read
- 7 // number of primitives to draw
- );
- }
- /// <summary>
- /// Draws the line strip.
- /// </summary>
- // private void DrawLineStrip()
- // {
- // for (int i = 0; i < pointList.Length; i++)
- // pointList[i].Color = Color.Red;
- //
- // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
- // PrimitiveType.LineStrip,
- // pointList,
- // 0, // vertex buffer offset to add to each element of the index buffer
- // 8, // number of vertices to draw
- // lineStripIndices,
- // 0, // first index element to read
- // 7 // number of primitives to draw
- // );
- // for (int i = 0; i < pointList.Length; i++)
- // pointList[i].Color = Color.White;
- //
- // }
- //
- // /// <summary>
- // /// Draws the triangle list.
- // /// </summary>
- // private void DrawTriangleList()
- // {
- // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
- // PrimitiveType.TriangleList,
- // pointList,
- // 0, // vertex buffer offset to add to each element of the index buffer
- // 8, // number of vertices to draw
- // triangleListIndices,
- // 0, // first index element to read
- // 6 // number of primitives to draw
- // );
- // }
- //
- // /// <summary>
- // /// Draws the triangle strip.
- // /// </summary>
- // private void DrawTriangleStrip()
- // {
- // for (int i = 0; i < pointList.Length; i++)
- // pointList[i].Color = Color.Red;
- //
- // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
- // PrimitiveType.TriangleStrip,
- // pointList,
- // 0, // vertex buffer offset to add to each element of the index buffer
- // 8, // number of vertices to draw
- // triangleStripIndices,
- // 0, // first index element to read
- // 6 // number of primitives to draw
- // );
- // for (int i = 0; i < pointList.Length; i++)
- // pointList[i].Color = Color.White;
- //
- // }
- }
- }
|