Hi guys, i'm lamer in programming with DirectX, but actually have to make project of 3D game, so i came here to ask your help :)
I got 2 books about programming in .NET with DirectX, but actually no one of them helped me, cuz the code is written wrong (may be they wrote it for c# 2005). So now i learn the basics from internet. I got stuck on the second tutorial of creating a 2D triangle. I took the code from here and here , combined together and i didnt get errors, but after i run the program, it tells me about the error in one line, that i cant really understand why there is a mistake. As i said, i dont know where the mistake is, so i put here all the code. Sorry for inconveniece.
namespace Project1
{
public class CreateDevice: Form
{
private Device device = null;
VertexBuffer vertexBuffer;
public void OnCreateDevice(object sender, EventArgs e)
{
CustomVertex.TransformedColored[ verts = new CustomVertex.TransformedColored[3];
Device dev = (Device)sender;
// Now create the vertex buffer
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 3, dev, 0,
CustomVertex.TransformedColored.Format, Pool.Default);
GraphicsStream stm = vertexBuffer.Lock(0, 0, LockFlags.None);
verts[0].X = 150; verts[0].Y = 50; verts[0].Z = 0.5f; verts[0].Rhw = 1;
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
verts[1].X = 250; verts[1].Y = 250; verts[1].Z = 0.5f; verts[1].Rhw = 1;
verts[1].Color = System.Drawing.Color.Brown.ToArgb();
verts[2].X = 50; verts[2].Y = 250; verts[2].Z = 0.5f; verts[2].Rhw = 1;
verts[2].Color = System.Drawing.Color.LightPink.ToArgb();
stm.Write(verts);
vertexBuffer.Unlock();
}
public CreateDevice()
{
this.ClientSize = new System.Drawing.Size(400,300);
this.Text = "D3D Tutorial 01: CreateDevice";
}
public bool InitializeGraphics()
{
try
{
// Now setup our D3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
return true;
}
catch (DirectXException)
{
return false;
}
}
static void Main()
{
using (CreateDevice frm = new CreateDevice())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
frm.Show();
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
public void Render()
{
if (device == null)
return;
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
device.BeginScene();
device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); - Here the error is
device.EndScene();
device.Present();
}
}
}