using System;
using Elf.Games.Framework;
using Elf.Games.Framework.Graphics;
using Buffer = Elf.Games.Framework.Graphics.Buffer;
using Microsoft.Xna.Framework;
namespace ElfGameProject1
{
class MyGame : Game
{
/// <summary>
/// The rotation of the mesh.
/// </summary>
Single rotation = 0;
/// <summary>
/// The effect used for drawing the mesh.
/// </summary>
Effect effect;
/// <summary>
/// Create the objects you need to run the game.
/// </summary>
protected override void Initialize()
{
base.Initialize();
// Set the window title
WindowTitle = "Framework Demonstration";
// Create the buffers for the box mesh data
var vertexBuffer = Buffer.FromData(Device, BufferType.VertexBuffer, BufferMode.Static, PrimitiveHelper.CreateBoxVertices(1, 1, 1));
var indexBuffer = Buffer.FromData(Device, BufferType.IndexBuffer, BufferMode.Static, PrimitiveHelper.CreateBoxIndices());
// Create the camera matrices
var matrices = new Matrix[]
{
Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 0), Vector3.Up),
Matrix.CreatePerspectiveFieldOfView((Single)System.Math.PI / 180 * 45, 1, 0.01f, 1000f),
};
// Create the effect
effect = new Effect();
effect.PrimitiveType = PrimitiveTopology.Triangles;
// Setup the vertex shader stage
effect.VertexDeclaration = Vertex.Declaration;
effect.VertexShader.SetFunction(ShaderFunction.FromFile(Device, "basic.vs.fx", "VS", ShaderProfile.VS_5_0));
effect.VertexShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Static, matrices), 0);
effect.VertexShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Dynamic, Matrix.Identity), 1);
// Setup the pixel shader stage
effect.PixelShader.SetFunction(ShaderFunction.FromFile(Device, "basic.ps.fx", "PS", ShaderProfile.PS_5_0));
effect.PixelShader.SetConstantBuffer(Buffer.FromData(Device, BufferType.ConstantBuffer, BufferMode.Static, Color.Cyan), 0);
effect.PixelShader.SetTexture(new Texture2D(Device, "bricks.jpg"), 0);
// Set the mesh data and the effect
Device.VertexBuffer = vertexBuffer;
Device.IndexBuffer = indexBuffer;
Device.Effect = effect;
// Apply the configuration for the pipeline
Device.ApplyEffect();
}
/// <summary>
/// Update the game world and logic.
/// </summary>
protected override void Update()
{
// Update the mesh rotation
rotation += 0.01f;
// Update the world matrix
effect.VertexShader.ConstantBuffers[1].Update(Matrix.CreateFromYawPitchRoll(rotation, rotation * 0.9f, rotation * 1.2f));
base.Update();
}
/// <summary>
/// Draw the game world and UI.
/// </summary>
protected override void Draw()
{
// Set the background color
Device.Clear(Color.CornflowerBlue);
// Draw the mesh
Device.DrawIndexed();
base.Draw();
}
}
}