1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace game
{
public class Game : Form
{
static void Main()
{
Game g = new Game();
g.InitializeGraphics();
g.Show();
while (g.Created)
{
g.Render();
Application.DoEvents();
}
}
Game()
{
this.Size = new Size(800, 800);
this.Resize += new EventHandler(onResize);
}
private Device device;
private VertexBuffer vertices;
private void onResize(object o, EventArgs ea)
{
this.Size = new Size(800, 800);
}
protected bool InitializeGraphics()
{
PresentParameters pres = new PresentParameters();
pres.Windowed = true;
pres.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pres);
device.RenderState.Lighting = false;
vertices = CreateVertexBuffer(device);
return true;
}
protected VertexBuffer CreateVertexBuffer(Device device)
{
device.VertexFormat = CustomVertex.PositionColored.Format;
VertexBuffer buf = new VertexBuffer(typeof(CustomVertex.PositionColored), 6, device, 0, CustomVertex.PositionColored.Format, Pool.Default);
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])buf.Lock(0, 0);
verts[0] = new CustomVertex.PositionColored(-10, 10, 0, Color.Red.ToArgb());
verts[1] = new CustomVertex.PositionColored(10, 10, 0, Color.Green.ToArgb());
verts[2] = new CustomVertex.PositionColored(10, -10, 0, Color.Blue.ToArgb());
verts[3] = new CustomVertex.PositionColored(-10, 10, 0, Color.Red.ToArgb());
verts[4] = new CustomVertex.PositionColored(10, -10, 0, Color.Red.ToArgb());
verts[5] = new CustomVertex.PositionColored(-10, -10, 0, Color.Red.ToArgb());
buf.Unlock();
return buf;
}
protected void SetupMatrices()
{
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30F), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0F, 1.0F, 1.0F, 100.0F);
}
protected void Render()
{
device.Clear(ClearFlags.Target, Color.White, 1.0F, 0);
device.BeginScene();
SetupMatrices();
device.SetStreamSource(0, vertices, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
device.EndScene();
device.Present();
}
}
} |
Partager