Code:
// Project Name: D3DTutorial
// D3DBlank.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;
public delegate void MessageDelegate(byte message); // Delegate for messages arriving via DirectPlay.
public delegate void AudioDelegate(); // Delegate to handle audio playback.
public delegate void PeerCloseCallback(); // This delegate will be called when the session terminated event is fired.
public class GraphicsClass : GraphicsSample
{
private D3DXFont drawingFont = null;
private Point destination = new Point(0, 0);
public GraphicsClass()
{
this.Text = "D3DTutorial";
this.KeyDown += new KeyEventHandler(this.OnPrivateKeyDown);
this.KeyUp += new KeyEventHandler(this.OnPrivateKeyUp);
drawingFont = new D3DXFont( "Arial", System.Drawing.FontStyle.Bold );
}
/// <summary>
/// Event Handler for windows messages
/// </summary>
private void OnPrivateKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
{
destination.X = 1;
break;
}
case Keys.Down:
{
destination.X = -1;
break;
}
case Keys.Left:
{
destination.Y = 1;
break;
}
case Keys.Right:
{
destination.Y = -1;
break;
}
}
}
private void OnPrivateKeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
destination.X = 0;
break;
case Keys.Left:
case Keys.Right:
{
destination.Y = 0;
break;
}
}
}
private VertexBuffer vertices;
protected bool InitializeGraphics() {
PresentParameters pres = new PresentParameters();
pres.Windowed = true;
pres.SwapEffect = SwapEffect.Discard;
device = new Device(0,
DeviceType.Hardware, this,
CreateFlags.HardwareVertexProcessing, pres);
vertices = CreateVertexBuffer(device);
return true;
}
protected VertexBuffer CreateVertexBuffer(Device device) {
device.VertexFormat = CustomVertex.TransformedColored.Format;
CustomVertex.TransformedColored[] verts =
new CustomVertex.TransformedColored[3];
verts[0] = new CustomVertex.TransformedColored(
this.Width/2, this.Height/4, 0.5f, 1.0f, Color.Red.ToArgb());
verts[1] = new CustomVertex.TransformedColored(
this.Width*3/4, this.Height*3/4, 0.5f, 1.0f, Color.Green.ToArgb());
verts[2] = new CustomVertex.TransformedColored(
this.Width/4, this.Height*3/4, 0.5f, 1.0f, Color.Blue.ToArgb());
VertexBuffer vbuf = new VertexBuffer(
typeof(CustomVertex.TransformedColored),
verts.Length,
device,
0,
CustomVertex.TransformedColored.Format,
Pool.Default);
GraphicsStream gs = vbuf.Lock(0, 0, 0);
gs.Write(verts);
vbuf.Unlock();
return vbuf;
}
/// <summary>
/// Called once per frame, the call is the entry point for 3d rendering. This
/// function sets up render states, clears the viewport, and renders the scene.
/// </summary>
protected override void Render()
{
//Clear the backbuffer to a Blue color
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
//Begin the scene
device.BeginScene();
drawingFont.DrawText(5, 5, Color.White.ToArgb(), "X: " + destination.X + " Y: " + destination.Y);
//
// TODO: Insert application rendering code here.
//
device.SetStreamSource(0, vertices, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();
}
protected void DisposeGraphics() {
device.Dispose();
}
/// <summary>
/// Initialize scene objects.
/// </summary>
protected override void InitializeDeviceObjects()
{
drawingFont.InitializeDeviceObjects(device);
//
// TODO: Insert application device initialization code here.
//
}
/// <summary>
/// Called when a device needs to be restored.
/// </summary>
protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
{
//
// TODO: Insert application device restoration code here.
//
}
}
I am reading the tutorial and entered the code as is in the