Advertisement
Guest User

ELF Games Framework: Main Class of Application

a guest
Feb 17th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using Elf.Games.Framework;
  3. using Elf.Games.Framework.Graphics;
  4. using Buffer = Elf.Games.Framework.Graphics.Buffer;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace ElfGameProject1
  8. {
  9.     class MyGame : Game
  10.     {
  11.         /// <summary>
  12.         /// The rotation of the mesh.
  13.         /// </summary>
  14.         Single rotation = 0;
  15.  
  16.         /// <summary>
  17.         /// The effect used for drawing the mesh.
  18.         /// </summary>
  19.         Effect effect;
  20.  
  21.         /// <summary>
  22.         /// Create the objects you need to run the game.
  23.         /// </summary>
  24.         protected override void Initialize()
  25.         {
  26.             base.Initialize();
  27.  
  28.             // Set the window title
  29.             WindowTitle = "Framework Demonstration";
  30.  
  31.             // Create the buffers for the box mesh data
  32.             var vertexBuffer = Buffer.FromData(Device, BufferType.VertexBuffer, BufferMode.Static, PrimitiveHelper.CreateBoxVertices(1, 1, 1));
  33.             var indexBuffer = Buffer.FromData(Device, BufferType.IndexBuffer, BufferMode.Static, PrimitiveHelper.CreateBoxIndices());
  34.  
  35.             // Create the camera matrices
  36.             var matrices = new Matrix[]
  37.             {
  38.                 Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 0), Vector3.Up),
  39.                 Matrix.CreatePerspectiveFieldOfView((Single)System.Math.PI / 180 * 45, 1, 0.01f, 1000f),
  40.             };
  41.  
  42.             // Create the effect
  43.             effect = new Effect();
  44.             effect.PrimitiveType = PrimitiveTopology.Triangles;
  45.  
  46.             // Setup the vertex shader stage
  47.             effect.VertexDeclaration = Vertex.Declaration;
  48.             effect.VertexShader.SetFunction(ShaderFunction.FromFile(Device, "basic.vs.fx", "VS", ShaderProfile.VS_5_0));
  49.             effect.VertexShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Static, matrices), 0);
  50.             effect.VertexShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Dynamic, Matrix.Identity), 1);
  51.  
  52.             // Setup the pixel shader stage
  53.             effect.PixelShader.SetFunction(ShaderFunction.FromFile(Device, "basic.ps.fx", "PS", ShaderProfile.PS_5_0));
  54.             effect.PixelShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Static, Color.Cyan), 0);
  55.             effect.PixelShader.SetTexture(new Texture2D(Device, "bricks.jpg"), 0);
  56.  
  57.             // Set the mesh data and the effect
  58.             Device.VertexBuffer = vertexBuffer;
  59.             Device.IndexBuffer = indexBuffer;
  60.             Device.Effect = effect;
  61.  
  62.             // Apply the configuration for the pipeline
  63.             Device.ApplyEffect();
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Update the game world and logic.
  68.         /// </summary>
  69.         protected override void Update()
  70.         {
  71.             // Update the mesh rotation
  72.             rotation += 0.01f;
  73.  
  74.             // Update the world matrix
  75.             effect.VertexShader.ConstantBuffers[1].Update(Matrix.CreateFromYawPitchRoll(rotation, rotation * 0.9f, rotation * 1.2f));
  76.  
  77.             base.Update();
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Draw the game world and UI.
  82.         /// </summary>
  83.         protected override void Draw()
  84.         {
  85.             // Set the background color
  86.             Device.Clear(Color.CornflowerBlue);
  87.  
  88.             // Draw the mesh
  89.             Device.DrawIndexed();
  90.  
  91.             base.Draw();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement